libgrapheme

Freestanding C library for unicode string handling
git clone https://git.sinitax.com/suckless/libgrapheme
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 1930624b9a9703c3449d2a877640e33c6d71f190
parent da46b2648d2846dc23e310b7ac0cc3ddebb7ccd3
Author: Laslo Hunhold <dev@frign.de>
Date:   Tue,  1 Mar 2022 09:27:12 +0100

Properly handle cp == NULL in grapheme_decode_utf8()

During refactoring I totally forgot about it. Instead of adding a
check every time we do anything, we save a lot of branching by doing
a single branch in the beginning, optionally setting cp, if NULL, to a
pointer to a local dummy variable.

Now it works as expected and documented, given my goal is that there
should be no case where a function segfaults due to a passed NULL
pointer.

Thanks a lot to Hécate (retro-freedom.nz) for reporting this!

Signed-off-by: Laslo Hunhold <dev@frign.de>

Diffstat:
Msrc/utf8.c | 9+++++++++
1 file changed, 9 insertions(+), 0 deletions(-)

diff --git a/src/utf8.c b/src/utf8.c @@ -51,6 +51,15 @@ size_t grapheme_decode_utf8(const char *str, size_t len, uint_least32_t *cp) { size_t off, i; + uint_least32_t tmp; + + if (cp == NULL) { + /* + * instead of checking every time if cp is NULL within + * the decoder, simply point it at a dummy variable here. + */ + cp = &tmp; + } if (str == NULL || len == 0) { /* a sequence must be at least 1 byte long */