mirror of
https://github.com/github/codeql.git
synced 2026-04-29 18:55:14 +02:00
Merge pull request #654 from geoffw0/lossyresultcast
CPP: Work on Lossy function result cast query
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
| 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:101:10:101:12 | call to pow | Return value of type double is implicitly converted to int here. |
|
||||
| test.cpp:103:10:103:12 | call to pow | Return value of type double is implicitly converted to int here. |
|
||||
| test.cpp:105:10:105:12 | call to pow | Return value of type double is implicitly converted to int here. |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Conversion/LossyFunctionResultCast.ql
|
||||
@@ -0,0 +1,131 @@
|
||||
|
||||
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()))
|
||||
{
|
||||
setPosInt(roundf(getFloat()));
|
||||
setPosFloat(roundf(getFloat()));
|
||||
}
|
||||
if (round(getDouble()))
|
||||
{
|
||||
setPosInt(round(getDouble()));
|
||||
setPosFloat(round(getDouble()));
|
||||
}
|
||||
}
|
||||
|
||||
double pow(double x, double y);
|
||||
|
||||
int test2(double v, double w, int n)
|
||||
{
|
||||
switch (n)
|
||||
{
|
||||
case 1:
|
||||
return pow(2, v); // GOOD
|
||||
case 2:
|
||||
return pow(10, v); // GOOD
|
||||
case 3:
|
||||
return pow(2.5, v); // BAD
|
||||
case 4:
|
||||
return pow(v, 2); // BAD
|
||||
case 5:
|
||||
return pow(v, w); // BAD
|
||||
};
|
||||
}
|
||||
|
||||
double myRound1(double v)
|
||||
{
|
||||
return round(v);
|
||||
}
|
||||
|
||||
double myRound2(double v)
|
||||
{
|
||||
double result = round(v);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
double myRound3(double v)
|
||||
{
|
||||
return (v > 0) ? round(v) : 0;
|
||||
}
|
||||
|
||||
void test3()
|
||||
{
|
||||
int i = myRound1(1.5); // GOOD
|
||||
int j = myRound2(2.5); // GOOD
|
||||
int k = myRound3(3.5); // GOOD
|
||||
}
|
||||
Reference in New Issue
Block a user