To me it looks like this should be fixed in Cppcheck. I guess you can not do much to get Cppcheck to report this issue.
Here is the output for the first example where the message is missing with some debug output:
In both cases Cppcheck knows that InputReady could be "Uninit", but it is only reported in one case.
Do you have a trac account to report it?
I have not found a ticket that reports this issue already, but there are some tickets related to uninitvar false negatives. I have not read all of them, there could be related tickets: https://trac.cppcheck.net/search?q=false+negative+uninitvar
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hmm... it's not entirely conclusive if your code is unsafe or safe. it would be nice to see what the possible values of d is. If d is always 10 or 13 then there is no bug.
I have no idea how much noise it will produce to warn about code like this, I can only guess. I am just saying that we need to be careful about this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, this topic also covers another false negative of missing warning on uninitialized var. Therefore I decided to add it there. Please, test the following piece of code:
classElem{public:Elem();~Elem();private:inti;}Elem::Elem()//: i(0) // Initialization commented{// Using 'this' in a constructor suppresses warnings about missing initialization of member variables!//pList.reset(new pList( this ));}
Checking C:\Users\x\Desktop\reportToCPPCheck\member-var-not-init-missing-warning.cpp ...
[C:\Users\x\Desktop\reportToCPPCheck\member-var-not-init-missing-warning.cpp:9]: (warning) Member variable 'Elem::i' is not initialized in the constructor.
What is wrong:
There is no warning about missing initialization after uncommenting line with keyword this what I suppose is wrong.
Last edit: Set 2018-11-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Let me explain why there is no warning when "this" is used. I believe it is because that in general we need to determine what exactly this code is doing:
pList.reset(new pList( this ));
If it indirectly writes i then there is no error.
In your code example the variable i can't be indirectly written through the public interface so I guess a warning is fine. But in general; if there is a setter or something then a warning should not be written unless we determine that the setter is not used.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was hoping that someone with more knowledge of the internals of Cppcheck writes something about this issue. For me it is not entirely clear if there is really no way that i could be initialized by pList().
Since pList is not known it might be hard for Cppcheck to tell what it does with the this pointer. On the other hand i is a private variable and there are no friend classes, so i guess it should be safe to assume that i is not initialized anywhere.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for this discussion. My simplified example is based on a real code in which a developer really forgot to initialize a private variable and there was no setter. This hidden mystake caused serious problems! That is why I suppose it is rather important to think about the missing warning (and implement the warning somehow).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey guys.
Have found an code where cppcheck (current version 1.84) does'nt recognize an uninitialized variable.
Here is the code where it fails, InputReady could be uninitialized!
Result (uninitvar is overseen):
When i'll modify the code a little bit and put the crlf check into one if, it does'nt fail:
The result is correct, uninit var is recognized by cppcheck:
Already tried --inconclusive cli option, which does'nt make a difference.
Any hints?
Is there a option i am missing?
Thanks a lot
Last edit: popy2007 2018-09-11
To me it looks like this should be fixed in Cppcheck. I guess you can not do much to get Cppcheck to report this issue.
Here is the output for the first example where the message is missing with some debug output:
Output for the second example where the message is shown with debug output:
In both cases Cppcheck knows that InputReady could be "Uninit", but it is only reported in one case.
Do you have a trac account to report it?
I have not found a ticket that reports this issue already, but there are some tickets related to uninitvar false negatives. I have not read all of them, there could be related tickets:
https://trac.cppcheck.net/search?q=false+negative+uninitvar
thx, for the debug output.
Sadly i have no trac account to report it.
Can you please report it and link this discussion?
Thanks a lot
I have created the ticket 8755 for this issue.
Thanks for your report.
thx a lot
Thanks!
hmm... it's not entirely conclusive if your code is unsafe or safe. it would be nice to see what the possible values of d is. If d is always 10 or 13 then there is no bug.
I have no idea how much noise it will produce to warn about code like this, I can only guess. I am just saying that we need to be careful about this.
I do believe we can safely warn here:
Does your real code also handle other values of d than 10 or 13?
d is a char received over serial interface, so yes, it could be 0-255.
The code does not handle other values.
cppcheck should handle it, shoud'nt it?
somehow we need to determine that it's an arbitrary value. We have some tickets about treating input values from files/etc as arbitrary..
It might be ok to assume that all input variables have arbitrary values if --inconclusive is used.
I do think this code looks dangerous and can see that a warning would be interesting.
Last edit: Daniel Marjamäki 2018-11-03
If somebody writes a patch it would be very interesting to try it out to see how noisy it is.
Hi, this topic also covers another false negative of missing warning on uninitialized var. Therefore I decided to add it there. Please, test the following piece of code:
Running:
"C:\Program Files\Cppcheck\cppcheck.exe" --enable=all C:\Users\x\Desktop\reportToCPPCheck\member-var-not-init-missing-warning.cpp
reports:
What is wrong:
There is no warning about missing initialization after uncommenting line with keyword this what I suppose is wrong.
Last edit: Set 2018-11-21
Reminder, there is no reposnse for almost three weeks...
This issue persists in the newest version of CPPCheck, 1.86.
ok sorry that we did not reply.
Let me explain why there is no warning when "this" is used. I believe it is because that in general we need to determine what exactly this code is doing:
If it indirectly writes i then there is no error.
In your code example the variable i can't be indirectly written through the public interface so I guess a warning is fine. But in general; if there is a setter or something then a warning should not be written unless we determine that the setter is not used.
Example code:
We can't warn here unless we determine that
pList
constructor does not callsetI
.I was hoping that someone with more knowledge of the internals of Cppcheck writes something about this issue. For me it is not entirely clear if there is really no way that
i
could be initialized bypList()
.Since
pList
is not known it might be hard for Cppcheck to tell what it does with thethis
pointer. On the other handi
is a private variable and there are nofriend
classes, so i guess it should be safe to assume thati
is not initialized anywhere.Thanks for this discussion. My simplified example is based on a real code in which a developer really forgot to initialize a private variable and there was no setter. This hidden mystake caused serious problems! That is why I suppose it is rather important to think about the missing warning (and implement the warning somehow).
The initial issue is solved in cppcheck v1.89!
It finds the issue and warns about it.
Thanks a lot for fixing this.