From 7f44933c43f9f549963d68f27e2911bd9941c0e9 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 3 Nov 2013 03:48:18 +0100 Subject: [PATCH] include: remove uv_strlcat() and uv_strlcpy() It was pointed out that they don't quite work like their BSD namesakes and they arguably shouldn't have been part of the API anyway. Fixes #983. --- Makefile.am | 1 - checksparse.sh | 1 - include/uv.h | 15 -------- src/unix/pipe.c | 6 ++- src/uv-common.c | 34 ----------------- test/test-list.h | 4 -- test/test-util.c | 97 ------------------------------------------------ uv.gyp | 1 - 8 files changed, 4 insertions(+), 155 deletions(-) delete mode 100644 test/test-util.c diff --git a/Makefile.am b/Makefile.am index 9080b940..101c3d5a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -193,7 +193,6 @@ test_run_tests_SOURCES = test/blackhole-server.c \ test/test-udp-open.c \ test/test-udp-options.c \ test/test-udp-send-and-recv.c \ - test/test-util.c \ test/test-walk-handles.c test_run_tests_LDADD = libuv.la diff --git a/checksparse.sh b/checksparse.sh index c2e3e887..98dc03c5 100755 --- a/checksparse.sh +++ b/checksparse.sh @@ -158,7 +158,6 @@ test/test-udp-multicast-ttl.c test/test-udp-open.c test/test-udp-options.c test/test-udp-send-and-recv.c -test/test-util.c test/test-walk-handles.c " diff --git a/include/uv.h b/include/uv.h index b29174b8..e6b290bc 100644 --- a/include/uv.h +++ b/include/uv.h @@ -567,21 +567,6 @@ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb); UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len); -/* - * Utility function. Copies up to `size` characters from `src` to `dst` - * and ensures that `dst` is properly NUL terminated unless `size` is zero. - */ -UV_EXTERN size_t uv_strlcpy(char* dst, const char* src, size_t size); - -/* - * Utility function. Appends `src` to `dst` and ensures that `dst` is - * properly NUL terminated unless `size` is zero or `dst` does not - * contain a NUL byte. `size` is the total length of `dst` so at most - * `size - strlen(dst) - 1` characters will be copied from `src`. - */ -UV_EXTERN size_t uv_strlcat(char* dst, const char* src, size_t size); - - #define UV_STREAM_FIELDS \ /* number of bytes queued for writing */ \ size_t write_queue_size; \ diff --git a/src/unix/pipe.c b/src/unix/pipe.c index 705a9739..fd4afb63 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -72,7 +72,8 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) { sockfd = err; memset(&saddr, 0, sizeof saddr); - uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path)); + strncpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path) - 1); + saddr.sun_path[sizeof(saddr.sun_path) - 1] = '\0'; saddr.sun_family = AF_UNIX; if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) { @@ -167,7 +168,8 @@ void uv_pipe_connect(uv_connect_t* req, } memset(&saddr, 0, sizeof saddr); - uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path)); + strncpy(saddr.sun_path, name, sizeof(saddr.sun_path) - 1); + saddr.sun_path[sizeof(saddr.sun_path) - 1] = '\0'; saddr.sun_family = AF_UNIX; do { diff --git a/src/uv-common.c b/src/uv-common.c index 6cfd1085..4129a366 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -66,40 +66,6 @@ size_t uv_req_size(uv_req_type type) { #undef XX -size_t uv_strlcpy(char* dst, const char* src, size_t size) { - size_t n; - - if (size == 0) - return 0; - - for (n = 0; n < (size - 1) && *src != '\0'; n++) - *dst++ = *src++; - - *dst = '\0'; - - return n; -} - - -size_t uv_strlcat(char* dst, const char* src, size_t size) { - size_t n; - - if (size == 0) - return 0; - - for (n = 0; n < size && *dst != '\0'; n++, dst++); - - if (n == size) - return n; - - while (n < (size - 1) && *src != '\0') - n++, *dst++ = *src++; - - *dst = '\0'; - - return n; -} - uv_buf_t uv_buf_init(char* base, unsigned int len) { uv_buf_t buf; diff --git a/test/test-list.h b/test/test-list.h index d963f04c..a95105d6 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -209,8 +209,6 @@ TEST_DECLARE (thread_local_storage) TEST_DECLARE (thread_mutex) TEST_DECLARE (thread_rwlock) TEST_DECLARE (thread_create) -TEST_DECLARE (strlcpy) -TEST_DECLARE (strlcat) TEST_DECLARE (dlerror) TEST_DECLARE (poll_duplex) TEST_DECLARE (poll_unidirectional) @@ -505,8 +503,6 @@ TASK_LIST_START TEST_ENTRY (thread_mutex) TEST_ENTRY (thread_rwlock) TEST_ENTRY (thread_create) - TEST_ENTRY (strlcpy) - TEST_ENTRY (strlcat) TEST_ENTRY (dlerror) TEST_ENTRY (ip6_addr_link_local) #if 0 diff --git a/test/test-util.c b/test/test-util.c deleted file mode 100644 index d61d3b12..00000000 --- a/test/test-util.c +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#include "uv.h" -#include "task.h" - -#include - -#define memeq(a, b, c) (memcmp((a), (b), (c)) == 0) - - -TEST_IMPL(strlcpy) { - size_t r; - - { - char dst[2] = "A"; - r = uv_strlcpy(dst, "", 0); - ASSERT(r == 0); - ASSERT(memeq(dst, "A", 1)); - } - - { - char dst[2] = "A"; - r = uv_strlcpy(dst, "B", 1); - ASSERT(r == 0); - ASSERT(memeq(dst, "", 1)); - } - - { - char dst[2] = "A"; - r = uv_strlcpy(dst, "B", 2); - ASSERT(r == 1); - ASSERT(memeq(dst, "B", 2)); - } - - { - char dst[3] = "AB"; - r = uv_strlcpy(dst, "CD", 3); - ASSERT(r == 2); - ASSERT(memeq(dst, "CD", 3)); - } - - return 0; -} - - -TEST_IMPL(strlcat) { - size_t r; - - { - char dst[2] = "A"; - r = uv_strlcat(dst, "B", 1); - ASSERT(r == 1); - ASSERT(memeq(dst, "A", 2)); - } - - { - char dst[2] = "A"; - r = uv_strlcat(dst, "B", 2); - ASSERT(r == 1); - ASSERT(memeq(dst, "A", 2)); - } - - { - char dst[3] = "A"; - r = uv_strlcat(dst, "B", 3); - ASSERT(r == 2); - ASSERT(memeq(dst, "AB", 3)); - } - - { - char dst[5] = "AB"; - r = uv_strlcat(dst, "CD", 5); - ASSERT(r == 4); - ASSERT(memeq(dst, "ABCD", 5)); - } - - return 0; -} diff --git a/uv.gyp b/uv.gyp index 95095642..0233ffb1 100644 --- a/uv.gyp +++ b/uv.gyp @@ -297,7 +297,6 @@ 'test/runner.h', 'test/test-get-loadavg.c', 'test/task.h', - 'test/test-util.c', 'test/test-active.c', 'test/test-async.c', 'test/test-async-null-cb.c',