summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Batard <pbatard@gmail.com>2010-08-03 11:50:22 +0100
committerPete Batard <pbatard@gmail.com>2010-08-03 11:50:22 +0100
commitd91b872c4c87297d4df0f4e0220be04c223b2fdb (patch)
tree606abacf5b8ded844a6e77c975daf74c7932e39f
parentf5e41f3b7bcd1c43a1008327829a329b6605e93f (diff)
downloadlibusb-d91b872c4c87297d4df0f4e0220be04c223b2fdb.tar.gz
more safe string macros improvementspbr292
-rw-r--r--libusb/os/windows_usb.c2
-rw-r--r--libusb/os/windows_usb.h8
2 files changed, 6 insertions, 4 deletions
diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c
index 69ea185..a4d7f97 100644
--- a/libusb/os/windows_usb.c
+++ b/libusb/os/windows_usb.c
@@ -239,7 +239,7 @@ static char* sanitize_path(const char* path)
if ((ret_path = (char*)calloc(size, 1)) == NULL)
return NULL;
- safe_strncpy(&ret_path[add_root], size-add_root, path, safe_strlen(path));
+ safe_strcpy(&ret_path[add_root], size-add_root, path);
// Ensure consistancy with root prefix
for (j=0; j<root_size; j++)
diff --git a/libusb/os/windows_usb.h b/libusb/os/windows_usb.h
index 99a3a94..b2e9bd6 100644
--- a/libusb/os/windows_usb.h
+++ b/libusb/os/windows_usb.h
@@ -67,9 +67,11 @@ extern char *_strdup(const char *strSource);
#endif
#define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0)
#define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0)
-#define safe_strncpy(dst, dst_max, src, count) do {strncpy(dst, src, dst_max); ((char*)dst)[dst_max-1] = 0;} while(0)
-#define safe_strcpy(dst, dst_max, src) safe_strncpy(dst, dst_max, src, safe_strlen(src)+1)
-#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, min((size_t)count, (size_t)(dst_max - safe_strlen(dst) - 1)))
+#define safe_min(a, b) min((size_t)(a), (size_t)(b))
+#define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \
+ ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0)
+#define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1)
+#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1))
#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1)
#define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
#define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2), count)