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?
-
1Are you asking how to read your code to spot memory leaks? Or how to program against it?StoryTeller - Unslander Monica– StoryTeller - Unslander Monica2014-06-26 14:55:48 +00:00Commented Jun 26, 2014 at 14:55
-
3Could 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?Pascal Cuoq– Pascal Cuoq2014-06-26 15:04:28 +00:00Commented Jun 26, 2014 at 15:04
Add a comment
|
2 Answers
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.
Comments
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;
}