cpp:S6231 false positive

Programming language: c++ (cpp20)
Rule: cpp:S6231
IDE: Visual Studio (2022, 2026)
SonarQube version: 10.5.0.16999

This rule should not complain when both, address (data()) and length (size()) of a string_view object are provided to a called function.

Example:

std::string_view view{"some text"};

char const* comp_text{"some other text"};

if (0 == strncmp(comp_text, view.data(), view.size()))

  do_something();


That code is totally valid and there is no risk of reading past the end of the memory area, described by the string_view object.

The reason to use string_view objects, i.e. when parsing text, is to explicitly prevent permanently copying parts into new string objects. Doing so only to produce “compliant” code would be bad. Disabling this hint/warning would also be bad for those cases, where the size() property is not taken into account.

Hello @A-x-S,

Welcome to the Community!

Thank you for reporting this. The intent of rule S6231 is to prevent unnecessary temporaries and unsafe usage, not to flag this pattern. However, the analyzer currently does not recognize this safe usage and raises a false positive.

At this time, there is no configuration or workaround to prevent this specific false positive, other than suppressing it locally or marking it as a false positive in the UI. I will flag this for our internal team to consider an improvement, so that future versions of the rule can better recognize this safe pattern.

Best regards,

Stevan