From 5097155bdf39bd18a778b985354b0b38fd499f55 Mon Sep 17 00:00:00 2001 From: Chrissie Caulfield Date: Wed, 13 Jan 2021 14:12:02 +0000 Subject: [PATCH] strlcpy: Check for maxlen underflow (#432) * strlcpy: Check for maxlen underflow https://github.com/ClusterLabs/libqb/issues/429 * Always terminate the string if maxlen is > 0 --- lib/strlcpy.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/strlcpy.c b/lib/strlcpy.c index 4071edf32..63e17f783 100644 --- a/lib/strlcpy.c +++ b/lib/strlcpy.c @@ -33,8 +33,12 @@ 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) { - 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;