[commit: ghc] ghc-8.0: rts: Don't use strndup (7f19aed)

git at git.haskell.org git at git.haskell.org
Tue Apr 19 08:27:51 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-8.0
Link       : http://ghc.haskell.org/trac/ghc/changeset/7f19aed499744ed3595fef9f189f03925393a6b7/ghc

>---------------------------------------------------------------

commit 7f19aed499744ed3595fef9f189f03925393a6b7
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
    
    (cherry picked from commit d1ce35d2271ac8b79cb5e37677b1a989749e611c)


>---------------------------------------------------------------

7f19aed499744ed3595fef9f189f03925393a6b7
 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 5ecb5e0..46a7de1 100644
--- a/rts/RtsFlags.c
+++ b/rts/RtsFlags.c
@@ -1607,7 +1607,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