Fix line number reporting on config file errors after <inline> segments
PATCH 1/1 Fix line number reporting on config file errors after <inline> segments
<inline> segments neglected to increment the "current line number in config file" variable (line_num), so after the first <inline>, errors reported have the wrong line number.
Fix by introducing an extra argument to the check_inline*() / read_inline_file() call chain: "so many lines in the inline block".
Trac: #1325
--- src/openvpn/options.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
src/openvpn/options.c +20 -9
@@ -4634,7 +4634,8 @@ in_src_get(const struct in_src *is, char *line, const int size) }static char * -read_inline_file (struct in_src *is,const char *close_tag,struct gc_arena *gc) +read_inline_file (struct in_src *is,const char *close_tag, +int *num_lines,struct gc_arena *gc) {char line[OPTION_LINE_SIZE ];struct buffer buf =alloc_buf (8 *OPTION_LINE_SIZE );
@@ -4643,6 +4644,7 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)while (in_src_get (is, line,sizeof (line))) { + (*num_lines)++;char *line_ptr = line;/* Remove leading spaces */ while (isspace (*line_ptr))
@@ -4677,9 +4679,11 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc) }static bool -check_inline_file (struct in_src *is,char *p[],struct gc_arena *gc) +check_inline_file (struct in_src *is,char *p[], +int *num_lines,struct gc_arena *gc) {bool is_inline = false; + *num_lines =0 ;if (p[0 ] && !p[1 ]) {
@@ -4692,7 +4696,7 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), num_lines, gc); p[2 ] =NULL ;free_buf (&close_tag); is_inline = true;
@@ -4702,22 +4706,23 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) }static bool -check_inline_file_via_fp (FILE *fp,char *p[],struct gc_arena *gc) +check_inline_file_via_fp (FILE *fp,char *p[], +int *num_lines,struct gc_arena *gc) {struct in_src is; is.type =IS_TYPE_FP ; is.u.fp = fp; -return check_inline_file (&is, p, gc); +return check_inline_file (&is, p, num_lines, gc); }static bool check_inline_file_via_buf (struct buffer *multiline,char *p[], -struct gc_arena *gc) +int *num_lines,struct gc_arena *gc) {struct in_src is; is.type =IS_TYPE_BUF ; is.u.multiline = multiline; -return check_inline_file (&is, p, gc); +return check_inline_file (&is, p, num_lines, gc); } static void
@@ -4782,12 +4787,15 @@ read_config_file(struct options *options,if (parse_line (line + offset, p,SIZE (p)-1 , file, line_num, msglevel, &options->gc)) {bool is_inline; +int lines;bypass_doubledash (&p[0 ]); - is_inline =check_inline_file_via_fp (fp, p, &options->gc); + is_inline =check_inline_file_via_fp (fp, p, + &lines, &options->gc);add_option (options, p, is_inline, file, line_num, level, msglevel, permission_mask, option_types_found, es); + line_num += lines; } }if (fp != stdin)
@@ -4831,11 +4839,14 @@ read_config_string(const char *prefix,if (parse_line (line, p,SIZE (p)-1 , prefix, line_num, msglevel, &options->gc)) {bool is_inline; +int lines;bypass_doubledash (&p[0 ]); - is_inline =check_inline_file_via_buf (&multiline, p, &options->gc); + is_inline =check_inline_file_via_buf (&multiline, p, + &lines, &options->gc);add_option (options, p, is_inline, prefix, line_num,0 , msglevel, permission_mask, option_types_found, es); + line_num += lines; }CLEAR (p); }
Signature · 1 line
2.26.2
Hi,
Quoted 37 lines · Gert Doering
<inline> segments neglected to increment the "current line number in config file" variable (
line_num), so after the first <inline>, errors reported have the wrong line number.Fix by introducing an extra argument to the check_inline*() /
read_inline_file()call chain: "so many lines in the inline block".Trac: #1325
Signed-off-by Gert Doering <[email protected]...>--- src/openvpn/options.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)src/openvpn/options.c +3
-1@@ -4634,7 +4634,8 @@ in_src_get(const struct in_src *is, char *line, const int size) }static char * -read_inline_file (struct in_src *is,const char *close_tag,struct gc_arena *gc) +read_inline_file (struct in_src *is,const char *close_tag, +int *num_lines,struct gc_arena *gc) {char line[OPTION_LINE_SIZE ];struct buffer buf =alloc_buf (8 *OPTION_LINE_SIZE );@@ -4643,6 +4644,7 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)while (in_src_get (is, line,sizeof (line))) { + (*num_lines)++;char *line_ptr = line;/* Remove leading spaces */ while (isspace (*line_ptr))
src/openvpn/options.c read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc) · line 4683
Quoted 6 lines
src/openvpn/options.c +2
-1@@ -4677,9 +4679,11 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc) }static bool -check_inline_file (struct in_src *is,char *p[],struct gc_arena *gc) +check_inline_file (struct in_src *is,char *p[], +int *num_lines,struct gc_arena *gc)
Sorry for not chiming in earlier, but honestly I believe your other option would be "cleaner". The other option being "return int instead of bool, where the returned value is the number of lines of the inline'd material.
If 0, we know there was no inline'd material, thus we can treat that result as "not inline'd".
Anyway, this is just my opinion. If you believe this is a better approach, I am fine with it too.
src/openvpn/options.c check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) · line 4699
Quoted 12 lines
src/openvpn/options.c +2
-1{bool is_inline = false; + *num_lines =0 ;if (p[0 ] && !p[1 ]) {@@ -4692,7 +4696,7 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), num_lines, gc);
am I wrong or here we should add 1 to num_lines to include the opening tag that was parsed *before* entering read_inline_file()?
Regards,
Signature · 1 line
Antonio Quartulli
Hi,
Quoted 7 lines · Antonio Quartulli
Sorry for not chiming in earlier, but honestly I believe your other option would be "cleaner". The other option being "return int instead of bool, where the returned value is the number of lines of the inline'd material.
If 0, we know there was no inline'd material, thus we can treat that result as "not inline'd".
We could do that, indeed. I was torn between both variants - this one is a bit more intrusive (because it adds a new argument to so many functions), but on the other hand, the bool stays a bool and is not magically overloaded - which would make the call to add_option() either "even longer", or if you rely on magic "we just pass the integer to a bool-expecting function" much harder to understand.
Quoted 2 lines
Anyway, this is just my opinion. If you believe this is a better approach, I am fine with it too.
I wasn't sure. In the end, this one didn't feel so bad, so this is what we have.
Maybe I'll do the other one as well, for fun, and to compare :-)
Quoted 9 lines
src/openvpn/options.c check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) · line 4699
Quoted 6 lines
src/openvpn/options.c +1
-1@@ -4692,7 +4696,7 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), num_lines, gc);am I wrong or here we should add 1 to
num_linesto include the opening tag that was parsed *before* enteringread_inline_file()?
The opening tag is parsed as "regular" line in read_config_file(), and as such, the "++line_num" in there applies.
(I have tested this :-) - nothing more embarrassing than a patch to fix line numbers actually still printing wrong line numbers)
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Antonio Quartulli
Acked-byHi,
Quoted 36 lines · Gert Doering
Hi,
Quoted 7 lines · Antonio Quartulli
Sorry for not chiming in earlier, but honestly I believe your other option would be "cleaner". The other option being "return int instead of bool, where the returned value is the number of lines of the inline'd material.
If 0, we know there was no inline'd material, thus we can treat that result as "not inline'd".
We could do that, indeed. I was torn between both variants - this one is a bit more intrusive (because it adds a new argument to so many functions), but on the other hand, the bool stays a bool and is not magically overloaded - which would make the call to
add_option()either "even longer", or if you rely on magic "we just pass the integer to a bool-expecting function" much harder to understand.Quoted 2 lines
Anyway, this is just my opinion. If you believe this is a better approach, I am fine with it too.
I wasn't sure. In the end, this one didn't feel so bad, so this is what we have.
Maybe I'll do the other one as well, for fun, and to compare :-)
Quoted 9 lines
src/openvpn/options.c check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) · line 4699
Quoted 6 lines
src/openvpn/options.c +1
-1@@ -4692,7 +4696,7 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), num_lines, gc);am I wrong or here we should add 1 to
num_linesto include the opening tag that was parsed *before* enteringread_inline_file()?
Quoted 6 lines
The opening tag is parsed as "regular" line in
read_config_file(), and as such, the "++line_num" in there applies.(I have tested this :-) - nothing more embarrassing than a patch to fix line numbers actually still printing wrong line numbers)
Ok, then for me this patch is good to go.
FTR on IRC we discussed the possibility of using the "other approach". Gert may give it a try and decide if he wants to send that version to the ml as well.
In any case, this patch gets my blessing:
Signature · 1 line
Antonio Quartulli
PATCH v2 1/1 Fix line number reporting on config file errors after <inline> segments
In reply to Gert Doering<inline> segments neglected to increment the "current line number in config file" variable (line_num), so after the first <inline>, errors reported have the wrong line number.
Fix by introducing an extra argument to read_inline_file() function:
"so many lines in the inline block", and changing the return values of the "check_inline*()" functions to "int", changing this from "false/true" to "0 = no inline, 1...N = inline with <N> lines".
On calling add_options() this is implicitly converted back to bool.
v2: use int return value, not extra call-by-reference parameter
Trac: #1325
--- src/openvpn/options.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
src/openvpn/options.c +17 -16
@@ -4659,7 +4659,8 @@ in_src_get(const struct in_src *is, char *line, const int size) }static char * -read_inline_file (struct in_src *is,const char *close_tag,struct gc_arena *gc) +read_inline_file (struct in_src *is,const char *close_tag, +int *num_lines,struct gc_arena *gc) {char line[OPTION_LINE_SIZE ];struct buffer buf =alloc_buf (8 *OPTION_LINE_SIZE );
@@ -4668,6 +4669,7 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)while (in_src_get (is, line,sizeof (line))) { + (*num_lines)++;char *line_ptr = line;/* Remove leading spaces */ while (isspace (*line_ptr))
@@ -4701,10 +4703,10 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)return ret; } -static bool +static int check_inline_file (struct in_src *is,char *p[],struct gc_arena *gc) { -bool is_inline = false; +int num_inline_lines =0 ;if (p[0 ] && !p[1 ]) {
@@ -4717,16 +4719,15 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), &num_inline_lines, gc); p[2 ] =NULL ;free_buf (&close_tag); - is_inline = true; } } -return is_inline; +return num_inline_lines; } -static bool +static int check_inline_file_via_fp (FILE *fp,char *p[],struct gc_arena *gc) {struct in_src is;
@@ -4735,7 +4736,7 @@ check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc)return check_inline_file (&is, p, gc); } -static bool +static int check_inline_file_via_buf (struct buffer *multiline,char *p[],struct gc_arena *gc) {
@@ -4806,13 +4807,13 @@ read_config_file(struct options *options, }if (parse_line (line + offset, p,SIZE (p)-1 , file, line_num, msglevel, &options->gc)) { -bool is_inline; -bypass_doubledash (&p[0 ]); - is_inline =check_inline_file_via_fp (fp, p, &options->gc); -add_option (options, p, is_inline, file, line_num, level, +int lines_inline = +check_inline_file_via_fp (fp, p, &options->gc); +add_option (options, p, lines_inline, file, line_num, level, msglevel, permission_mask, option_types_found, es); + line_num += lines_inline; } }if (fp != stdin)
@@ -4855,12 +4856,12 @@ read_config_string(const char *prefix, ++line_num;if (parse_line (line, p,SIZE (p)-1 , prefix, line_num, msglevel, &options->gc)) { -bool is_inline; -bypass_doubledash (&p[0 ]); - is_inline =check_inline_file_via_buf (&multiline, p, &options->gc); -add_option (options, p, is_inline, prefix, line_num,0 , msglevel, +int lines_inline = +check_inline_file_via_buf (&multiline, p, &options->gc); +add_option (options, p, lines_inline, prefix, line_num,0 , msglevel, permission_mask, option_types_found, es); + line_num += lines_inline; }CLEAR (p); }
Signature · 1 line
2.26.2
Hi,
Quoted 83 lines · Gert Doering
<inline> segments neglected to increment the "current line number in config file" variable (
line_num), so after the first <inline>, errors reported have the wrong line number.Fix by introducing an extra argument to
read_inline_file()function:
"so many lines in the inline block", and changing the return values of the "check_inline*()" functions to "int", changing this from "false/true" to "0 = no inline, 1...N = inline with <N> lines".On calling
add_options()this is implicitly converted back to bool.v2: use int return value, not extra call-by-reference parameter
Trac: #1325
--- src/openvpn/options.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)src/openvpn/options.c +9
-8@@ -4659,7 +4659,8 @@ in_src_get(const struct in_src *is, char *line, const int size) }static char * -read_inline_file (struct in_src *is,const char *close_tag,struct gc_arena *gc) +read_inline_file (struct in_src *is,const char *close_tag, +int *num_lines,struct gc_arena *gc) {char line[OPTION_LINE_SIZE ];struct buffer buf =alloc_buf (8 *OPTION_LINE_SIZE );@@ -4668,6 +4669,7 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)while (in_src_get (is, line,sizeof (line))) { + (*num_lines)++;char *line_ptr = line;/* Remove leading spaces */ while (isspace (*line_ptr))@@ -4701,10 +4703,10 @@ read_inline_file(struct in_src *is, const char *close_tag, struct gc_arena *gc)return ret; } -static bool +static int check_inline_file (struct in_src *is,char *p[],struct gc_arena *gc) { -bool is_inline = false; +int num_inline_lines =0 ;if (p[0 ] && !p[1 ]) {@@ -4717,16 +4719,15 @@ check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) p[0 ] =string_alloc (arg +1 , gc); close_tag =alloc_buf (strlen (p[0 ]) +4 );buf_printf (&close_tag,"</%s>" , p[0 ]); - p[1 ] =read_inline_file (is,BSTR (&close_tag), gc); + p[1 ] =read_inline_file (is,BSTR (&close_tag), &num_inline_lines, gc); p[2 ] =NULL ;free_buf (&close_tag); - is_inline = true; } } -return is_inline; +return num_inline_lines; } -static bool +static int check_inline_file_via_fp (FILE *fp,char *p[],struct gc_arena *gc) {struct in_src is;@@ -4735,7 +4736,7 @@ check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc)return check_inline_file (&is, p, gc); } -static bool +static int check_inline_file_via_buf (struct buffer *multiline,char *p[],struct gc_arena *gc) {
src/openvpn/options.c read_config_file(struct options *options, · line 4812
Quoted 10 lines
src/openvpn/options.c +2
-4@@ -4806,13 +4807,13 @@ read_config_file(struct options *options, }if (parse_line (line + offset, p,SIZE (p)-1 , file, line_num, msglevel, &options->gc)) { -bool is_inline; -bypass_doubledash (&p[0 ]); - is_inline =check_inline_file_via_fp (fp, p, &options->gc); - add_option(options, p, is_inline, file, line_num, level, +int lines_inline = +check_inline_file_via_fp (fp, p, &options->gc);
This is quite ugly - maybe we could just keep it on one line (as we recently agreed that we can go beyond 80 chars if useful)?
Quoted 15 lines
src/openvpn/options.c +2
-2+add_option (options, p, lines_inline, file, line_num, level, msglevel, permission_mask, option_types_found, es); + line_num += lines_inline; } }if (fp != stdin)@@ -4855,12 +4856,12 @@ read_config_string(const char *prefix, ++line_num;if (parse_line (line, p,SIZE (p)-1 , prefix, line_num, msglevel, &options->gc)) { -bool is_inline; -bypass_doubledash (&p[0 ]);
src/openvpn/options.c read_config_string(const char *prefix, · line 4867
Quoted 10 lines
src/openvpn/options.c +4
-2- is_inline =check_inline_file_via_buf (&multiline, p, &options->gc); -add_option (options, p, is_inline, prefix, line_num,0 , msglevel, +int lines_inline = +check_inline_file_via_buf (&multiline, p, &options->gc); +add_option (options, p, lines_inline, prefix, line_num,0 , msglevel, permission_mask, option_types_found, es); + line_num += lines_inline; }CLEAR (p); }
The rest looks good and I "nicely" get errors reporting the correct lines!
Signature · 1 line
Antonio Quartulli
Patch has been applied to the master, release/2.5 and release/2.4 branch (bugfix).
Release/2.4 has the same code, but is missing one recent round of uncrustification, many conflicts due to different context and one due to the new inline-file-handling.
Theoretically it could also be applied to release/2.3, but since it's not a really *important* bugfix (= nobody but me noticed in 10+ years), I've withstood the temptation.
I have adjusted the line break as instructed (creating two lines with 86 chars).
commit a686f7e29af012783371f401f394ac1e62e5b75f (master) commit 97af8b3101af6a1f8fef778d2c9e0e5f13d2d440 (release/2.5) commit 8ce5a319fd7a9b13ed6fee92fbda567c24f832c5 (release/2.4)
Author: Gert Doering Date: Sun Dec 6 13:57:11 2020 +0100
Fix line number reporting on config file errors after <inline> segments
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg21334.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering