One issue that I run into from time to time is a negative number going into a square root. The output of the square root of a negative number is typically a NaN. I was of thinking of trying to add a check to the bug hunting analysis. After taking a quick look at bughuntingchecks.cpp I thought I should check and see ifanyone thought the check was good idea or any advice for adding adding acheck to the bug hunting analysis would be greatly appreciated.
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sounds good to me! I assume you mean when using sqrt standard function.
There is configuration already in std.cfg that says that a negative input value is invalid.
There is bug hunting checking that check function calls based on configuration.. but it only handles integers right now it seems. Look in the function checkFunctionCall() below the comment // Check invalid function argument values...
Feel free to look into this.
Last edit: Daniel Marjamäki 2020-08-03
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was looking to use the cppcheck_low(x) and cppcheck_high(x) to test the bug hunting analysis on sqrt() function from the command line. I know I don't have the cppcheck_low setup right. Just using the example from the manual wouldn't compile and it didn't saying anything about it in the --bug-hunting
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:28: error: ISO C++ forbids declaration of ‘cppcheck_high’ with no type [-fpermissive]
typedef cppcheck_high(100) unsigned int percent_t;
How do I tell it about the types?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am confused. That example from the manual does not work well when I check it with cppcheck. I expected to get a warning from Cppcheck but I don't get it.
I get no error. Not sure what I do wrong. I have the feeling this did work when I wrote the manual. If it does not work then I feel that it's a bug that should be fixed!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ah thanks. The manual should be clarified. There are a few solutions.. they can for instance be defined out during compilation. If you for instance have this somewhere then gcc should be happy:
I am not certain that everybody wants to put these specific cppcheck annotations __cppcheck_low__ and __cppcheck_high__ in their code. Maybe it's more interesting to define various user macros that expands into __cppcheck_low__ and __cppcheck_high__ i.e.:
Yeah I can see most people wouldn't want to put tool specific macros in their code. At this point I'm just taking baby steps to figure out how it all works. It compiles now but I don't see comping about it in the bug hunting check. The only thing the bug hunting finds is
I was wondering if maybe there was an issue with the way I built cppcheck. Below is my cmake output. This is the first time I'm looking into using the bugging hunting option.
I was able to get it to build with z3 now. It now detects the range violations on integers. I guess I was wondering where would be the best place to look to get it to try and look into range violations on floats?
It seems that I get stuck with something very similar in PR2956. I'm trying to add "null" values for string and float literals. This is necessary to handle them inside the condition statements. Something like this:
It is currently marked here as TODO_ASSERT_EQUALS:
intfoo() {
inta=42;if(0.0) {
a=0;
}
returna==0;
}
According to debug output, there is no binary operator token in cppcheck AST:
##AST
[ifAlwaysFalse.cpp:2]
='signed int'|-a'signed int'
`-42'signed int'
[ifAlwaysFalse.cpp:3]
(|-if
`-0.0'double'
[ifAlwaysFalse.cpp:4]
='signed int'|-a'signed int'
`-0'signed int'
[ifAlwaysFalse.cpp:6]
return'signed int'
`-=='bool'|-a'signed int'
`-0'signed int'
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using z3 version 4.8.9 If I actually recompile my version of z3 with debug on it segfaults on the solve.add call.
Below is part of the stack trace
Checkinglittlest.cpp...Beforesolver.addProgramreceivedsignalSIGSEGV,Segmentationfault.0x00007ffff63c1530inast::hash(this=0x0)at/BuildTest/z3/src/ast/ast.h:501501unsignedhash()const{returnm_hash;}Missingseparatedebuginfos,use:debuginfo-installglibc-2.17-317.el7.x86_64libgcc-4.8.5-44.el7.x86_64libstdc++-4.8.5-44.el7.x86_64(gdb)where#0 0x00007ffff63c1530 in ast::hash (this=0x0) at /BuildTest/z3/src/ast/ast.h:501#1 0x00007ffff63f8098 in obj_ptr_hash<ast>::operator() (this=0x14530b8, a=0x0) at /BuildTest/z3/src/util/hash.h:168#2 0x00007ffff63f3c6e in chashtable<ast*, obj_ptr_hash<ast>, ast_eq_proc>::get_hash (this=0x14530b8, d=@0x7fffffff7d10: 0x0)at/BuildTest/z3/src/util/chashtable.h:77#3 0x00007ffff63eeffe in chashtable<ast*, obj_ptr_hash<ast>, ast_eq_proc>::contains (this=0x14530b8, d=@0x7fffffff7d10: 0x0)at/BuildTest/z3/src/util/chashtable.h:423#4 0x00007ffff642134e in ast_manager::contains (this=0x1452dc8, a=0x0) at /BuildTest/z3/src/ast/ast.h:1669#5 0x00007ffff76f6988 in api::context::save_ast_trail (this=0x13d6288, n=0x0) at /BuildTest/z3/src/api/api_context.cpp:222#6 0x00007ffff76f68b1 in api::context::mk_numeral_core (this=0x13d6288, n=..., s=0x1170368) at /BuildTest/z3/src/api/api_context.cpp:202#7 0x00007ffff7714fb6 in Z3_mk_int (c=0x13d6288, value=0, ty=0x1170368) at /BuildTest/z3/src/api/api_numeral.cpp:102#8 0x0000000000bec591 in z3::context::num_val (this=0x7fffffff7f30, n=0, s=...) at /BuildTest/opt/usr/local/include/z3++.h:3216#9 0x0000000000beb76b in z3::operator< (a=..., b=0) at /BuildTest/opt/usr/local/include/z3++.h:1662#10 0x0000000000bd7290 in ExprEngine::FloatRange::isLessThan (this=0x1165030, dataBase=0x7fffffffa2c0, value=0)at/BuildTest/cppcheck/lib/exprengine.cpp:1449#11 0x0000000000a30426 in checkFunctionCall (tok=0x1162a80, value=..., dataBase=0x7fffffffa2c0) at /BuildTest/cppcheck/lib/bughuntingchecks.cpp:592
The ASSERT it is tripping on is in /BuildTest/z3/src/api/api_context.cpp:222
One issue that I run into from time to time is a negative number going into a square root. The output of the square root of a negative number is typically a NaN. I was of thinking of trying to add a check to the bug hunting analysis. After taking a quick look at bughuntingchecks.cpp I thought I should check and see ifanyone thought the check was good idea or any advice for adding adding acheck to the bug hunting analysis would be greatly appreciated.
Thanks
Sounds good to me! I assume you mean when using
sqrt
standard function.There is configuration already in
std.cfg
that says that a negative input value is invalid.There is bug hunting checking that check function calls based on configuration.. but it only handles integers right now it seems. Look in the function
checkFunctionCall()
below the comment// Check invalid function argument values..
.Feel free to look into this.
Last edit: Daniel Marjamäki 2020-08-03
Looking at std.cfg is the line <valid>0.0:</valid> the line that says the input value can't be negative.
Last edit: john borland 2020-10-26
yes.
I was looking to use the cppcheck_low(x) and cppcheck_high(x) to test the bug hunting analysis on sqrt() function from the command line. I know I don't have the cppcheck_low setup right. Just using the example from the manual wouldn't compile and it didn't saying anything about it in the --bug-hunting
Example code
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:28: error: ISO C++ forbids declaration of ‘cppcheck_high’ with no type [-fpermissive]
typedef cppcheck_high(100) unsigned int percent_t;
How do I tell it about the types?
I am confused. That example from the manual does not work well when I check it with cppcheck. I expected to get a warning from Cppcheck but I don't get it.
With this code:
I get this error:
When I use the typedef :
I get no error. Not sure what I do wrong. I have the feeling this did work when I wrote the manual. If it does not work then I feel that it's a bug that should be fixed!
Ah thanks. The manual should be clarified. There are a few solutions.. they can for instance be defined out during compilation. If you for instance have this somewhere then gcc should be happy:
I am not certain that everybody wants to put these specific cppcheck annotations
__cppcheck_low__
and__cppcheck_high__
in their code. Maybe it's more interesting to define various user macros that expands into__cppcheck_low__
and__cppcheck_high__
i.e.:With that code you just need to manually ensure that
__cppcheck__
is defined during cppcheck analysis.Yeah I can see most people wouldn't want to put tool specific macros in their code. At this point I'm just taking baby steps to figure out how it all works. It compiles now but I don't see comping about it in the bug hunting check. The only thing the bug hunting finds is
Last edit: john borland 2020-10-26
I was wondering if maybe there was an issue with the way I built cppcheck. Below is my cmake output. This is the first time I'm looking into using the bugging hunting option.
I'm thinking my problem is when I build it
-- USE_Z3 = OFF
Sine Z3 is used in the bug hunting analysis?
Yes..
USE_Z3
should beON
.I would not use matchcompiler during development. I have the feeling that will complicate the debugging.
I was able to get it to build with z3 now. It now detects the range violations on integers. I guess I was wondering where would be the best place to look to get it to try and look into range violations on floats?
Below is the example I am toying with .
If you want to check
sqrt
I think this is a good start:We have code that will check integer values here:
https://github.com/danmar/cppcheck/blob/2a05bc565d/lib/bughuntingchecks.cpp#L576
We need to tweak that code so it works for floats also.
It seems the problem right now for my example code is that we get false positives.
In this code I do not want that "err" is set to true for the first function call: https://github.com/danmar/cppcheck/blob/2a05bc565d/lib/bughuntingchecks.cpp#L592
If you want to investigate why "err" will be true for the first function call feel free to do it.
Yeah I think starting with the false positives with the sqrt function where the best place to start.
I'm using cppcheck-2.3 and z3-4.8.9
It looks like z3: ast is not an expression is coming out of the catch statement in exprengine.cpp:1350
It looks like because it can't solve for what "$5" is it is throwing an exception out of the solver.add call. Any ideas?
Could you provide a minimal example that demonstrates this problem?
A more trivial code example is:
please use latest cppcheck-head it's hard to read the code when your code differs to what I see.
we have fixed a number of problems also .. so it would be unfortunate if you have some problem that we already fixed.
The code in
ExprEngine::FloatRange::isLessThan
is:I can see that some exception is thrown but I can't personally see what exception it is.. :-(
I am not sure how to interpret the
z3: ast is not an expression
expression that you get.The
std::cout
prints this output for me:I believe the first line declares a z3 symbol with the name
|0.0|
.. I am not sure why we do that right here.Not sure.. but maybe the proper output should say this instead:
You can test the z3 expressions on the command line.
Put this in a file
1.z3
:Then run this command:
It will say
unsat
.I have the feeling an assertion is missing in the std::cout output that ensures that
|0.0|
is equal to0.0
.It seems that I get stuck with something very similar in PR2956. I'm trying to add "null" values for string and float literals. This is necessary to handle them inside the condition statements. Something like this:
But now I get strange results in my tests for generated z3 expressions.
For example, the following snippet (test
ifAlwaysFalse3
) does not generate z3 expression at all:But it seems that it works correct for me, because it gives the following debug output:
hmm strange. maybe there is something bad with the unit test framework.
For information.. I am guessing that the constraint can be tweaked in that debug output, I'd guess this is false too:
if (0.7)
.what is your test code for that? The second operand for
expr(code,binop)
should be a binary operator token (in cppcheck ast, not z3 expression).Last edit: Daniel Marjamäki 2020-12-18
It is currently marked here as TODO_ASSERT_EQUALS:
According to debug output, there is no binary operator token in cppcheck AST:
I added the cout, but my code doesn't get to it. So I added a cout before the solver.add line which shows up in my output.
I'm currently pointing at
I changed the code to look like this
This is what my output looks like
littlest.cpp Looks like this
I'm using z3 version 4.8.9 If I actually recompile my version of z3 with debug on it segfaults on the solve.add call.
Below is part of the stack trace
The ASSERT it is tripping on is in /BuildTest/z3/src/api/api_context.cpp:222
~~~
~~~
What version of z3 are you using?
Last edit: john borland 2020-12-18