aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-05-26 13:09:42 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-05-26 13:09:42 -0300
commitfc7bc0412ee111cdea61c89f3ec2461601013fa2 (patch)
treea7efb9c21c1903b5e79e18186d0172afb06eb40e /src
parent3a68a4861adcc950cdbde709d4841f8ea0c52b12 (diff)
parent014febef22a279b9a457aa2830caeec1d9917461 (diff)
downloadrneovim-fc7bc0412ee111cdea61c89f3ec2461601013fa2.tar.gz
rneovim-fc7bc0412ee111cdea61c89f3ec2461601013fa2.tar.bz2
rneovim-fc7bc0412ee111cdea61c89f3ec2461601013fa2.zip
Merge pull request #761 'fix a few strncpy calls by using xstrlcpy'
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/private/helpers.c2
-rw-r--r--src/nvim/api/private/helpers.h3
-rw-r--r--src/nvim/memory.c13
-rw-r--r--src/nvim/memory.h13
-rw-r--r--src/nvim/os/server.c12
5 files changed, 37 insertions, 6 deletions
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c
index c4340ddd89..a43e7a8d2a 100644
--- a/src/nvim/api/private/helpers.c
+++ b/src/nvim/api/private/helpers.c
@@ -67,7 +67,7 @@ bool try_end(Error *err)
ET_ERROR,
NULL,
&should_free);
- strncpy(err->msg, msg, sizeof(err->msg));
+ xstrlcpy(err->msg, msg, sizeof(err->msg));
err->set = true;
free_global_msglist();
diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h
index da7e357d04..2d917c2b5e 100644
--- a/src/nvim/api/private/helpers.h
+++ b/src/nvim/api/private/helpers.h
@@ -5,10 +5,11 @@
#include "nvim/api/private/defs.h"
#include "nvim/vim.h"
+#include "nvim/memory.h"
#define set_api_error(message, err) \
do { \
- strncpy(err->msg, message, sizeof(err->msg)); \
+ xstrlcpy(err->msg, message, sizeof(err->msg)); \
err->set = true; \
} while (0)
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index b6990890a6..238a6791c0 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -183,6 +183,19 @@ char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen)
}
}
+size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size)
+{
+ size_t ret = strlen(src);
+
+ if (size) {
+ size_t len = (ret >= size) ? size - 1 : ret;
+ memcpy(dst, src, len);
+ dst[len] = '\0';
+ }
+
+ return ret;
+}
+
char *xstrdup(const char *str)
{
char *ret = strdup(str);
diff --git a/src/nvim/memory.h b/src/nvim/memory.h
index 2723a7ba80..accf293176 100644
--- a/src/nvim/memory.h
+++ b/src/nvim/memory.h
@@ -125,6 +125,19 @@ char *xstpcpy(char *restrict dst, const char *restrict src);
/// @param maxlen
char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen);
+/// xstrlcpy - Copy a %NUL terminated string into a sized buffer
+///
+/// Compatible with *BSD strlcpy: the result is always a valid
+/// NUL-terminated string that fits in the buffer (unless,
+/// of course, the buffer size is zero). It does not pad
+/// out the result like strncpy() does.
+///
+/// @param dst Where to copy the string to
+/// @param src Where to copy the string from
+/// @param size Size of destination buffer
+/// @return Length of the source string (i.e.: strlen(src))
+size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size);
+
/// Duplicates a chunk of memory using xmalloc
///
/// @see {xmalloc}
diff --git a/src/nvim/os/server.c b/src/nvim/os/server.c
index 9b5410c323..b2faa49a86 100644
--- a/src/nvim/os/server.c
+++ b/src/nvim/os/server.c
@@ -85,7 +85,11 @@ void server_start(char *endpoint, ChannelProtocol prot)
char addr[ADDRESS_MAX_SIZE];
// Trim to `ADDRESS_MAX_SIZE`
- strncpy(addr, endpoint, sizeof(addr));
+ if (xstrlcpy(addr, endpoint, sizeof(addr)) >= sizeof(addr)) {
+ // TODO(aktau): since this is not what the user wanted, perhaps we
+ // should return an error here
+ EMSG2("Address was too long, truncated to %s", addr);
+ }
// Check if the server already exists
if (map_has(cstr_t)(servers, addr)) {
@@ -111,7 +115,7 @@ void server_start(char *endpoint, ChannelProtocol prot)
}
// Extract the address part
- strncpy(ip, addr, addr_len);
+ xstrlcpy(ip, addr, addr_len);
int port = NEOVIM_DEFAULT_TCP_PORT;
@@ -119,7 +123,7 @@ void server_start(char *endpoint, ChannelProtocol prot)
char *port_end;
// Extract the port
port = strtol(ip_end + 1, &port_end, 10);
-
+
errno = 0;
if (errno != 0 || port == 0 || port > 0xffff) {
// Invalid port, treat as named pipe or unix socket
@@ -183,7 +187,7 @@ void server_stop(char *endpoint)
char addr[ADDRESS_MAX_SIZE];
// Trim to `ADDRESS_MAX_SIZE`
- strncpy(addr, endpoint, sizeof(addr));
+ xstrlcpy(addr, endpoint, sizeof(addr));
if ((server = map_get(cstr_t)(servers, addr)) == NULL) {
EMSG2("Not listening on %s", addr);