pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
fatal flex error in guc-file.l kills the postmaster
6+ messages / 3 participants
[nested] [flat]

* fatal flex error in guc-file.l kills the postmaster
@ 2011-12-16 17:22 Noah Misch <noah@leadboat.com>
  2011-12-18 04:04 ` Re: fatal flex error in guc-file.l kills the postmaster Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Noah Misch @ 2011-12-16 17:22 UTC (permalink / raw)
  To: pgsql-bugs

While testing the configuration include directory patch, I decided to try
the regular postgresql.conf "include" on a directory:

  include 'base'

Reloading the configuration then rather rudely kills the postmaster:

  FATAL:  input in flex scanner failed

Setting aside whether we should offer a better diagnostic when a user points
"include" at a directory, the yy_fatal_error hack that made sense for scan.l
doesn't cut it for guc-file.l.  Like other guc-file.l errors, we need to
choose the elevel based on which process is running the code.  ERROR is never
the right choice.  We should instead stash the message, longjmp out of the
flex parser, and proceed to handle the error mostly like a regular syntax
error.  See attached small patch.

It seems plainly hackish to mutate the body of yy_fatal_error() by #define'ing
fprintf().  Why didn't we instead #define YY_FATAL_ERROR and provide our own
complete replacement?  (Both strategies are undocumented.)  I haven't changed
that decision in this patch, though.

On a related note, the out-of-memory handling during config file reload is
inconsistent.  guc-file.l uses palloc/pstrdup in various places.  An OOM at
those sites during a reload would kill the postmaster.  guc.c has functions
like guc_malloc that handle variable elevels.  However, various check_* and
assign_* functions, such as check_log_destination(), use pstrdup() and
guc_malloc(ERROR).  Failing there during a reload would also kill the
postmaster.  Is it worth doing better here?

Incidentally, the wal writer process no longer exits promptly on postmaster
death.  I didn't look into that further.

Thanks,
nm

Attachments:

guc-flex-fatal-v1.patch (text/plain, 5.1K) download *** a/src/backend/utils/misc/guc-file.l --- b/src/backend/utils/misc/guc-file.l *************** *** 20,28 **** #include "utils/guc.h" ! /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */ #undef fprintf ! #define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg))) enum { GUC_ID = 1, --- 20,41 ---- #include "utils/guc.h" ! /* ! * flex emits a yy_fatal_error() function that it calls in response to ! * critical errors like malloc failure, file I/O errors, and detection of ! * internal inconsistency. That function prints a message and calls exit(). ! * Mutate it to instead stash the message and jump out of the parser; see ! * GUC_safe_yylex() for the other half of the story. Assume all msg arguments ! * point to string constants; this holds for flex 2.5.31 (earliest we support) ! * and flex 2.5.35 (latest as of this writing). ! */ ! static const char *GUC_flex_fatal_errmsg; ! static sigjmp_buf GUC_flex_fatal_jmp; #undef fprintf ! #define fprintf(file, fmt, msg) \ ! 0; /* eat cast to void */ \ ! GUC_flex_fatal_errmsg = msg; \ ! siglongjmp(GUC_flex_fatal_jmp, 1) enum { GUC_ID = 1, *************** *** 32,39 **** enum { GUC_EQUALS = 5, GUC_UNQUOTED_STRING = 6, GUC_QUALIFIED_ID = 7, ! GUC_EOL = 99, ! GUC_ERROR = 100 }; static unsigned int ConfigFileLineno; --- 45,53 ---- GUC_EQUALS = 5, GUC_UNQUOTED_STRING = 6, GUC_QUALIFIED_ID = 7, ! GUC_EOL = 98, ! GUC_ERROR = 99, ! GUC_INTERNAL_ERROR = 100 }; static unsigned int ConfigFileLineno; *************** *** 437,442 **** ParseConfigFile(const char *config_file, const char *calling_file, bool strict, --- 451,469 ---- } /* + * Wrap the generated lexer so we can regain control after a fatal, internal + * flex error. On return of GUC_INTERNAL_ERROR, GUC_flex_fatal_errmsg will + * contain a pointer into static storage containing the error message. + */ + static int + GUC_safe_yylex(void) + { + if (sigsetjmp(GUC_flex_fatal_jmp, 1) != 0) + return GUC_INTERNAL_ERROR; + return GUC_yylex(); + } + + /* * Read and parse a single configuration file. This function recurses * to handle "include" directives. * *************** *** 478,484 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, errorcount = 0; /* This loop iterates once per logical line */ ! while ((token = yylex())) { char *opt_name = NULL; char *opt_value = NULL; --- 505,511 ---- errorcount = 0; /* This loop iterates once per logical line */ ! while ((token = GUC_safe_yylex())) { char *opt_name = NULL; char *opt_value = NULL; *************** *** 493,501 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, opt_name = pstrdup(yytext); /* next we have an optional equal sign; discard if present */ ! token = yylex(); if (token == GUC_EQUALS) ! token = yylex(); /* now we must have the option value */ if (token != GUC_ID && --- 520,528 ---- opt_name = pstrdup(yytext); /* next we have an optional equal sign; discard if present */ ! token = GUC_safe_yylex(); if (token == GUC_EQUALS) ! token = GUC_safe_yylex(); /* now we must have the option value */ if (token != GUC_ID && *************** *** 510,516 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, opt_value = pstrdup(yytext); /* now we'd like an end of line, or possibly EOF */ ! token = yylex(); if (token != GUC_EOL) { if (token != 0) --- 537,543 ---- opt_value = pstrdup(yytext); /* now we'd like an end of line, or possibly EOF */ ! token = GUC_safe_yylex(); if (token != GUC_EOL) { if (token != 0) *************** *** 583,589 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, pfree(opt_value); /* report the error */ ! if (token == GUC_EOL || token == 0) ereport(elevel, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error in file \"%s\" line %u, near end of line", --- 610,619 ---- pfree(opt_value); /* report the error */ ! if (token == GUC_INTERNAL_ERROR) ! elog(elevel, "%s at file \"%s\" line %u", ! GUC_flex_fatal_errmsg, config_file, ConfigFileLineno); ! else if (token == GUC_EOL || token == 0) ereport(elevel, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error in file \"%s\" line %u, near end of line", *************** *** 612,622 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, break; } ! /* resync to next end-of-line or EOF */ ! while (token != GUC_EOL && token != 0) ! token = yylex(); ! /* break out of loop on EOF */ ! if (token == 0) break; } --- 642,655 ---- break; } ! /* ! * Resync to next end-of-line or EOF. Abandon the file after a ! * flex-internal error, which may corrupt parser state. Hopefully, ! * it's not so corrupt that yy_delete_buffer() will explode... ! */ ! while (token != GUC_EOL && token != 0 && token != GUC_INTERNAL_ERROR) ! token = GUC_safe_yylex(); ! if (token != GUC_EOL) break; }
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: fatal flex error in guc-file.l kills the postmaster
  2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
@ 2011-12-18 04:04 ` Tom Lane <tgl@sss.pgh.pa.us>
  2011-12-18 16:53   ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2011-12-18 04:04 UTC (permalink / raw)
  To: Noah Misch <noah@leadboat.com>; +Cc: pgsql-bugs

