[hackers] [libgrapheme] Properly handle cp == NULL in grapheme_decode_utf8() || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Tue, 1 Mar 2022 09:32:16 +0100 (CET)

commit 1930624b9a9703c3449d2a877640e33c6d71f190
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Tue Mar 1 09:27:12 2022 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
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 <dev_AT_frign.de>

diff --git a/src/utf8.c b/src/utf8.c
index f386edf..3584c61 100644
--- a/src/utf8.c
+++ b/src/utf8.c
_AT_@ -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 */
Received on Tue Mar 01 2022 - 09:32:16 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 01 2022 - 09:36:38 CET