Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
util: add uv_strlcpy() and uv_strlcat() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Nov 23, 2011
1 parent 0834e73 commit b52b8c7
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 22 deletions.
15 changes: 15 additions & 0 deletions include/uv.h
Expand Up @@ -371,6 +371,21 @@ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
UV_EXTERN uv_buf_t uv_buf_init(char* base, size_t 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; \
Expand Down
18 changes: 0 additions & 18 deletions src/unix/core.c
Expand Up @@ -827,21 +827,3 @@ int uv__cloexec(int fd, int set) {
return 0;
#endif
}


/* TODO move to uv-common.c? */
size_t uv__strlcpy(char* dst, const char* src, size_t size) {
const char *org;

if (size == 0) {
return 0;
}

org = src;
while (--size && *src) {
*dst++ = *src++;
}
*dst = '\0';

return src - org;
}
2 changes: 0 additions & 2 deletions src/unix/internal.h
Expand Up @@ -138,8 +138,6 @@ enum {
UV_TCP_KEEPALIVE = 0x100 /* Turn on keep-alive. */
};

size_t uv__strlcpy(char* dst, const char* src, size_t size);

int uv__close(int fd);
void uv__req_init(uv_req_t*);
void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle, uv_handle_type type);
Expand Down
4 changes: 2 additions & 2 deletions src/unix/pipe.c
Expand Up @@ -74,7 +74,7 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
}

memset(&saddr, 0, sizeof saddr);
uv__strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;

if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
Expand Down Expand Up @@ -197,7 +197,7 @@ void uv_pipe_connect(uv_connect_t* req,
}

memset(&saddr, 0, sizeof saddr);
uv__strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;

/* We don't check for EINPROGRESS. Think about it: the socket
Expand Down
35 changes: 35 additions & 0 deletions src/uv-common.c
Expand Up @@ -41,6 +41,41 @@ uv_counters_t* uv_counters() {
}


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, size_t len) {
uv_buf_t buf;
buf.base = base;
Expand Down
5 changes: 5 additions & 0 deletions test/test-list.h
Expand Up @@ -118,6 +118,9 @@ TEST_DECLARE (threadpool_queue_work_simple)
TEST_DECLARE (thread_mutex)
TEST_DECLARE (thread_rwlock)
TEST_DECLARE (thread_create)
TEST_DECLARE (strlcpy)
TEST_DECLARE (strlcat)

#ifdef _WIN32
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
TEST_DECLARE (argument_escaping)
Expand Down Expand Up @@ -273,6 +276,8 @@ TASK_LIST_START
TEST_ENTRY (thread_mutex)
TEST_ENTRY (thread_rwlock)
TEST_ENTRY (thread_create)
TEST_ENTRY (strlcpy)
TEST_ENTRY (strlcat)

#if 0
/* These are for testing the test runner. */
Expand Down
97 changes: 97 additions & 0 deletions test/test-util.c
@@ -0,0 +1,97 @@
/* 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 <string.h>

#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;
}
1 change: 1 addition & 0 deletions uv.gyp
Expand Up @@ -278,6 +278,7 @@
'test/runner.h',
'test/test-get-loadavg.c',
'test/task.h',
'test/test-util.c',
'test/test-async.c',
'test/test-error.c',
'test/test-callback-stack.c',
Expand Down

0 comments on commit b52b8c7

Please sign in to comment.