From 0d9de01f9dfb15198174f4f7eab209f00da9c7f9 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sat, 12 Dec 2020 14:16:50 +0100 Subject: [PATCH] Make charmap-converting a non-UTF8 string non-fatal --- include/asm/util.h | 3 +++ src/asm/charmap.c | 4 ++++ src/asm/util.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/asm/util.h b/include/asm/util.h index ca2e43e0..84728021 100644 --- a/include/asm/util.h +++ b/include/asm/util.h @@ -13,6 +13,9 @@ uint32_t calchash(const char *s); char const *print(int c); +/* + * @return The number of bytes read, or 0 if invalid data was found + */ size_t readUTF8Char(uint8_t *dest, char const *src); #endif /* RGBDS_UTIL_H */ diff --git a/src/asm/charmap.c b/src/asm/charmap.c index eaddc3e0..4fca8b07 100644 --- a/src/asm/charmap.c +++ b/src/asm/charmap.c @@ -229,6 +229,10 @@ size_t charmap_Convert(char const *input, uint8_t *output) } else if (*input) { /* No match found */ size_t codepointLen = readUTF8Char(output, input); + if (codepointLen == 0) { + error("Input string is not valid UTF-8!"); + break; + } input += codepointLen; /* OK because UTF-8 has no NUL in multi-byte chars */ output += codepointLen; outputLen += codepointLen; diff --git a/src/asm/util.c b/src/asm/util.c index c8ef1c77..d1f04806 100644 --- a/src/asm/util.c +++ b/src/asm/util.c @@ -70,7 +70,7 @@ size_t readUTF8Char(uint8_t *dest, char const *src) for (;;) { if (decode(&state, &codep, src[i]) == 1) - fatalerror("invalid UTF-8 character\n"); + return 0; dest[i] = src[i]; i++;