Use asprintf for string construction
[openafs.git] / src / util / dirpath.c
index a2162ba..b307184 100644 (file)
@@ -556,12 +556,9 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
            status = EINVAL;
        } else {
            /* fully qualified path; just make a copy */
-           newPath = (char *)malloc(strlen(cpath) + 1);
-           if (!newPath) {
+           newPath = strdup(cpath);
+           if (!newPath)
                status = ENOMEM;
-           } else {
-               (void)strcpy(newPath, cpath);
-           }
        }
 
     } else {
@@ -645,20 +642,12 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
 
     LocalizePathHead(&cpath, &relativeTo);
     if (*cpath == '/') {
-       newPath = (char *)malloc(strlen(cpath) + 1);
-       if (!newPath) {
-           status = ENOMEM;
-       } else {
-           strcpy(newPath, cpath);
-       }
+       newPath = strdup(cpath);
     } else {
-       newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1);
-       if (!newPath) {
-           status = ENOMEM;
-       } else {
-           sprintf(newPath, "%s/%s", relativeTo, cpath);
-       }
+       asprintf(&newPath, "%s/%s", relativeTo, cpath);
     }
+    if (newPath == NULL)
+       status = ENOMEM;
 
     if (status == 0) {
        FilepathNormalize(newPath);