i calling vsnprintf , below if vargs null vsnprintf coredump in strlen function,but same core work fine in other os linux , aix ....
is there solution ? can't avoid passing null varags , want vsnprintf must not coredump ...
code:
int example(char * buff,size_t count,const char format[],...) { va_list vargs = null; va_start(vargs,format); ret = vsnprintf(buff,count,format,vargs); va_end(vargs); return ret; } main() { char buff[100] = {0}; char *filename = null; ret = example(buff,100,"file name %s",filename); }
there several solutions, none entirely trivial. unfortunately, finding systems have "catch" null pointers in handling of "%s", fixes problem seeing. not required c standard.
the simple (in terms of complexity of implementation) check string before passing example
, e.g. :
char *filename = null; if (!filename) filename = "(null)"); ret = example(buff,100,"file name %s",filename);
or
char *filename = null; ret = example(buff,100,"file name %s",filename?filename:"(null)");
but if example
called gazillion times in code, may simpler search "%s" , check @ point if value null, , replace it. gets quite messy, because pretty have implement full "%[flags][width][type]" parser printf formatting, , walk on arguments. there further problem here: can't modify va_list
(it may work, it's undefined behaviour), end writing own vsnprintf
function.
so, try fix @ source before going down route of implementing own function.
Comments
Post a Comment