Variables should be initialized before use - expand hotspot

Hi all,

According to TOPIC: Uninitialized value used but not pointed out under C code.

This example did use an uninitialized variable, but due to the limitation of SonarQube, this issue becomes a hidden leak.

examples:

in test.h

struct str{int a;};
void func(struct str *);

int test.c:

include "test.h"
void func(struct str *s)
{
    if (s->a == x)
        DOSOMETHING
}

in main1.c

#include "test.h"
int main(void)
{
    struct str s;
    func(&s);
}

in main2.c

#include "test.h"
int main(void)
{
    struct str s = {0};
    func(&s);
}

In main1.c, we did not give s any initialized value, so in func() the condition sentence will do something un-predictable.
And main2.c should be the safe way to use the function.

I guess this could be seen as a Security Hotspot issue?

Hi @deron.chen,

My apologies for the long delay. Somehow, your post fell through the cracks.

Indeed, I think it might make a good hotspot rule.

It will inevitably produce a significant amount of false positives for code that uses a function to initialize the value, given:

test.c

#include "test.h"
void func(struct str *s)
{
  s->a = 42;
}

it would still report an issue in main1.c, since it does not see test.c during the analysis of main1.c and does not know that the struct is initialized in the function.

However, it might provide a good value as a hotspot rule, indeed. I have created a ticket to specify such a rule.
Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.