Noah Misch <noah@leadboat.com> writes:
> Setting aside whether we should offer a better diagnostic when a user points
> "include" at a directory, the yy_fatal_error hack that made sense for scan.l
> doesn't cut it for guc-file.l.  Like other guc-file.l errors, we need to
> choose the elevel based on which process is running the code.  ERROR is never
> the right choice.  We should instead stash the message, longjmp out of the
> flex parser, and proceed to handle the error mostly like a regular syntax
> error.  See attached small patch.

Well, if you're going to criticize the original method as being hackish,
you really need to offer a cleaner substitute than this one ;-).  In
particular I'm not happy with adding a sigsetjmp() cycle for every
single token parsed.

> It seems plainly hackish to mutate the body of yy_fatal_error() by #define'ing
> fprintf().

I agree, but the flex boys haven't provided any nicer API ...
at least not unless we'd like to convert to C++.

> Why didn't we instead #define YY_FATAL_ERROR and provide our own
> complete replacement?

For one reason, yy_fatal_error would still be there and would draw an
"unused static function" warning.

> On a related note, the out-of-memory handling during config file reload is
> inconsistent.  guc-file.l uses palloc/pstrdup in various places.  An OOM at
> those sites during a reload would kill the postmaster.

If you've got OOM in the postmaster, you're dead anyway.  I feel no
compulsion to worry about this.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: fatal flex error in guc-file.l kills the postmaster
  2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2011-12-18 04:04 ` Re: fatal flex error in guc-file.l kills the postmaster Tom Lane <tgl@sss.pgh.pa.us>
@ 2011-12-18 16:53   ` Noah Misch <noah@leadboat.com>
  2012-01-17 01:54     ` Re: fatal flex error in guc-file.l kills the postmaster Robert Haas <robertmhaas@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Noah Misch @ 2011-12-18 16:53 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On Sat, Dec 17, 2011 at 11:04:43PM -0500, Tom Lane wrote:
> Noah Misch <noah@leadboat.com> writes:
> > Setting aside whether we should offer a better diagnostic when a user points
> > "include" at a directory, the yy_fatal_error hack that made sense for scan.l
> > doesn't cut it for guc-file.l.  Like other guc-file.l errors, we need to
> > choose the elevel based on which process is running the code.  ERROR is never
> > the right choice.  We should instead stash the message, longjmp out of the
> > flex parser, and proceed to handle the error mostly like a regular syntax
> > error.  See attached small patch.
> 
> Well, if you're going to criticize the original method as being hackish,
> you really need to offer a cleaner substitute than this one ;-).  In
> particular I'm not happy with adding a sigsetjmp() cycle for every
> single token parsed.

On the contrary, I want to make it even more of a hack to get better behavior!

Here's a version that calls sigsetjmp() once per file.  While postgresql.conf
scanning hardly seems like the place to be shaving cycles, this does catch
fatal errors in functions other than yylex(), notably yy_create_buffer().

> > On a related note, the out-of-memory handling during config file reload is
> > inconsistent.  guc-file.l uses palloc/pstrdup in various places.  An OOM at
> > those sites during a reload would kill the postmaster.
> 
> If you've got OOM in the postmaster, you're dead anyway.  I feel no
> compulsion to worry about this.

Works for me.

Thanks,
nm

Attachments:

guc-flex-fatal-v2.patch (text/plain, 3.2K) download *** a/src/backend/utils/misc/guc-file.l --- b/src/backend/utils/misc/guc-file.l *************** *** 20,28 **** #include "utils/guc.h" ! /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */ #undef fprintf ! #define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg))) enum { GUC_ID = 1, --- 20,40 ---- #include "utils/guc.h" ! /* ! * flex emits a yy_fatal_error() function that it calls in response to ! * critical errors like malloc failure, file I/O errors, and detection of ! * internal inconsistency. That function prints a message and calls exit(). ! * Mutate it to instead stash the message and jump out of the parser. Assume ! * all msg arguments point to string constants; this holds for flex 2.5.31 ! * (earliest we support) and flex 2.5.35 (latest as of this writing). ! */ ! static const char *GUC_flex_fatal_errmsg; ! static sigjmp_buf *GUC_flex_fatal_jmp; #undef fprintf ! #define fprintf(file, fmt, msg) \ ! 0; /* eat cast to void */ \ ! GUC_flex_fatal_errmsg = msg; \ ! siglongjmp(*GUC_flex_fatal_jmp, 1) enum { GUC_ID = 1, *************** *** 464,482 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, ConfigVariable **head_p, ConfigVariable **tail_p) { bool OK = true; ! YY_BUFFER_STATE lex_buffer; int errorcount; int token; /* * Parse */ - lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE); - yy_switch_to_buffer(lex_buffer); - ConfigFileLineno = 1; errorcount = 0; /* This loop iterates once per logical line */ while ((token = yylex())) { --- 476,511 ---- ConfigVariable **head_p, ConfigVariable **tail_p) { bool OK = true; ! volatile YY_BUFFER_STATE lex_buffer = NULL; int errorcount; int token; + sigjmp_buf flex_fatal_jmp; + + if (sigsetjmp(flex_fatal_jmp, 1) == 0) + GUC_flex_fatal_jmp = &flex_fatal_jmp; + else + { + /* + * Regain control after a fatal, internal flex error. It may have + * corrupted parser state. Consequently, abandon the file, but trust + * that the state remains sane enough for yy_delete_buffer(). + */ + elog(elevel, "%s at file \"%s\" line %u", + GUC_flex_fatal_errmsg, config_file, ConfigFileLineno); + + yy_delete_buffer(lex_buffer); + return false; + } /* * Parse */ ConfigFileLineno = 1; errorcount = 0; + lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE); + yy_switch_to_buffer(lex_buffer); + /* This loop iterates once per logical line */ while ((token = yylex())) { *************** *** 532,537 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, --- 561,567 ---- depth + 1, elevel, head_p, tail_p)) OK = false; + GUC_flex_fatal_jmp = &flex_fatal_jmp; yy_switch_to_buffer(lex_buffer); ConfigFileLineno = save_ConfigFileLineno; pfree(opt_name); *************** *** 549,554 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, --- 579,585 ---- depth + 1, elevel, head_p, tail_p)) OK = false; + GUC_flex_fatal_jmp = &flex_fatal_jmp; yy_switch_to_buffer(lex_buffer); ConfigFileLineno = save_ConfigFileLineno; pfree(opt_name);
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: fatal flex error in guc-file.l kills the postmaster
  2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2011-12-18 04:04 ` Re: fatal flex error in guc-file.l kills the postmaster Tom Lane <tgl@sss.pgh.pa.us>
  2011-12-18 16:53   ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
