OSDN Git Service

Replace strlcpy() with xstrcpy(), which exits if the string won't fit.
authorRob Landley <rob@landley.net>
Mon, 19 Nov 2007 07:51:00 +0000 (01:51 -0600)
committerRob Landley <rob@landley.net>
Mon, 19 Nov 2007 07:51:00 +0000 (01:51 -0600)
lib/lib.c
lib/lib.h

index 4cbb222..5d01efc 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
 
 #include "toys.h"
 
-#if !defined(__UCLIBC__) && !defined(__KLIBC__)
-
-// uClibc has this, and if we define our own it conflicts.
-
-// Like strncpy but always null terminated.
-void strlcpy(char *dest, char *src, size_t size)
+// Strcpy with size checking: exit if there's not enough space for the string.
+void xstrcpy(char *dest, char *src, size_t size)
 {
-       int len = strlen(src);
-       if (size--) {
-               if (len > size) len=size;
-               memcpy(dest,src, len);
-               dest[len] = 0;
-       }
+       if (strlen(src)+1 > size) error_exit("xstrcpy");
+       strcpy(dest, src);
 }
-#endif
-
 
 void verror_msg(char *msg, int err, va_list va)
 {
@@ -116,7 +106,7 @@ void *xrealloc(void *ptr, size_t size)
 void *xstrndup(char *s, size_t n)
 {
        void *ret = xmalloc(++n);
-       strlcpy(ret, s, n);
+       xstrcpy(ret, s, n);
 
        return ret;
 }
index 07f4a6d..67c6dac 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -36,10 +36,7 @@ struct dirtree *dirtree_read(char *path, struct dirtree *parent,
                     int (*callback)(struct dirtree *node));
 
 // lib.c
-#if !defined(__UCLIBC__) && !defined(__KLIBC__)
-void strlcpy(char *dest, char *src, size_t size);
-#endif
-
+void xstrcpy(char *dest, char *src, size_t size);
 void verror_msg(char *msg, int err, va_list va);
 void error_msg(char *msg, ...);
 void perror_msg(char *msg, ...);