aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.h1
-rw-r--r--lib/xwrap.c12
-rw-r--r--toys/posix/sed.c3
3 files changed, 12 insertions, 4 deletions
diff --git a/lib/lib.h b/lib/lib.h
index e3dbc462..5733103b 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -101,6 +101,7 @@ void *xzalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrndup(char *s, size_t n);
char *xstrdup(char *s);
+void *xmemdup(void *s, long len);
char *xmprintf(char *format, ...) printf_format;
void xprintf(char *format, ...) printf_format;
void xputs(char *s);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index a20d4b6b..65e3018d 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -21,9 +21,9 @@ void xstrncpy(char *dest, char *src, size_t size)
void xstrncat(char *dest, char *src, size_t size)
{
- long len = strlen(src);
+ long len = strlen(dest);
- if (len+strlen(dest)+1 > size)
+ if (len+strlen(src)+1 > size)
error_exit("'%s%s' > %ld bytes", dest, src, (long)size);
strcpy(dest+len, src);
}
@@ -80,6 +80,14 @@ char *xstrdup(char *s)
return xstrndup(s, strlen(s));
}
+void *xmemdup(void *s, long len)
+{
+ void *ret = xmalloc(len);
+ memcpy(ret, s, len);
+
+ return ret;
+}
+
// Die unless we can allocate enough space to sprintf() into.
char *xmprintf(char *format, ...)
{
diff --git a/toys/posix/sed.c b/toys/posix/sed.c
index a57891e5..30d8a154 100644
--- a/toys/posix/sed.c
+++ b/toys/posix/sed.c
@@ -827,8 +827,7 @@ static void jewel_of_judgement(char **pline, long len)
if (strchr("aiqr=", c) && i>1) break;
// Add step to pattern
- corwin = xmalloc(reg-toybuf);
- memcpy(corwin, toybuf, reg-toybuf);
+ corwin = xmemdup(toybuf, reg-toybuf);
reg = (reg-toybuf) + (char *)corwin;
// Parse arguments by command type