From: Matthew Fernandez Date: Sun, 7 Aug 2022 02:52:18 +0000 (-0700) Subject: cgraph agxbput_n: take an early exit when input string is zero X-Git-Tag: 5.0.1~16^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=931977254bcc4947c14b726c107aa91b6e2ae3f5;p=graphviz cgraph agxbput_n: take an early exit when input string is zero An upcoming change makes it possible to enter this function with `xb->ptr` as `NULL`. It looks as if there is nothing wrong with this; a zero-sized string fits in any agxbuf, including an unallocated one. However UBSan educates us that calling `memcpy` with a `NULL` destination pointer is undefined behavior, even when the passed size is 0. So this change avoids reaching `memcpy` when we know the function will be a no-op. Note that this is sort of fixing a latent bug, in that an agxbuf user could already cause the scenario described above by `agxbdisown`ing an agxbuf and then calling `agxbput_n` on it with size 0. However no existing callers do this. --- diff --git a/lib/cgraph/agxbuf.h b/lib/cgraph/agxbuf.h index 12bf6c3f6..7412114bd 100644 --- a/lib/cgraph/agxbuf.h +++ b/lib/cgraph/agxbuf.h @@ -150,6 +150,9 @@ static inline PRINTF_LIKE(2, 3) int agxbprint(agxbuf *xb, const char *fmt, * Append string s of length ssz into xb */ static inline size_t agxbput_n(agxbuf *xb, const char *s, size_t ssz) { + if (ssz == 0) { + return 0; + } if (xb->ptr + ssz > xb->eptr) agxbmore(xb, ssz); memcpy(xb->ptr, s, ssz);