Adjacent string literals combine two different encoding prefixes
ID |
c.correctness.mixed_encoding_string_concat |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Literal Format |
Language |
C / C++ |
Description
Reports two adjacent string literals whose encoding prefixes differ, such as u"utf16" U"utf32" or u8"utf8" L"wide". Concatenating a prefixed literal with an unprefixed one is well-formed, but concatenating two literals that each carry a different, non-empty prefix is not.
Rationale
The standard only defines string-literal concatenation when at most one of the adjacent literals carries an encoding prefix; combining u, U, L, and u8 with each other is ill-formed. A conforming compiler must reject the program, so this is a portable compile-time failure rather than a subtle runtime bug - but it usually appears after a literal was edited or copy-pasted from code using a different character type, and the resulting error can be confusing to someone unfamiliar with the concatenation rule.
The following code illustrates the pattern detected by this rule:
void report()
{
// FLAGGED: Adjacent string literals combine two different encoding prefixes
const char16_t *utf16_utf32 = u"utf16" U"utf32";
// FLAGGED: Adjacent string literals combine two different encoding prefixes
const char *utf8_utf32 = u8"utf8" U"utf32";
const wchar_t *wide_narrow = L"wide" "narrow";
const char *plain = "hello" "world";
const wchar_t *wide_wide = L"one" L"two";
}