From fb29dba40ea12f552933dc4de75d06bc327589b4 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 16 Sep 2022 22:40:25 +0000 Subject: [PATCH] Fix another charpool blunder: null goes at end of string, not end of allocation. --- charpool.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/charpool.cc b/charpool.cc index 39427058f..b0699d21d 100644 --- a/charpool.cc +++ b/charpool.cc @@ -106,14 +106,16 @@ void CharPool::clear(void) { } const char *CharPool::dup(const char *src, int len) { - int sz = len < 0 ? strlen(src) : len; + if (len < 0) + len = strlen(src); + int sz = len + 1; char *p = buckets.back() + nexti; int modulus; if ((modulus = sz % ALIGN_ON)) sz += ALIGN_ON - modulus; - while (nexti + sz > currentbucketsz - 1) { + while (nexti + sz > currentbucketsz) { /* Doh! We've got to make room */ currentbucketsz <<= 1; nexti = 0; @@ -121,7 +123,7 @@ const char *CharPool::dup(const char *src, int len) { buckets.push_back(p); } - nexti += sz + 1; - p[sz] = '\0'; - return (const char *) memcpy(p, src, sz); + nexti += sz; + p[len] = '\0'; + return (const char *) memcpy(p, src, len); }