0

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?

2
  • 1
    Are you asking how to read your code to spot memory leaks? Or how to program against it? Commented Jun 26, 2014 at 14:55
  • 3
    Could you clarify the notion of “by static analysis(without using any tool)”? A static analyzer is a tool. Is your intention to do static analysis without using any static analyzer? Commented Jun 26, 2014 at 15:04

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.