False-positive cpp:S109 with string literals and concatenation

  • What language is this for? C++
  • Which rule? cpp:S109
  • SonarCloud, connected*
#include <string>

using namespace std;
using namespace std::string_literals;

void foo(const wchar_t * unused);

void bar() {
    foo((L"XX"s + to_wstring(0)).c_str());
}

image

Hello @bers

First, I’d like to thank you for providing us with a small autonomous example that reproduces the issue. It is very helpful.

We indeed have a problem with this rule. It detects all magic numbers, even the ones that are not explicitly written in the code. When you write L"XX"s, the C++ language transforms this into something like:

 std::operator""s(L"XX", 2UL);

And we erroneously detect as a magic number the 2 that is the length of the string literal…

We already have a ticket to correct this issue. I added your case to this ticket.

1 Like

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