[PATCH 6/6] Convert some malloc + strncpy pairs into strndup calls

Alan Coopersmith alan.coopersmith at oracle.com
Fri Oct 28 17:34:38 PDT 2011


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 dix/atom.c             |    7 ++-----
 hw/kdrive/src/kinput.c |    4 +---
 os/utils.c             |    4 +---
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/dix/atom.c b/dix/atom.c
index 88b40db..83ff71a 100644
--- a/dix/atom.c
+++ b/dix/atom.c
@@ -118,14 +118,11 @@ MakeAtom(const char *string, unsigned len, Bool makeit)
 	}
 	else
 	{
-	    char *newstring = malloc(len + 1);
-	    if (!newstring) {
+	    nd->string = strndup(string, len);
+	    if (!nd->string) {
 		free(nd);
 		return BAD_RESOURCE;
 	    }
-	    strncpy(newstring, string, (int)len);
-	    newstring[len] = 0;
-	    nd->string = newstring;
 	}
 	if ((lastAtom + 1) >= tableLength) {
 	    NodePtr *table;
diff --git a/hw/kdrive/src/kinput.c b/hw/kdrive/src/kinput.c
index 6a1ce49..9c0b34f 100644
--- a/hw/kdrive/src/kinput.c
+++ b/hw/kdrive/src/kinput.c
@@ -1049,12 +1049,10 @@ KdGetOptions (InputOption **options, char *string)
     if (strchr(string, '='))
     {
         tam_key = (strchr(string, '=') - string);
-        key = malloc(tam_key + 1);
+        key = strndup(string, tam_key);
         if (!key)
             goto out;
 
-        strncpy(key, string, tam_key);
-        key[tam_key] = '\0';
         value = strdup(strchr(string, '=') + 1);
         if (!value)
             goto out;
diff --git a/os/utils.c b/os/utils.c
index 07cf4c2..1c75dfc 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1664,13 +1664,11 @@ CheckUserParameters(int argc, char **argv, char **envp)
 		    if (!eq)
 			continue;
 		    len = eq - envp[i];
-		    e = malloc(len + 1);
+		    e = strndup(envp[i], len);
 		    if (!e) {
 			bad = InternalError;
 			break;
 		    }
-		    strncpy(e, envp[i], len);
-		    e[len] = 0;
 		    if (len >= 4 &&
 			(strcmp(e + len - 4, "PATH") == 0 ||
 			 strcmp(e, "TERMCAP") == 0)) {
-- 
1.7.3.2



More information about the xorg-devel mailing list