[antlr-interest] [C runtime] don't compare pointer for negative return values !

Holger Schurig hs4233 at mail.mn-solutions.de
Fri Aug 24 04:26:16 PDT 2007


In the examples there is a file examples/C/main.c which contains 
this code:

   input       = antlr3AsciiFileStreamNew(fName);

   // The input will be created successfully, providing that
   // there is enough memory and the file exists etc


That is utterly nonsense.

When I get something and cast this something to "unsigned", how 
can this new value (which is unsigned by the cast!) now ever be 
smaller than zero?

BTW, GCC 4.1.2 emits this as warning

Second: the return value for antlr3AsciiFileStreamNew() is a 
pointer. Comparing this for < 0 is nonsense again, because the 
c-runtime could as well give you back 0xD00C0020 as address for 
this pointer. This, seen as a signed value, would be smaller 
than zero. Yet the object has been successfully created.



So the whole interface idea isn't sane.  You can't really, in a 
platform independend way, encode return values into 
pointer-returning functions.

Suggestion:

All the xxxxxNew()-Functions that normally return a pointer 
should return NULL as error condition. You could have an 
antlr3errorid variable somewhere, where higher levels could 
refer to to find out why the xxxxxNew()-Function failed.

If you don't like a global variable, then change the calling of 
the xxxxxNew() functions to something like this:

    pANTLS_POINTER xxxxNew(ANTLR3_INT32 *errCode);

so the end-user can specify where to store the error condition 
code.


More information about the antlr-interest mailing list