davidgiven / ack

The Amsterdam Compiler Kit
http://tack.sf.net
Other
420 stars 59 forks source link

cpp.ansi crashes on stringifying non-existent argument #238

Closed GabrielRavier closed 2 years ago

GabrielRavier commented 2 years ago
#define str(x)  # x
str()

Trying to pre-process this results in this output:

"test.c", line 2: too few macro arguments
Segmentation fault (core dumped)

This seems to be caused by the code trying to stringify the non-existent argument to the macro. (Also, while I suppose crashing is a technically correct response to ill-formed code, it doesn't exactly seem very appropriate to have it ever happen in general)

tkchia commented 2 years ago

Hello @GabrielRavier, hello @davidgiven,

Apparently there is a null pointer dereference at line 709 of lang/cem/cpp.ansi/replace.c, which will be here:

   702      if (*ptr & FORMALP) {
   703          register int n = *ptr++ & 0177;
   704          register char *p;
   705  
   706          assert(n != 0);
   707          p = args->a_rawvec[n-1];
   708          add2repl(repl, '"');
   709          while (*p) {
   710              if (is_wsp(*p)) {
   711                  if (!space) {
   712                      space = 1;
   713                      add2repl(repl, ' ');
   714                  }
   715                  p++;
   716                  continue;
   717              }
   718              space = 0;
   ...              ...
   729          }

Thank you!