Hello,
The function strsave() in csh(1) is practically strdup(3).
The only difference is memory allocation failure results in
calling the stderror() error handler, which will later exit.
This patch makes the code (IMO) clearer by removing two loops.
xmalloc() behaves the same as xreallocarray() in terms of
calling stderror(). Does this look OK?
- Michael
Index: misc.c
===================================================================
RCS file: /cvs/src/bin/csh/misc.c,v
retrieving revision 1.20
diff -u -p -u -r1.20 misc.c
--- misc.c 20 Jun 2017 16:44:06 -0000 1.20
+++ misc.c 8 Sep 2018 02:23:34 -0000
@@ -34,6 +34,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
+#include <string.h>
#include "csh.h"
#include "extern.h"
@@ -56,15 +57,13 @@ char *
strsave(char *s)
{
char *n;
- char *p;
+ size_t len;
if (s == NULL)
s = "";
- for (p = s; *p++;)
- continue;
- n = p = xreallocarray(NULL, (p - s), sizeof(char));
- while ((*p++ = *s++) != '\0')
- continue;
+ len = strlen(s) + 1;
+ n = xmalloc(len);
+ (void)memcpy(n, s, len);
return (n);
}