From fb89eac7dcbc9338d467e2d56777c8b5979afdfd Mon Sep 17 00:00:00 2001 From: anon Date: Sat, 1 Apr 2023 11:47:08 +0100 Subject: [PATCH] Fix GetCodepointNext to return default value with size=0 on invalid input. Modify LoadCodepoints to work when GetCodepointNext returns a size of 0. All internal use of GetCodepointNext and GetCodepointPrev checked. This fix may break external code dealing with invalid input as the old code erroneously never returned a size of 0, external code that doesn't properly check for size=0 may endlessly loop or overflow a buffer on invalid input. --- src/rtext.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rtext.c b/src/rtext.c index 2d01360ef..32379551a 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1704,7 +1704,9 @@ int *LoadCodepoints(const char *text, int *count) for (int i = 0; i < textLength; codepointCount++) { codepoints[codepointCount] = GetCodepointNext(text + i, &codepointSize); - i += codepointSize; + + if (codepoints[codepointCount] == 0x3f) i += 1; + else i += codepointSize; } // Re-allocate buffer to the actual number of codepoints loaded @@ -1917,7 +1919,7 @@ int GetCodepointNext(const char *text, int *codepointSize) codepoint = ((0x1f & ptr[0]) << 6) | (0x3f & ptr[1]); *codepointSize = 2; } - else + else if (0x00 == (0x80 & ptr[0])) { // 1 byte UTF-8 codepoint codepoint = ptr[0];