[rtext] Fix TextToPascal()/TextToCamel() truncating text after a separator (#6132)

Both functions walk the input with two indexes and, on hitting '_', advance
j once and only write buffer[i] when the next character is one they know how
to handle. TextToPascal() (src/rtext.c:2186) covers 'a'-'z' and '0'-'9',
TextToCamel() (src/rtext.c:2270) covers only 'a'-'z'. Anything else leaves
buffer[i] at the zero it was memset to, so the returned string ends there
and the rest of the text is silently dropped: TextToPascal("text_UTF8_load")
returned "Text" and TextToCamel("sound_3d_mix") returned "sound".

The same path also reads past the end of the string. When '_' is the last
character, j++ lands on the '\0', neither branch matches, and then the loop
increment moves j one further before the condition reads text[j], one byte
beyond the terminator.

Skip the whole run of separators instead, stop when it reaches the end of
the text, and copy any character that has no upper case form as it is.
This commit is contained in:
Max Freedom Pollard
2026-09-06 18:17:52 -04:00
committed by GitHub
parent c64eda6377
commit 2b991b0243

View File

@@ -2186,9 +2186,11 @@ char *TextToPascal(const char *text)
if (text[j] != '_') buffer[i] = text[j];
else
{
j++;
while (text[j] == '_') j++; // Skip one or more separators
if (text[j] == '\0') break; // Text ends on a separator, nothing left to copy
if ((text[j] >= 'a') && (text[j] <= 'z')) buffer[i] = text[j] - 32;
else if ((text[j] >= '0') && (text[j] <= '9')) buffer[i] = text[j];
else buffer[i] = text[j]; // Character can not be upper-cased, copy it as is
}
}
}
@@ -2268,8 +2270,11 @@ char *TextToCamel(const char *text)
if (text[j] != '_') buffer[i] = text[j];
else
{
j++;
while (text[j] == '_') j++; // Skip one or more separators
if (text[j] == '\0') break; // Text ends on a separator, nothing left to copy
if ((text[j] >= 'a') && (text[j] <= 'z')) buffer[i] = text[j] - 32;
else buffer[i] = text[j]; // Character can not be upper-cased, copy it as is
}
}
}