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 amai)

#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 sigra, 17 years ago

Cc: sigra added

comment:2 by aggro80, 17 years ago

Component: New checkImprove check

comment:3 by php-coderrr, 17 years ago

Cc: php-coderrr added

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]))

comment:4 by aggro80, 17 years ago

php-coderrr has a good point in there. Tickets #338 and #339 created to make cppcheck to detect that problem. Note that those are unrelated to this memory leak problem (except perhaps the sizeof ticket, which might help with a lot of things).

comment:5 by seb777, 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 hyd_danmar, 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 seb777, 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 amai, 14 years ago

Cc: amai added
Keywords: memory leak added
Priority: Normal

comment:9 by orbitcowboy, 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:10 by amai, 8 years ago

Description: modified (diff)

#8510 has been closed as a duplicate.

comment:11 by kidkat, 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.

comment:12 by kidkat, 21 months ago

Related/duplicate: #2565

Note: See TracTickets for help on using tickets.