Skip to content

Commit 2632f65

Browse files
michaelpqpull[bot]
authored andcommitted
Add error context callback when tokenizing authentication files
The parsing of the authentication files for HBA and ident entries happens in two phases: - Tokenization of the files, creating a list of TokenizedAuthLines. - Validation of the HBA and ident entries, building a set of HbaLines or IdentLines. The second phase doing the validation provides already some error context about the configuration file and the line where a problem happens, but there is no such information in the first phase when tokenizing the files. This commit adds an ErrorContextCallback in tokenize_auth_file(), with a context made of the line number and the configuration file name involved in a problem. This is useful for files included in an HBA file for user and database lists, and it will become much more handy to track problems for files included via a potential @include[_dir,_if_exists]. The error context is registered so as the full chain of events is reported when using cascaded inclusions when for example tokenize_auth_file() recurses over itself on new files, displaying one context line for each file gone through when tokenizing things. Author: Michael Paquier Reviewed-by: Julien Rouhaud Discussion: https://postgr.es/m/Y2xUBJ+S+Z0zbxRW@paquier.xyz
1 parent 7aa6f75 commit 2632f65

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/backend/libpq/hba.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ typedef struct check_network_data
6666
bool result; /* set to true if match */
6767
} check_network_data;
6868

69+
typedef struct
70+
{
71+
const char *filename;
72+
int linenum;
73+
} tokenize_error_callback_arg;
6974

7075
#define token_has_regexp(t) (t->regex != NULL)
7176
#define token_is_keyword(t, k) (!t->quoted && strcmp(t->string, k) == 0)
@@ -125,6 +130,7 @@ static int regcomp_auth_token(AuthToken *token, char *filename, int line_num,
125130
char **err_msg, int elevel);
126131
static int regexec_auth_token(const char *match, AuthToken *token,
127132
size_t nmatch, regmatch_t pmatch[]);
133+
static void tokenize_error_callback(void *arg);
128134

129135

130136
/*
@@ -570,6 +576,18 @@ open_auth_file(const char *filename, int elevel, int depth,
570576
return file;
571577
}
572578

579+
/*
580+
* error context callback for tokenize_auth_file()
581+
*/
582+
static void
583+
tokenize_error_callback(void *arg)
584+
{
585+
tokenize_error_callback_arg *callback_arg = (tokenize_error_callback_arg *) arg;
586+
587+
errcontext("line %d of configuration file \"%s\"",
588+
callback_arg->linenum, callback_arg->filename);
589+
}
590+
573591
/*
574592
* tokenize_auth_file
575593
* Tokenize the given file.
@@ -598,6 +616,16 @@ tokenize_auth_file(const char *filename, FILE *file, List **tok_lines,
598616
StringInfoData buf;
599617
MemoryContext linecxt;
600618
MemoryContext oldcxt;
619+
ErrorContextCallback tokenerrcontext;
620+
tokenize_error_callback_arg callback_arg;
621+
622+
callback_arg.filename = filename;
623+
callback_arg.linenum = line_number;
624+
625+
tokenerrcontext.callback = tokenize_error_callback;
626+
tokenerrcontext.arg = (void *) &callback_arg;
627+
tokenerrcontext.previous = error_context_stack;
628+
error_context_stack = &tokenerrcontext;
601629

602630
linecxt = AllocSetContextCreate(CurrentMemoryContext,
603631
"tokenize_auth_file",
@@ -686,10 +714,13 @@ tokenize_auth_file(const char *filename, FILE *file, List **tok_lines,
686714
}
687715

688716
line_number += continuations + 1;
717+
callback_arg.linenum = line_number;
689718
}
690719

691720
MemoryContextSwitchTo(oldcxt);
692721

722+
error_context_stack = tokenerrcontext.previous;
723+
693724
return linecxt;
694725
}
695726

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,6 +3723,7 @@ timeout_params
37233723
timerCA
37243724
tlist_vinfo
37253725
toast_compress_header
3726+
tokenize_error_callback_arg
37263727
transferMode
37273728
transfer_thread_arg
37283729
trgm

0 commit comments

Comments
 (0)