[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reporting malloc failure in actions
From: |
Christian Schoenebeck |
Subject: |
Re: Reporting malloc failure in actions |
Date: |
Thu, 31 Dec 2020 14:43:51 +0100 |
On Mittwoch, 30. Dezember 2020 19:50:05 CET Joe Nelson wrote:
> > void* p = malloc(n);
> > if (!p) {
> >
> > fprintf(stderr, "Parser: malloc(%s) with size %zu failed.
> > Aborting.
\n",
> >
> > expr, n);
>
> Might be more flexible to call yyerror() than printing directly to
> stderr? Although the name of that function depends on api.prefix, and
> its arguments depend on %param and %parse-param, so I'm not sure how our
> general-purpose parser_alloc() would know what to do. Plus we'd have to
> build the error string ourselves, which probably requires a malloc()...
Yes, the signature of yyerror() depends on several Bison options. There is
also option %locations that you would need to take into account. It's possible
to handle these, but obviously more work for you.
> > abort();
>
> Rather than aborting, maybe we could do something like this
>
> #ifdef YYNOMEM
> /* newer bison version */
> YYNOMEM;
> #else
> /* older bison (undocumented feature) */
> goto yyexhaustedlab;
> #endif
You would be dealing with internals, but AFAICS that internal label already
exists for decades, so it'll be okay as pragmatic solution I guess.
> > }
> > memset(p, 0, n);
>
> What about using calloc() rather than the combination of malloc() and
> memset()?
Yes, calloc() would be an appropriate replacement.
Best regards,
Christian Schoenebeck