Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fonsTextIterNext when string ends with invalid UTF-8 sequence #497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/fontstash.h
Original file line number Diff line number Diff line change
Expand Up @@ -1395,13 +1395,15 @@ int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, FONSquad* quad)
{
FONSglyph* glyph = NULL;
const char* str = iter->next;
unsigned int last_dec_status = FONS_UTF8_ACCEPT;
iter->str = iter->next;

if (str == iter->end)
return 0;

for (; str != iter->end; str++) {
if (fons__decutf8(&iter->utf8state, &iter->codepoint, *(const unsigned char*)str))
last_dec_status = fons__decutf8(&iter->utf8state, &iter->codepoint, *(const unsigned char*)str);
if (last_dec_status)
continue;
str++;
// Get glyph and quad
Expand All @@ -1416,6 +1418,12 @@ int fonsTextIterNext(FONScontext* stash, FONStextIter* iter, FONSquad* quad)
}
iter->next = str;

if (last_dec_status) {
// If we are here then the iterator reached the end of the string and UTF-8 encoding failed
// In this case we do not have a valid code point so we need to return false
return 0;
}

return 1;
}

Expand Down