From 081232081ccb96d2361834625caba4d3d2341437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 9 Jul 2018 07:03:01 +0200 Subject: Introduce free_and_strndup and use it in bus-message.c v2: fix error in free_and_strndup() When the orignal and copied message were the same, but shorter than specified length l, memory read past the end of the buffer would be performed. A test case is included: a string that had an embedded NUL ("q\0") is used to replace "q". v3: Fix one more bug in free_and_strndup and add tests. v4: Some style fixed based on review, one more use of free_and_replace, and make the tests more comprehensive. (cherry picked from commit 7f546026abbdc56c453a577e52d57159458c3e9c) --- src/basic/string-util.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/basic/string-util.h') diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 3cada129e..46760cbc1 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -182,6 +182,7 @@ char *strrep(const char *s, unsigned n); int split_pair(const char *s, const char *sep, char **l, char **r); int free_and_strdup(char **p, const char *s); +int free_and_strndup(char **p, const char *s, size_t l); /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { -- cgit v1.2.3