aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-11-25 22:30:56 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-11-25 22:30:56 +0100
commit0b4c4d74344613a15791e61179a441d80803d67a (patch)
tree4a908f3f0b4e6704b6895ee5e3c679250550270a /src/libexpr
parentbc9692a6b701fffe25d2e3b1d16f00edd596930f (diff)
Don't use GC_STRNDUP
It calls strlen() on the input (rather than simply copying at most `size` bytes), which can fail if the input is not zero-terminated and is inefficient in any case. Fixes #7347.
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc10
1 files changed, 3 insertions, 7 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 563f24e48..6ba44cc1d 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -69,15 +69,11 @@ static char * dupString(const char * s)
// empty string.
static const char * makeImmutableStringWithLen(const char * s, size_t size)
{
- char * t;
if (size == 0)
return "";
-#if HAVE_BOEHMGC
- t = GC_STRNDUP(s, size);
-#else
- t = strndup(s, size);
-#endif
- if (!t) throw std::bad_alloc();
+ auto t = allocString(size + 1);
+ memcpy(t, s, size);
+ t[size] = 0;
return t;
}