New function 'getcpy_len' works similarly to 'getcpy' but
accepts a character pointer and length instead of a C string.

If the call site knows the length, getcpy_len is more
efficient, because it does not call strlen.

Update 4 callers that could more correctly and/or more
efficiently call getcpy_len.


diff --git a/sbr/getcpy.c b/sbr/getcpy.c
index dd1debb6..cec4d08a 100644
--- a/sbr/getcpy.c
+++ b/sbr/getcpy.c
@@ -22,3 +22,14 @@ getcpy(const char *str)
 
     return mh_xcalloc(1, 1);
 }
+
+/* Return malloc'd copy of str of length len, or of "" if NULL,
+ * exit on failure. */
+char *
+getcpy_len(const char *str, size_t len)
+{
+    if (str)
+        return xmemtostr(str, len);
+
+    return mh_xcalloc(1, 1);
+}
diff --git a/sbr/getcpy.h b/sbr/getcpy.h
index c98146e1..8c67caf8 100644
--- a/sbr/getcpy.h
+++ b/sbr/getcpy.h
@@ -10,3 +10,8 @@
 
 /* Return malloc'd copy of str, or of "" if NULL, exit on failure. */
 char *getcpy(const char *str);
+
+/* Return malloc'd copy of str of length len, or of "" if NULL,
+ * exit on failure.
+ * See comment at xmemtostr about strings containing a NUL byte. */
+char *getcpy_len(const char *str, size_t len);
diff --git a/uip/imaptest.c b/uip/imaptest.c
index 8167a7ff..23ca93d0 100644
--- a/uip/imaptest.c
+++ b/uip/imaptest.c
@@ -929,11 +929,11 @@ getline:
                 has_prefix_len(line + 2, len - 2, token)) {
 		if (*tokenresponse)
 		    free(*tokenresponse);
-		*tokenresponse = getcpy(line + 2);
+		*tokenresponse = getcpy_len(line + 2, len - 2);
 	    }
 	} if (condok && has_prefix_len(line, len, "+")) {
 	    if (status) {
-		*status = getcpy(line);
+		*status = getcpy_len(line, len);
 	    }
 	    /*
 	     * Special case; return now but don't dequeue the tag,
@@ -964,7 +964,7 @@ getline:
 				      "time", cmd2->prefix);
 			free(cmd2);
 			if (status)
-			    *status = getcpy(line);
+			    *status = getcpy_len(line, len);
 			goto getline;
 		    }
 		}
@@ -1068,8 +1068,7 @@ batchfile(const char *filename, char *afolder, bool queue)
 		    die("Unable to read next line for -afolder");
 		if (rc == 1)
 		    die("Folder name cannot be blank");
-		line[rc - 1] = '\0';
-		afolder = getcpy(line);
+		afolder = getcpy_len(line, rc - 1); /* -1 to skip newline */
 		afolder_alloc = true;
 		continue;
 

Reply via email to