changeset 20580:7acd63403bc1

9814 strndup() performs pathologically Reviewed by: Robert Mustacchi <rm@joyent.com> Reviewed by: Patrick Mooney <patrick.mooney@joyent.com> Reviewed by: Toomas Soome <tsoome@me.com> Reviewed by: Garrett D'Amore <garrett@damore.org> Reviewed by: Andy Fiddaman <af@citrus-it.net> Approved by: Richard Lowe <richlowe@richlowe.net>
author Bryan Cantrill <bryan@joyent.com>
date Fri, 07 Sep 2018 23:33:07 +0000
parents 2727d178ec5b
children 2c43fe65fda6 9b9c078a243b
files usr/src/lib/libc/port/gen/strndup.c
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/lib/libc/port/gen/strndup.c	Sat Sep 08 22:39:06 2018 +0000
+++ b/usr/src/lib/libc/port/gen/strndup.c	Fri Sep 07 23:33:07 2018 +0000
@@ -21,10 +21,12 @@
 
 /*
  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2018 Joyent, Inc.
  */
 
 #include "lint.h"
 #include <string.h>
+#include <strings.h>
 #include <stdlib.h>
 #include <sys/types.h>
 
@@ -38,7 +40,10 @@
 	char *s2;
 
 	n = strnlen(s1, n);
-	if ((s2 = malloc(n + 1)) != NULL)
-		(void) strlcpy(s2, s1, n + 1);
+	if ((s2 = malloc(n + 1)) != NULL) {
+		bcopy(s1, s2, n);
+		s2[n] = '\0';
+	}
+
 	return (s2);
 }