@ 2012-01-17 01:54     ` Robert Haas <robertmhaas@gmail.com>
  2012-01-17 18:23       ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Robert Haas @ 2012-01-17 01:54 UTC (permalink / raw)
  To: Noah Misch <noah@leadboat.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs

On Sun, Dec 18, 2011 at 11:53 AM, Noah Misch <noah@leadboat.com> wrote:
> Here's a version that calls sigsetjmp() once per file.  While postgresql.conf
> scanning hardly seems like the place to be shaving cycles, this does catch
> fatal errors in functions other than yylex(), notably yy_create_buffer().

This strikes me as more clever than necessary:

#define fprintf(file, fmt, msg) \
    0; /* eat cast to void */ \
    GUC_flex_fatal_errmsg = msg; \
    siglongjmp(*GUC_flex_fatal_jmp, 1)

Can't we just define a function jumpoutoftheparser() here and call
that function rather than doing that /* eat cast to void */ hack?
That would also involve fewer assumptions about the coding style
emitted by flex.  For example, if flex chose to do something like:

  if (bumpity) fprintf(__FILE__, "%s", "dinglewump");

...the proposed definition would be a disaster.  I doubt that inlining
is a material performance benefit here; siglongjmp() can't be all that
cheap, and the error path shouldn't be all that frequent.

Instead of making ParseConfigFp responsible for restoring
GUC_flex_fatal_jmp after calling anything that might recursively call
ParseConfigFp, I think it would make more sense to define it to stash
away the previous value and restore it on exit.  That way it wouldn't
need to know which of the things that it calls might in turn
recursively call it, which seems likely to reduce the chances of
present or future bugs.  A few comments about whichever way we go with
it seem like a good idea, too.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: fatal flex error in guc-file.l kills the postmaster
  2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2011-12-18 04:04 ` Re: fatal flex error in guc-file.l kills the postmaster Tom Lane <tgl@sss.pgh.pa.us>
  2011-12-18 16:53   ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2012-01-17 01:54     ` Re: fatal flex error in guc-file.l kills the postmaster Robert Haas <robertmhaas@gmail.com>
@ 2012-01-17 18:23       ` Noah Misch <noah@leadboat.com>
  2012-01-18 02:49         ` Re: fatal flex error in guc-file.l kills the postmaster Robert Haas <robertmhaas@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Noah Misch @ 2012-01-17 18:23 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs

On Mon, Jan 16, 2012 at 08:54:53PM -0500, Robert Haas wrote:
> On Sun, Dec 18, 2011 at 11:53 AM, Noah Misch <noah@leadboat.com> wrote:
> > Here's a version that calls sigsetjmp() once per file. ?While postgresql.conf
> > scanning hardly seems like the place to be shaving cycles, this does catch
> > fatal errors in functions other than yylex(), notably yy_create_buffer().
> 
> This strikes me as more clever than necessary:
> 
> #define fprintf(file, fmt, msg) \
>     0; /* eat cast to void */ \
>     GUC_flex_fatal_errmsg = msg; \
>     siglongjmp(*GUC_flex_fatal_jmp, 1)
> 
> Can't we just define a function jumpoutoftheparser() here and call
> that function rather than doing that /* eat cast to void */ hack?
> That would also involve fewer assumptions about the coding style
> emitted by flex.  For example, if flex chose to do something like:
> 
>   if (bumpity) fprintf(__FILE__, "%s", "dinglewump");
> 
> ...the proposed definition would be a disaster.  I doubt that inlining
> is a material performance benefit here; siglongjmp() can't be all that
> cheap, and the error path shouldn't be all that frequent.
> 
> Instead of making ParseConfigFp responsible for restoring
> GUC_flex_fatal_jmp after calling anything that might recursively call
> ParseConfigFp, I think it would make more sense to define it to stash
> away the previous value and restore it on exit.  That way it wouldn't
> need to know which of the things that it calls might in turn
> recursively call it, which seems likely to reduce the chances of
> present or future bugs.  A few comments about whichever way we go with
> it seem like a good idea, too.

Agreed on all points.  I also changed the save/restore of ConfigFileLineno to
work the same way.  New version attached.

Thanks,
nm

Attachments:

guc-flex-fatal-v3.patch (text/plain, 4.9K) download *** a/src/backend/utils/misc/guc-file.l --- b/src/backend/utils/misc/guc-file.l *************** *** 20,28 **** #include "utils/guc.h" ! /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */ #undef fprintf ! #define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg))) enum { GUC_ID = 1, --- 20,33 ---- #include "utils/guc.h" ! /* ! * flex emits a yy_fatal_error() function that it calls in response to ! * critical errors like malloc failure, file I/O errors, and detection of ! * internal inconsistency. That function prints a message and calls exit(). ! * Mutate it to instead call our handler, which jumps out of the parser. ! */ #undef fprintf ! #define fprintf(file, fmt, msg) GUC_flex_fatal(msg) enum { GUC_ID = 1, *************** *** 37,46 **** enum { --- 42,54 ---- }; static unsigned int ConfigFileLineno; + static const char *GUC_flex_fatal_errmsg; + static sigjmp_buf *GUC_flex_fatal_jmp; /* flex fails to supply a prototype for yylex, so provide one */ int GUC_yylex(void); + static int GUC_flex_fatal(const char *msg); static char *GUC_scanstr(const char *s); %} *************** *** 437,442 **** ParseConfigFile(const char *config_file, const char *calling_file, bool strict, --- 445,466 ---- } /* + * Flex fatal errors bring us here. Stash the error message and jump back to + * ParseConfigFp(). Assume all msg arguments point to string constants; this + * holds for flex 2.5.31 (earliest we support) and flex 2.5.35 (latest as of + * this writing). Otherwise, we would need to copy the message. + * + * We return "int" since this takes the place of calls to fprintf(). + */ + static int + GUC_flex_fatal(const char *msg) + { + GUC_flex_fatal_errmsg = msg; + siglongjmp(*GUC_flex_fatal_jmp, 1); + return 0; /* keep compiler quiet */ + } + + /* * Read and parse a single configuration file. This function recurses * to handle "include" directives. * *************** *** 464,482 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, ConfigVariable **head_p, ConfigVariable **tail_p) { bool OK = true; ! YY_BUFFER_STATE lex_buffer; int errorcount; int token; /* * Parse */ - lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE); - yy_switch_to_buffer(lex_buffer); - ConfigFileLineno = 1; errorcount = 0; /* This loop iterates once per logical line */ while ((token = yylex())) { --- 488,525 ---- ConfigVariable **head_p, ConfigVariable **tail_p) { bool OK = true; ! unsigned int save_ConfigFileLineno = ConfigFileLineno; ! sigjmp_buf *save_GUC_flex_fatal_jmp = GUC_flex_fatal_jmp; ! sigjmp_buf flex_fatal_jmp; ! volatile YY_BUFFER_STATE lex_buffer = NULL; int errorcount; int token; + if (sigsetjmp(flex_fatal_jmp, 1) == 0) + GUC_flex_fatal_jmp = &flex_fatal_jmp; + else + { + /* + * Regain control after a fatal, internal flex error. It may have + * corrupted parser state. Consequently, abandon the file, but trust + * that the state remains sane enough for yy_delete_buffer(). + */ + elog(elevel, "%s at file \"%s\" line %u", + GUC_flex_fatal_errmsg, config_file, ConfigFileLineno); + + OK = false; + goto cleanup; + } + /* * Parse */ ConfigFileLineno = 1; errorcount = 0; + lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE); + yy_switch_to_buffer(lex_buffer); + /* This loop iterates once per logical line */ while ((token = yylex())) { *************** *** 526,539 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, * An include_if_exists directive isn't a variable and should be * processed immediately. */ - unsigned int save_ConfigFileLineno = ConfigFileLineno; - if (!ParseConfigFile(opt_value, config_file, false, depth + 1, elevel, head_p, tail_p)) OK = false; yy_switch_to_buffer(lex_buffer); - ConfigFileLineno = save_ConfigFileLineno; pfree(opt_name); pfree(opt_value); } --- 569,579 ---- *************** *** 543,556 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, * An include directive isn't a variable and should be processed * immediately. */ - unsigned int save_ConfigFileLineno = ConfigFileLineno; - if (!ParseConfigFile(opt_value, config_file, true, depth + 1, elevel, head_p, tail_p)) OK = false; yy_switch_to_buffer(lex_buffer); - ConfigFileLineno = save_ConfigFileLineno; pfree(opt_name); pfree(opt_value); } --- 583,593 ---- *************** *** 620,626 **** ParseConfigFp(FILE *fp, const char *config_file, int depth, int elevel, --- 657,667 ---- break; } + cleanup: yy_delete_buffer(lex_buffer); + /* Each recursion level must save and restore these static variables. */ + ConfigFileLineno = save_ConfigFileLineno; + GUC_flex_fatal_jmp = save_GUC_flex_fatal_jmp; return OK; }
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: fatal flex error in guc-file.l kills the postmaster
  2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2011-12-18 04:04 ` Re: fatal flex error in guc-file.l kills the postmaster Tom Lane <tgl@sss.pgh.pa.us>
  2011-12-18 16:53   ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
  2012-01-17 01:54     ` Re: fatal flex error in guc-file.l kills the postmaster Robert Haas <robertmhaas@gmail.com>
  2012-01-17 18:23       ` Re: fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
