M09Y
M09Y

Reputation: 15

How to detect memory leak in huge C code by static analysis(without using any tool)

How to detect memory leak in third party C code by static analysis(without using any tool). Like how do we verify that the allocated memory has been freed without using any tools?

Upvotes: 0

Views: 631

Answers (2)

LombaX
LombaX

Reputation: 17364

Talking in absolute terms, "you can't".

How can you spot a leak in this code (it has no sense, it's only to make you understand). If the users pass 1 as the command line parameters, the code will not leak. However, if he pass 2...

int main(int argc, const char * argv[]) {
    //insert code here...

    int numberOfLoops = atoi(argv[1]);

    int i = 0;
    void *ptr;
    for (i = 0; i <= numberOfLoops; i++) {
        ptr = malloc(sizeof(int));
        printf("loop\n");
    }

    free(ptr);

    return 0;
}

Upvotes: 2

unwind
unwind

Reputation: 399793

If you're not going to be using any tool, then of course all you can do is read the code, and think about how it executes.

  • Are there any corner cases where the exucution splits into rarely-taken paths, that might fail to free resources?
  • How about the I/O, can that fail and cause unexpected paths to be taken?

Not using any tools for this is a very strange restriction, of course.

Upvotes: 3

Related Questions