Hello, we think we are receiving false positive code smells in the following simplified C++ code:
typedef enum gnss_power {
POWEROFF,
POWERON,
LOWPOWER,
POWERLAST
} power_t;
static void use_power_enum(power_t *enum_var) {
int result;
result = static_cast<int>(*enum_var);
}
static void pwr_control(const std::string &arg) {
power_t power = POWERLAST;
int res = 0;
// the following lines of code does not raise issues:
power = static_cast<power_t>(NULL); // null
power = static_cast<power_t>(1); // within range of enum
power = static_cast<power_t>(6); // outside of enum range
// the expression which raises an issue:
power = static_cast<power_t>(std::stol(arg));
if (power < POWERLAST) {
use_power_enum(&power);
} else {
// res = warn error
}
}
Our setup:
SonarLint v3.11.0 - not connected to server.
SonarQube server - 9.5.0
Both SonarLint and SonarQube server reports issues.
I assume you are using VS Code?
I cannot reproduce the issue. For me, S4962 isn’t raised on your snippet.
Can you follow steps 3 and 4 in the troubleshooting section? Also, share with me the reproducer file to be able to investigate the issue.
I have tried step 3 - to enable rule cpp:S2260 /c:S2260. After enabling it the issue disappeared.
Also, the step 4 was tried. A file sonar-cfamily-reproducer.zip was generated. Can I provide provide it privately?
@Ovidijus, If you no longer reproduce the issue, then the reproducer won’t help. Once you face the false positive again, generate the reproducer of the culprit file and I will send you a PM where you can share it privately.
Note: make sure to remove the reproducer option for the analysis to work correctly again.
That is strange as rules are independent. Did you try to run the analysis after enabling S2260? The analysis runs automatically when you modify the file.
Thanks for the reproducer. I was able to reproduce it. I wasn’t able to reproduce it before due to different STL implementations.
#include <string>
namespace test {
inline long stol(const std::string& _Str, size_t* _Idx = NULL, int _Base = 10);
}
typedef enum gnss_power {
POWEROFF,
POWERON,
LOWPOWER,
POWERLAST
} power_t;
static void pwr_control(const std::string& arg) {
power_t power = static_cast<power_t>(test::stol(arg)); // FP. S4962 is raised on this line instead of the stol declaration
}
In your STL stol is use NULL as a default argument for the second parameter. So the issue triggers incorrectly on your implicit argument code instead of triggering on the STL declaration.
my STL stol declaration use nullptr and that is why I wasn’t able to reproduce it.
For example, if you use in your code std::stol(arg, nullptr) the issue should disappear.
For SonarLint No. once we fix the issue, You will need to update the SonarLint version to fix the issue.
For SQ, yes. Once we fix it, you will have to update the server.