Menu

misleading warning [pointerSize]

2023-06-07
2023-06-20
  • Yannick HOUDOT

    Yannick HOUDOT - 2023-06-07

    Hi,

    I don't understand why the warning is about the size of pointer 'p' ?!?

    Best regards.

    === test.c ===
    void * func(size_t n)
    {
    char *p;

    if (!(p = malloc( n * sizeof(void*) )))
    {
    }
    return p;
    

    }

    void main() { free( func(16 ) ) ; }
    === test.c == end

    $ /opt/cppcheck-2.10/cppcheck --enable=all test.c
    Checking test.c ...
    test.c:5:11: warning: Size of pointer 'p' used instead of size of its data. [pointerSize]
    if (!(p = malloc( n +sizeof(void*) + sizeof(size_t))))
    ^
    $

     
  • Andrew C Aitchison

    Do you want to allocate space for n pointers or n chars ?
    You probably want either
    void *p;
    or
    sizeof(char)

     
  • Yannick HOUDOT

    Yannick HOUDOT - 2023-06-08

    ok, so if I understand correctly, this test checks for a match between the pointer type and the types in the memory size calculation...

    ok, but I stand by the message being misleading when it talks about the size of 'p' instead of a none match between types ...

    Thanks for the hint !
    Best.

     
    • Daniel Marjamäki

      Yes.. hmm the message is confusing. Thanks.

      I have created this ticket:
      https://trac.cppcheck.net/ticket/11754

       
  • Daniel Marjamäki

    I wonder if you think this is a false positive. I mean was do you think your own code was better or was it better to tweak the code.

    If I understand it correctly you wanted to allocate this amount of bytes:

    p = malloc( n + sizeof(void*) + sizeof(size_t))
    

    Maybe we should implement some exception. If sizeof(void*) is used it's likely the user wanted to get "size of pointer".. there is no big danger there is an extra "*" at least.

     
  • Yannick HOUDOT

    Yannick HOUDOT - 2023-06-08

    This code looks like an allocation for a "command bloc", it's like a packed structure.
    there's no alignement involved, so it use a char* pointer to fill it up.
    I used them all day long.

    I corrected the code by using a temporary "void*" pointer instead, no more warning.

     
  • Daniel Marjamäki

    I corrected the code by using a temporary "void*" pointer instead, no more warning.

    I don't like that such correction is used to silence cppcheck. So I think this is a false positive.

     
  • Yannick HOUDOT

    Yannick HOUDOT - 2023-06-20

    late reply, but yes, I do think myself that it's a false positive.
    the main issue is really the warning sentence... not clear enough, or misleading.
    Thanks for the ticket opening.

     

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.