1

I am calling vsnprintf , as below If Vargs is NULL then vsnprintf coredump in strlen function,but same core work fine in other OS like linux , AIX ....

Is there any solution for this ? I can't avoid passing NULL into varags , I 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 is %s",FileName);
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
Syed Shamsheer
  • 193
  • 1
  • 2
  • 8
  • 4
    The argument corresponding to `%s` must not be a null pointer. You're violating the function's preconditions. – Angew is no longer proud of SO May 06 '13 at 09:23
  • Hi Angew , This code has no issue in linux , I think In solaris , thr is no check for NULL before calling strlen function. I think this is a bug in solaris vsnprintf , is thr any solution ? – Syed Shamsheer May 06 '13 at 09:25
  • 4
    Solaris does the right thing. The safety net that Linux and others give you is incorrect because it encourages unportable and unsafe behavior. – Art May 06 '13 at 09:26
  • HI, Now I can change example function but i can't change main function, is thr any way to check for NULL in example function. – Syed Shamsheer May 06 '13 at 09:32
  • I've just written an answer - fixing it in `example` is difficult. – Mats Petersson May 06 '13 at 09:34
  • So what would you like to get if the argument is a NULL pointer? If the `format` string is null, there is nothing you can really do, is there? Why not test this before calling `va_start` and return from the function early? – David Rodríguez - dribeas May 06 '13 at 12:32
  • 1
    Solaris 11 added checks to avoid segfaulting when users incorrectly pass NULL pointers to `*printf()` functions. For earlier Solaris releases you'd need to request a backport of that enhancement (Oracle bug 15493532) from Oracle support. – alanc May 07 '13 at 01:51

1 Answers1

1

There are several solutions, but none is entirely trivial. Unfortunately, what you are finding is that SOME systems have a "catch" for NULL pointers in the handling of "%s", which fixes up the problem you are seeing. This is not required by the C standard.

The most simple (in terms of complexity of implementation) is to simply check the string before passing it into example, e.g. :

char *FileName = NULL;
if (!FileName) FileName = "(null)");
ret = example(buff,100,"File Name is %s",FileName);

or

char *FileName = NULL;
ret = example(buff,100,"File Name is %s",FileName?FileName:"(null)");

But if example is called a gazillion times in the code, it may be simpler to search for "%s" and check at that point if the value is NULL, and replace it. This gets quite messy, because you pretty much have to implement a full "%[flags][width][type]" parser for the printf formatting, and walk over the arguments. But there is a further problem here: You can't modify the va_list (it MAY work, but it's undefined behaviour), so you probably will end up writing your own vsnprintf function.

So, I would try to fix it at the source before going down the route of implementing your own function.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227