commit 1930624b9a9703c3449d2a877640e33c6d71f190
Author:     Laslo Hunhold <[email protected]>
AuthorDate: Tue Mar 1 09:27:12 2022 +0100
Commit:     Laslo Hunhold <[email protected]>
CommitDate: Tue Mar 1 09:27:12 2022 +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 <[email protected]>

diff --git a/src/utf8.c b/src/utf8.c
index f386edf..3584c61 100644
--- 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 */

Reply via email to