Casting integer to enum causes: Use the "nullptr" literal (cpp:S4962)

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.

Thank you.

Hi @Ovidijus,

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.

Thanks,

Hello,

Yes, I am using VS Code.

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?

Thank you.

@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.

Thanks,

Hi,

What I meant was that issue disappears when cpp:S2260 is enabled. After, disabling - it shows up again.

The rule cpp:S2260 /c:S2260 was disabled before generating sonar-cfamily-reproducer.zip.

Can I share it with you?

Thanks.

@Ovidijus,

I sent you a PM to upload the reproducer.

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,

After enabling the rule S2260, the same issue still exists in the analysis result. My bad for bad for incorrect reporting before.

Thank you.

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.

Here is the ticket([CPP-3911] - Jira), we should fix it soon.

Thanks for your valuable report!

Thank you a lot @Abbas !

Will we need to update SonarQube server version to see changes?

Thanks

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.

Thank you for the clarification.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.