[commit: ghc] master: rts: Don't use strndup (d1ce35d)
git at git.haskell.org
git at git.haskell.org
Mon Apr 18 22:01:12 UTC 2016
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/d1ce35d2271ac8b79cb5e37677b1a989749e611c/ghc
>---------------------------------------------------------------
commit d1ce35d2271ac8b79cb5e37677b1a989749e611c
Author: Ben Gamari <bgamari.foss at gmail.com>
Date: Mon Apr 18 22:32:59 2016 +0200
rts: Don't use strndup
Reviewers: austin
Subscribers: thomie
Differential Revision: https://phabricator.haskell.org/D2125
>---------------------------------------------------------------
d1ce35d2271ac8b79cb5e37677b1a989749e611c
rts/RtsFlags.c | 2 +-
rts/RtsUtils.c | 12 ++++++++++++
rts/RtsUtils.h | 2 ++
3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/rts/RtsFlags.c b/rts/RtsFlags.c
index 9db3cd4..bffb128 100644
--- a/rts/RtsFlags.c
+++ b/rts/RtsFlags.c
@@ -1597,7 +1597,7 @@ static rtsBool read_heap_profiling_flag(const char *arg_in)
if (!right)
right = arg + strlen(arg);
- char *selector = strndup(left, right - left);
+ char *selector = stgStrndup(left, right - left + 1);
switch (arg[2]) {
case 'c': // cost centre label select
diff --git a/rts/RtsUtils.c b/rts/RtsUtils.c
index a36532c..c837143 100644
--- a/rts/RtsUtils.c
+++ b/rts/RtsUtils.c
@@ -112,6 +112,18 @@ stgCallocBytes (int n, int m, char *msg)
return space;
}
+/* borrowed from the MUSL libc project */
+char *stgStrndup(const char *s, size_t n)
+{
+ size_t l = strnlen(s, n);
+ char *d = stgMallocBytes(l+1, "stgStrndup");
+ if (!d) return NULL;
+ memcpy(d, s, l);
+ d[l] = 0;
+ return d;
+}
+
+
/* To simplify changing the underlying allocator used
* by stgMallocBytes(), provide stgFree() as well.
*/
diff --git a/rts/RtsUtils.h b/rts/RtsUtils.h
index 5d825a2..d76c921 100644
--- a/rts/RtsUtils.h
+++ b/rts/RtsUtils.h
@@ -26,6 +26,8 @@ void *stgReallocBytes(void *p, int n, char *msg);
void *stgCallocBytes(int n, int m, char *msg)
GNUC3_ATTRIBUTE(__malloc__);
+char *stgStrndup(const char *s, size_t n);
+
void stgFree(void* p);
/* -----------------------------------------------------------------------------
More information about the ghc-commits
mailing list