r21508 - gnucash/trunk/src/gnome - Protect strcmp from crashing on NULL values

Geert Janssens janssens-geert at telenet.be
Mon Oct 31 11:07:40 EDT 2011

On maandag 31 oktober 2011, Derek Atkins wrote:
> Geert Janssens <gjanssens at code.gnucash.org> writes:
> > -    if(strcmp(order, DEFAULT_SORT_ORDER) != 0)
> > +    if (order && (strcmp (order, DEFAULT_SORT_ORDER) != 0))
> Why not use safe_strcmp()?
> -derek

I could have, but in this case only using safe_strcmp would still yield the 
wrong result. Using safe_strcmp, if order were NULL, then the test would 
evaluate to TRUE (a NULL value is always not equal to a non-null value) and 
the "then" part would be executed. However, in this case if order is NULL, the 
"then" part has to be skipped.

And by the way, I don't see much use for safe_strcmp anymore in gnucash. I'd 
rather user g_strcmp0, which behaves exactly the same, but is part of glibc. 
Our code is heavily dependent on glibc anyway, so I prefer to use its 
convenience functions over maintaining our own.


More information about the gnucash-devel mailing list