Skip to content

Commit a903971

Browse files
committed
Refactor more logic for compilation of regular expressions in hba.c
It happens that the parts of hba.conf that are planned to be extended to support regular expressions would finish by using the same error message as the one used currently for pg_ident.conf when a regular expression cannot be compiled, as long as the routine centralizing the logic, regcomp_auth_token(), knows from which file the regexp comes from and its line location in the so-said file. This change makes the follow-up patches slightly simpler, and the logic remains the same. I suspect that this makes the proposal to add support for file inclusions in pg_ident.conf and pg_hba.conf slightly simpler, as well. Extracted from a larger patch by the same author. This is similar to the refactoring done in fc579e1. Author: Bertrand Drouvot Discussion: https://postgr.es/m/[email protected]
1 parent 42d01f5 commit a903971

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/backend/libpq/hba.c

+25-19
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ static List *tokenize_inc_file(List *tokens, const char *outer_filename,
119119
const char *inc_filename, int elevel, char **err_msg);
120120
static bool parse_hba_auth_opt(char *name, char *val, HbaLine *hbaline,
121121
int elevel, char **err_msg);
122-
static int regcomp_auth_token(AuthToken *token);
122+
static int regcomp_auth_token(AuthToken *token, char *filename, int line_num,
123+
char **err_msg, int elevel);
123124
static int regexec_auth_token(const char *match, AuthToken *token,
124125
size_t nmatch, regmatch_t pmatch[]);
125126

@@ -305,10 +306,12 @@ copy_auth_token(AuthToken *in)
305306

306307
/*
307308
* Compile the regular expression and store it in the AuthToken given in
308-
* input. Returns the result of pg_regcomp().
309+
* input. Returns the result of pg_regcomp(). On error, the details are
310+
* stored in "err_msg".
309311
*/
310312
static int
311-
regcomp_auth_token(AuthToken *token)
313+
regcomp_auth_token(AuthToken *token, char *filename, int line_num,
314+
char **err_msg, int elevel)
312315
{
313316
pg_wchar *wstr;
314317
int wlen;
@@ -326,6 +329,22 @@ regcomp_auth_token(AuthToken *token)
326329

327330
rc = pg_regcomp(token->regex, wstr, wlen, REG_ADVANCED, C_COLLATION_OID);
328331

332+
if (rc)
333+
{
334+
char errstr[100];
335+
336+
pg_regerror(rc, token->regex, errstr, sizeof(errstr));
337+
ereport(elevel,
338+
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
339+
errmsg("invalid regular expression \"%s\": %s",
340+
token->string + 1, errstr),
341+
errcontext("line %d of configuration file \"%s\"",
342+
line_num, filename)));
343+
344+
*err_msg = psprintf("invalid regular expression \"%s\": %s",
345+
token->string + 1, errstr);
346+
}
347+
329348
pfree(wstr);
330349
return rc;
331350
}
@@ -2374,7 +2393,6 @@ parse_ident_line(TokenizedAuthLine *tok_line, int elevel)
23742393
List *tokens;
23752394
AuthToken *token;
23762395
IdentLine *parsedline;
2377-
int rc;
23782396

23792397
Assert(tok_line->fields != NIL);
23802398
field = list_head(tok_line->fields);
@@ -2410,22 +2428,10 @@ parse_ident_line(TokenizedAuthLine *tok_line, int elevel)
24102428
* Now that the field validation is done, compile a regex from the user
24112429
* token, if necessary.
24122430
*/
2413-
rc = regcomp_auth_token(parsedline->token);
2414-
if (rc)
2431+
if (regcomp_auth_token(parsedline->token, IdentFileName, line_num,
2432+
err_msg, elevel))
24152433
{
2416-
char errstr[100];
2417-
2418-
pg_regerror(rc, parsedline->token->regex, errstr, sizeof(errstr));
2419-
ereport(elevel,
2420-
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
2421-
errmsg("invalid regular expression \"%s\": %s",
2422-
parsedline->token->string + 1, errstr),
2423-
errcontext("line %d of configuration file \"%s\"",
2424-
line_num, IdentFileName)));
2425-
2426-
*err_msg = psprintf("invalid regular expression \"%s\": %s",
2427-
parsedline->token->string + 1, errstr);
2428-
2434+
/* err_msg includes the error to report */
24292435
return NULL;
24302436
}
24312437

0 commit comments

Comments
 (0)