From: Jakub Jelinek Date: Wed, 9 Apr 2025 10:27:38 +0000 (+0200) Subject: libcpp: Fix error recovery after use of __VA_ARGS__ as macro argument [PR118674] X-Git-Tag: basepoints/gcc-16~222 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6e77a83ffbe4253c306b5b3750cf4ee38e5ce071;p=thirdparty%2Fgcc.git libcpp: Fix error recovery after use of __VA_ARGS__ as macro argument [PR118674] The following testcase ICEs after emitting one pedwarn (about using __VA_ARGS__ in a place where it shouldn't be used) and one error. The error is emitted by _cpp_save_parameter where it sees the node has been used already earlier. But unlike the other _cpp_save_parameter caller which does goto out; if it returns false, this call with explicit __VA_ARGS__ doesn't and if it increments number of parameters etc. after the error, we then try to unsave it twice. The following patch fixes it by doing the goto out in that case too, the macro will then not be considered as variable arguments macro, but for error recovery I think that is fine. The other option would be before the other _cpp_save_parameter caller check if the node is pfile->spec_nodes.n__VA_ARGS__ and in that case also error and goto out, but that seems more expensive than this for the common case that the macro definition is correct. 2025-04-09 Jakub Jelinek PR preprocessor/118674 * macro.cc (parse_params) : If _cpp_save_parameter failed for __VA_ARGS__, goto out. * gcc.dg/cpp/pr118674.c: New test. --- diff --git a/gcc/testsuite/gcc.dg/cpp/pr118674.c b/gcc/testsuite/gcc.dg/cpp/pr118674.c new file mode 100644 index 00000000000..00ea438eeb2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr118674.c @@ -0,0 +1,5 @@ +/* PR preprocessor/118674 */ +/* { dg-do preprocess } */ +#define M(__VA_ARGS__, ...) +/* { dg-error "'__VA_ARGS__' can only appear in the expansion of a C99 variadic macro" "" { target *-*-* } .-1 } */ +/* { dg-error "duplicate macro parameter '__VA_ARGS__'" "" { target *-*-* } .-2 } */ diff --git a/libcpp/macro.cc b/libcpp/macro.cc index aa430e46640..be257103471 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -3610,9 +3610,10 @@ parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *variadic_ptr) if (!prev_ident) { /* An ISO bare ellipsis. */ - _cpp_save_parameter (pfile, nparms, - pfile->spec_nodes.n__VA_ARGS__, - pfile->spec_nodes.n__VA_ARGS__); + if (!_cpp_save_parameter (pfile, nparms, + pfile->spec_nodes.n__VA_ARGS__, + pfile->spec_nodes.n__VA_ARGS__)) + goto out; nparms++; pfile->state.va_args_ok = 1; if (! CPP_OPTION (pfile, c99)