Skip to content

Commit d6e707a

Browse files
authored
Merge pull request #4 from hobostay/fix/utf8-codepoint-validation
fix: validate Unicode codepoints in utf8_encode()
2 parents 4a2c2c5 + c538bdf commit d6e707a

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,19 @@ static uint32_t raylib_key_unshifted_codepoint(int rl_key)
246246

247247
// Encode a single Unicode codepoint into a UTF-8 byte buffer.
248248
// Returns the number of bytes written (1–4).
249+
// Invalid codepoints (> U+10FFFF) are replaced with U+FFFD.
249250
static int utf8_encode(uint32_t cp, char out[4])
250251
{
252+
// Unicode defines the maximum valid codepoint as U+10FFFF.
253+
// Codepoints above this value are invalid and should be replaced
254+
// with the Unicode replacement character U+FFFD.
255+
const uint32_t MAX_UNICODE = 0x10FFFF;
256+
const uint32_t REPLACEMENT_CHAR = 0xFFFD;
257+
258+
if (cp > MAX_UNICODE) {
259+
cp = REPLACEMENT_CHAR;
260+
}
261+
251262
if (cp < 0x80) {
252263
out[0] = (char)cp;
253264
return 1;

0 commit comments

Comments
 (0)