Opened 18 years ago
Last modified 21 months ago
#114 new enhancement
memory leaks in array are not detected
| Reported by: | kidkat | Owned by: | noone |
|---|---|---|---|
| Priority: | Normal | Milestone: | |
| Component: | Improve check | Version: | |
| Keywords: | memleak valueflow | Cc: | sigra, php-coderrr, amai |
Description (last modified by )
#include <stdlib.h>
int main(int argc,char *argv[])
{
char* ptrs[2];
for( int i = 0; i < sizeof(ptrs); ++i )
{
ptrs[i] = malloc(10);
}
return 0;
}
Checking c:\temp\cppcheck_tests\test49.c...
49/49 files checked 100% done
Change History (12)
comment:1 by , 17 years ago
| Cc: | added |
|---|
comment:2 by , 17 years ago
| Component: | New check → Improve check |
|---|
comment:3 by , 17 years ago
| Cc: | added |
|---|
comment:4 by , 17 years ago
comment:5 by , 15 years ago
It seems that the latest cppcheck can report this kind of memory leak
[114.cpp:9]: (error) Buffer access out-of-bounds: ptrs
So , this ticket should be closed.
comment:6 by , 15 years ago
yes the example code is not good. the loop should say "i < 2" instead of "i < sizeof(ptrs)".
Cppcheck should still detect the memleak.
comment:7 by , 15 years ago
I'm sorry. The code shown a memory leak in the loop and cppcheck does not detect it.
Some enhancements have to be made to close this ticket.
comment:8 by , 14 years ago
| Cc: | added |
|---|---|
| Keywords: | memory leak added |
| Priority: | → Normal |
comment:9 by , 9 years ago
| Keywords: | memleak valueflow added; memory leak removed |
|---|
No memleak warining is shown for
#include <cstdlib> void f() { char* ptrs[2]; for( int i = 0; i < sizeof(ptrs); ++i ) { ptrs[i] = (char*)malloc(10); } }
$ cppcheck --enable=all --inconclusive --debug 114.cpp Checking 114.cpp ... [114.cpp:4]: (debug) valueflow.cpp:1253:valueFlowBeforeCondition bailout: variable i used in loop ##file 114.cpp 2: void f ( ) { 3: char * ptrs@1 [ 2 ] ; 4: for ( int i@2 = 0 ; i@2 < 16 ; ++ i@2 ) { 5: ptrs@1 [ i@2 ] = malloc ( 10 ) ; 6: } 7: } ##Value flow Line 3 2 always 2 Line 4 0 always 0 16 always 16 Line 5 i possible {0,15} 10 always 10 [114.cpp:5]: (error) Array 'ptrs[2]' accessed at index 15, which is out of bounds. [114.cpp:2]: (style) The function 'f' is never used. (information) Cppcheck cannot find all the include files (use --check-config for details)
comment:11 by , 8 years ago
If the loop is fixed (sorry about that) clang-tidy is able to detect this leak
1 warning generated.
/mnt/s/dev/example/main.c:12:12: warning: Potential memory leak [clang-analyzer-unix.Malloc]
return 0;
^
/mnt/s/dev/example/main.c:7:5: note: Loop condition is true. Entering loop body
for( int i = 0; i < sizeof(ptrs)/sizeof(ptrs[0]); ++i )
^
/mnt/s/dev/example/main.c:7:5: note: Loop condition is true. Entering loop body
/mnt/s/dev/example/main.c:9:19: note: Memory is allocated
ptrs[i] = malloc(10);
^
/mnt/s/dev/example/main.c:7:5: note: Loop condition is false. Execution continues on line 12
for( int i = 0; i < sizeof(ptrs)/sizeof(ptrs[0]); ++i )
^
/mnt/s/dev/example/main.c:12:12: note: Potential memory leak
return 0;
^
clang-tidy did not detect the loop going out of bounds though.
cppcheck not find this leak because ptrs declared as array -- with pointer all works as expected.
BTW, look like condition in for() loop is wrong: i < sizeof(ptrs) should be replaced to i < (sizeof(ptrs)/sizeof(ptrs[0]))