@ 2012-01-18 02:49         ` Robert Haas <robertmhaas@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Robert Haas @ 2012-01-18 02:49 UTC (permalink / raw)
  To: Noah Misch <noah@leadboat.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs

On Tue, Jan 17, 2012 at 1:23 PM, Noah Misch <noah@leadboat.com> wrote:
> On Mon, Jan 16, 2012 at 08:54:53PM -0500, Robert Haas wrote:
>> On Sun, Dec 18, 2011 at 11:53 AM, Noah Misch <noah@leadboat.com> wrote:
>> > Here's a version that calls sigsetjmp() once per file. ?While postgresql.conf
>> > scanning hardly seems like the place to be shaving cycles, this does catch
>> > fatal errors in functions other than yylex(), notably yy_create_buffer().
>>
>> This strikes me as more clever than necessary:
>>
>> #define fprintf(file, fmt, msg) \
>>     0; /* eat cast to void */ \
>>     GUC_flex_fatal_errmsg = msg; \
>>     siglongjmp(*GUC_flex_fatal_jmp, 1)
>>
>> Can't we just define a function jumpoutoftheparser() here and call
>> that function rather than doing that /* eat cast to void */ hack?
>> That would also involve fewer assumptions about the coding style
>> emitted by flex.  For example, if flex chose to do something like:
>>
>>   if (bumpity) fprintf(__FILE__, "%s", "dinglewump");
>>
>> ...the proposed definition would be a disaster.  I doubt that inlining
>> is a material performance benefit here; siglongjmp() can't be all that
>> cheap, and the error path shouldn't be all that frequent.
>>
>> Instead of making ParseConfigFp responsible for restoring
>> GUC_flex_fatal_jmp after calling anything that might recursively call
>> ParseConfigFp, I think it would make more sense to define it to stash
>> away the previous value and restore it on exit.  That way it wouldn't
>> need to know which of the things that it calls might in turn
>> recursively call it, which seems likely to reduce the chances of
>> present or future bugs.  A few comments about whichever way we go with
>> it seem like a good idea, too.
>
> Agreed on all points.  I also changed the save/restore of ConfigFileLineno to
> work the same way.  New version attached.

OK, committed.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2012-01-18 02:49 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-12-16 17:22 fatal flex error in guc-file.l kills the postmaster Noah Misch <noah@leadboat.com>
2011-12-18 04:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-12-18 16:53   ` Noah Misch <noah@leadboat.com>
2012-01-17 01:54     ` Robert Haas <robertmhaas@gmail.com>
2012-01-17 18:23       ` Noah Misch <noah@leadboat.com>
2012-01-18 02:49         ` Robert Haas <robertmhaas@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox