From fe0258f17254e7c8c1daac9047d05c18c28e59b9 Mon Sep 17 00:00:00 2001 From: Christine Caulfield Date: Mon, 4 Jan 2021 13:16:35 +0000 Subject: [PATCH 1/2] strlcpy: Check for maxlen underflow https://github.com/ClusterLabs/libqb/issues/429 --- lib/strlcpy.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/strlcpy.c b/lib/strlcpy.c index 4071edf32..11ff74337 100644 --- a/lib/strlcpy.c +++ b/lib/strlcpy.c @@ -33,7 +33,8 @@ strlcpy(char *dest, const char * src, size_t maxlen) size_t srclen = strlen(src); size_t len2cpy = QB_MIN(maxlen-1, srclen); - if (len2cpy > 0) { + // check maxlen separately as it could have underflowed from 0 above. + if (maxlen && len2cpy > 0) { strncpy(dest, src, len2cpy+1); dest[len2cpy] = '\0'; } From 3694ae6b9378660f876f6250868a523a025a9d80 Mon Sep 17 00:00:00 2001 From: Christine Caulfield Date: Thu, 7 Jan 2021 10:19:02 +0000 Subject: [PATCH 2/2] Always terminate the string if maxlen is > 0 --- lib/strlcpy.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/strlcpy.c b/lib/strlcpy.c index 11ff74337..63e17f783 100644 --- a/lib/strlcpy.c +++ b/lib/strlcpy.c @@ -33,9 +33,12 @@ strlcpy(char *dest, const char * src, size_t maxlen) size_t srclen = strlen(src); size_t len2cpy = QB_MIN(maxlen-1, srclen); - // check maxlen separately as it could have underflowed from 0 above. - if (maxlen && len2cpy > 0) { - strncpy(dest, src, len2cpy+1); + /* check maxlen separately as it could have underflowed from 0 above. */ + if (maxlen) { + if (len2cpy > 0) { + strncpy(dest, src, len2cpy+1); + } + /* Always terminate, even if its empty */ dest[len2cpy] = '\0'; } return srclen;