From 563698b2dc3f280b6d0d1e9156d0f1d4a4f756c4 Mon Sep 17 00:00:00 2001 From: Nicolas Hillegeer Date: Fri, 30 May 2014 18:45:11 +0200 Subject: [PATCH] api: also NUL-terminate Strings made from cstrs I believe we can now mostly assume that all encountered String's data members are safe to pass into functions that accept C strings. That should simplify interop with C string code. --- src/nvim/api/private/helpers.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 11de50455b..d946e60dd4 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -352,19 +352,22 @@ tabpage_T * find_tab(Tabpage tabpage, Error *err) return rv; } -/// Copies a C string into a String (binary safe string, characters + length) +/// Copies a C string into a String (binary safe string, characters + length). +/// The resulting string is also NUL-terminated, to facilitate interoperating +/// with code using C strings. /// /// @param str the C string to copy -/// @return the resulting String, if the input string was NULL, then an +/// @return the resulting String, if the input string was NULL, an /// empty String is returned -String cstr_to_string(const char *str) { +String cstr_to_string(const char *str) +{ if (str == NULL) { return (String) STRING_INIT; } size_t len = strlen(str); return (String) { - .data = xmemdup(str, len), + .data = xmemdupz(str, len), .size = len }; }