Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is VS2013 complaining about "using uninitialized memory"?

I have a code that goes like this:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *a;
    char *b;
    int c;
} my_type;

void free_my_type(my_type *p) {
    if (p) {
        if (p->a) free(p->a);  // line 12
        if (p->b) free(p->b);  // line 13
        free(p);
    }
}

int main(void) {
    my_type *p = malloc(sizeof(*p));

    p->a = malloc(10);
    p->b = malloc(10);
    p->c = 10;

    free_my_type(p);

    return 0;
}

VS's Code Analysis is complaining that I am:

"C6001 Using uninitialized memory '*p'"

        '*p' is not initialized                             12
        Skip this branch, (assume 'p->b' is false)          13
        '*p' is used, but may not have been initialized     13

I mean, it's a pointer and I'm checking to see if it is NULL. How will I ever know if *p is initialized?

Oddly enough, if there's only 1 other pointer inside the struct -- only char *a, for example -- the warning doesn't trigger. It also doesn't show up if I do free(p->b) before free(p->a) (swap lines 12 and 13).

like image 717
sthiago Avatar asked Nov 04 '15 03:11

sthiago


1 Answers

It seems to be a problem with the analyzer tool of visual studio 2013

as explained here:

https://randomascii.wordpress.com/2011/07/25/analyze-for-visual-studiothe-ugly-part-1/

https://randomascii.wordpress.com/2011/07/29/analyze-for-visual-studiothe-ugly-part-2/

https://randomascii.wordpress.com/2011/08/06/analyze-for-visual-studiothe-ugly-part-3-false-positives/

https://randomascii.wordpress.com/2011/08/20/analyze-for-visual-studiothe-ugly-part-4-false-negatives/

https://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/

as an update in the part 5, we can read this:

Update: Luckily VC++ 2013 has solved many of these issues, but the problems with __analysis_assume remain.

So even if they solved many of theses warning issues with newest visual studio versions, still some bugs occurs in the analyzer tool.

Test with VS2015 Enterprise: gives the same problem

enter image description here

like image 144
HDJEMAI Avatar answered Oct 13 '22 19:10

HDJEMAI