$ /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))))
^
$
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
}
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))))
^
$
Do you want to allocate space for n pointers or n chars ?
You probably want either
void *p;
or
sizeof(char)
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.
Yes.. hmm the message is confusing. Thanks.
I have created this ticket:
https://trac.cppcheck.net/ticket/11754
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:
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.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.
I don't like that such correction is used to silence cppcheck. So I think this is a false positive.
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.