From 3775e08bea5b16aaebfa37453589971e21103d47 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Mon, 29 Mar 2021 12:41:06 -0400 Subject: [PATCH] TT#111150 don't pass null pointers to memchr or memcpy If the length of the string is zero, the pointer to the buffer may be null. ASAN flags these as invalid/undefined, so don't do that. Change-Id: Ic55498465c53a78e1bd44c42d1a60f9bd3336473 --- lib/str.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/str.h b/lib/str.h index a232b091c..4af03bf50 100644 --- a/lib/str.h +++ b/lib/str.h @@ -155,6 +155,8 @@ INLINE int str_shift_cmp(str *s, const char *t) { return 0; } INLINE char *str_chr(const str *s, int c) { + if (!s->len) + return NULL; return memchr(s->s, c, s->len); } INLINE char *str_chr_str(str *out, const str *s, int c) { @@ -269,7 +271,8 @@ INLINE str *str_dup(const str *s) { str *r; r = str_alloc(s->len); r->len = s->len; - memcpy(r->s, s->s, s->len); + if (s->len) + memcpy(r->s, s->s, s->len); r->s[s->len] = '\0'; return r; }