diff --git a/ProcessUNIX.c b/ProcessUNIX.c
index 3b32ca7d0a26b388e7a88cebb2e2e76747157b9e..80c053c491d04af0b7d2a763c1d380237c14269d 100644
--- a/ProcessUNIX.c
+++ b/ProcessUNIX.c
@@ -498,11 +498,10 @@ int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir)
     cp->WorkingDirectory = 0;
   }
   if (dir) {
-    cp->WorkingDirectory = (char*)malloc(strlen(dir) + 1);
+    cp->WorkingDirectory = strdup(dir);
     if (!cp->WorkingDirectory) {
       return 0;
     }
-    strcpy(cp->WorkingDirectory, dir);
   }
   return 1;
 }
@@ -531,11 +530,10 @@ int kwsysProcess_SetPipeFile(kwsysProcess* cp, int prPipe, const char* file)
     *pfile = 0;
   }
   if (file) {
-    *pfile = (char*)malloc(strlen(file) + 1);
+    *pfile = strdup(file);
     if (!*pfile) {
       return 0;
     }
-    strcpy(*pfile, file);
   }
 
   /* If we are redirecting the pipe, do not share it or use a native