mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
CPP: Add a test for LossyFunctionResultCast.ql.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
| test.cpp:33:6:33:13 | call to getFloat | Return value of type float is implicitly converted to bool here. |
|
||||
| test.cpp:35:13:35:20 | call to getFloat | Return value of type float is implicitly converted to int here. |
|
||||
| test.cpp:38:6:38:14 | call to getDouble | Return value of type double is implicitly converted to bool here. |
|
||||
| test.cpp:40:13:40:21 | call to getDouble | Return value of type double is implicitly converted to int here. |
|
||||
| test.cpp:43:6:43:12 | call to getMyLD | Return value of type long double is implicitly converted to bool here. |
|
||||
| test.cpp:45:13:45:19 | call to getMyLD | Return value of type long double is implicitly converted to int here. |
|
||||
| test.cpp:78:6:78:11 | call to roundf | Return value of type float is implicitly converted to bool here. |
|
||||
| test.cpp:80:13:80:18 | call to roundf | Return value of type float is implicitly converted to int here. |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Conversion/LossyFunctionResultCast.ql
|
||||
@@ -0,0 +1,88 @@
|
||||
|
||||
typedef long double MYLD;
|
||||
|
||||
bool getBool();
|
||||
int getInt();
|
||||
float getFloat();
|
||||
double getDouble();
|
||||
MYLD getMyLD();
|
||||
float *getFloatPtr();
|
||||
float &getFloatRef();
|
||||
const float &getConstFloatRef();
|
||||
|
||||
void setPosInt(int x);
|
||||
void setPosFloat(float x);
|
||||
|
||||
double round(double x);
|
||||
float roundf(float x);
|
||||
|
||||
void test1()
|
||||
{
|
||||
// simple
|
||||
|
||||
if (getBool())
|
||||
{
|
||||
setPosInt(getBool());
|
||||
setPosFloat(getBool());
|
||||
}
|
||||
if (getInt())
|
||||
{
|
||||
setPosInt(getInt());
|
||||
setPosFloat(getInt());
|
||||
}
|
||||
if (getFloat()) // BAD
|
||||
{
|
||||
setPosInt(getFloat()); // BAD
|
||||
setPosFloat(getFloat());
|
||||
}
|
||||
if (getDouble()) // BAD
|
||||
{
|
||||
setPosInt(getDouble()); // BAD
|
||||
setPosFloat(getDouble());
|
||||
}
|
||||
if (getMyLD()) // BAD
|
||||
{
|
||||
setPosInt(getMyLD()); // BAD
|
||||
setPosFloat(getMyLD());
|
||||
}
|
||||
if (getFloatPtr())
|
||||
{
|
||||
// ...
|
||||
}
|
||||
if (getFloatRef()) // BAD [NOT DETECTED]
|
||||
{
|
||||
setPosInt(getFloatRef()); // BAD [NOT DETECTED]
|
||||
setPosFloat(getFloatRef());
|
||||
}
|
||||
if (getConstFloatRef()) // BAD [NOT DETECTED]
|
||||
{
|
||||
setPosInt(getConstFloatRef()); // BAD [NOT DETECTED]
|
||||
setPosFloat(getConstFloatRef());
|
||||
}
|
||||
|
||||
// explicit cast
|
||||
|
||||
if ((bool)getInt())
|
||||
{
|
||||
setPosInt(getInt());
|
||||
setPosFloat((float)getInt());
|
||||
}
|
||||
if ((bool)getFloat())
|
||||
{
|
||||
setPosInt((int)getFloat());
|
||||
setPosFloat(getFloat());
|
||||
}
|
||||
|
||||
// explicit rounding
|
||||
|
||||
if (roundf(getFloat())) // [FALSE POSITIVE]
|
||||
{
|
||||
setPosInt(roundf(getFloat())); // [FALSE POSITIVE]
|
||||
setPosFloat(roundf(getFloat()));
|
||||
}
|
||||
if (round(getDouble()))
|
||||
{
|
||||
setPosInt(round(getDouble()));
|
||||
setPosFloat(round(getDouble()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user