In the following example, cppcheck (1.9) reports a warning for possible invalid value, which seems justified, but cppcheck 2.5 and the current latest 2.6 dev (#737b619) do not produce this warning.
Is this a true regression? I'm not sure which version changed this behaviour, but it seems a shame to no longer be told that I might be being dumb in this particular flavour.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've checked with the repo tag for 2.4.1, and that version does report this invalidFunctionArg. So if it's some sort of regression, it's after that point.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For the above example, cppcheck 1.90 detects and reports the data_len >0 vs data_len -2 problem, but anything after 1.90 fails to find it if any simple condition is added between the data_len > 0 and its use.
Without the second condition, the problem is detected still 2.4.1 but missed in 2.5.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I agree this is unfortunate. I am not sure how to fix this.
there has been some fixes for unsigned value. see i.e. https://trac.cppcheck.net/ticket/10298 .. if data_len is 1 then data_len - 2 will be a large positive value and that is within the configured bounds.
it is very suspicious to have an overflow in that calculation. Cppcheck must not warn about unsigned overflows in general because it is defined behavior there will be a wrap-around and this is often used intentionally.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've done a little more testing, and found that for repo tag 2.5 of cppcheck, if the length parameter given to memcpy is signed, then it catches the problem. However, if the length parameter is size_t (an unsigned type), the potential problem is missed.
In the following example, cppcheck (1.9) reports a warning for possible invalid value, which seems justified, but cppcheck 2.5 and the current latest 2.6 dev (#737b619) do not produce this warning.
Running with cppcheck using enable=all:
Result with cppcheck version 1.90:
And with cppcheck version 2.5:
Is this a true regression? I'm not sure which version changed this behaviour, but it seems a shame to no longer be told that I might be being dumb in this particular flavour.
I've checked with the repo tag for 2.4.1, and that version does report this invalidFunctionArg. So if it's some sort of regression, it's after that point.
Update:
I have a better minimal example:
For the above example, cppcheck 1.90 detects and reports the data_len >0 vs data_len -2 problem, but anything after 1.90 fails to find it if any simple condition is added between the data_len > 0 and its use.
Without the second condition, the problem is detected still 2.4.1 but missed in 2.5.
I agree this is unfortunate. I am not sure how to fix this.
there has been some fixes for unsigned value. see i.e. https://trac.cppcheck.net/ticket/10298 .. if
data_len
is 1 thendata_len - 2
will be a large positive value and that is within the configured bounds.it is very suspicious to have an overflow in that calculation. Cppcheck must not warn about unsigned overflows in general because it is defined behavior there will be a wrap-around and this is often used intentionally.
I created ticket https://trac.cppcheck.net/ticket/10384
Thanks for creating the ticket Daniel.
I've done a little more testing, and found that for repo tag 2.5 of cppcheck, if the length parameter given to memcpy is signed, then it catches the problem. However, if the length parameter is size_t (an unsigned type), the potential problem is missed.
1a) Catches possible negative argument:
1b) Misses suspicious overflow in unsigned type size_t
2a) Misses negative argument, hidden by simple if-condition
2b) Proper behaviour - sees negative argument if condition is combined
I think there may be two things of note here.
1) suspicious overflow
2) separate condition hides potential invalid argument