Menu

vsprintf(NULL, format, paramList); is not NULL dereference but standard way how to get final string size.

2019-07-20
2019-11-10
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-07-20

    size = vsprintf(NULL, format, paramList);
    is not NULL dereference but standard way how to get final string size.

    #include <stdarg.h>
    #include <stdio.h>
    int string::printf(const char* format, ...)
    {
      va_list paramList;
      va_start(paramList, format);
     #ifdef _HAVE_VSNPRINTF
      size = vsnprintf(0, 0, format, paramList);
    #else
      size = vsprintf(NULL, format, paramList);
    #endif
    
      if(size)
        {
        resize(size);
    #ifdef _HAVE_VSNPRINTF
        size = vsnprintf(ch, maxlen, format, paramList);
    #else
        size = vsprintf(ch, format, paramList);
    #endif
        }
      else
        erase();
    
      va_end(paramList);
      return size;
    }
    
     

    Last edit: Jaroslav Fojtik 2019-11-10
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-11-10

    Look at here: http://libslack.org/manpages/snprintf.3.html

    If size is zero, nothing is written and str may be null.

     
  • Daniel Marjamäki

    I guess we need to update the configuration.. could you review it?

     
    • Jaroslav Fojtik

      Jaroslav Fojtik - 2019-11-10

      Where to find configuration? I did not develop cppcheck yet.

       
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-11-10

    I am using the latest one 1.89.

    There are two functions vsnprintf and vsprintf. May be that one of them is not defined same way and requires not NULL argument.

    Whole source code is attached.

     
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-11-10

    The error is still reported on the line:
    size = vsprintf(NULL, format, paramList);

    And the header file:

     

    Last edit: Jaroslav Fojtik 2019-11-10
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-11-10

    Please note when your system supports vsprintf only, it is much more neccessary to call with NULL first to check space to allocate.

    <function name="vsprintf,std::vsprintf">
    <returnvalue type="int">
    <noreturn>false</noreturn>
    <leak-ignore>
    <arg nr="1" direction="out">
    <not-null></not-null> <<<<<!!!!!!!
    </arg>
    <arg nr="2" direction="in">
    <not-null>
    <not-uninit>
    <formatstr>
    </formatstr></not-uninit></not-null></arg>
    <arg nr="3">
    </arg></leak-ignore></returnvalue></function>

     

    Last edit: Jaroslav Fojtik 2019-11-10
  • Daniel Marjamäki

    Do you want to send a github pull request? I would suggest that we replace <not-null/> with <not-uninit/>.

     
  • Jaroslav Fojtik

    Jaroslav Fojtik - 2019-11-10

    Pull request means to duplicate git repository. I can do this, but for this minor change it is not neccessary to have right to push backwards. You know better what to do with my report ;).

     
  • Daniel Marjamäki

    ok.. I fix it myself..

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.