aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2014-12-06 20:19:08 -0500
committerJustin M. Keyes <justinkz@gmail.com>2014-12-06 20:19:08 -0500
commit01fc0efdcaeefab5510a9c37ba4dd28e5638263a (patch)
tree9f4864aa53657d5ce5932c509c28c3069340ca88 /src
parentdcee93f8eae0ecc246280e8eb67d89ffe7f26b4a (diff)
parentcea5092f40610216a83b6ab0a56d94295ce1e88a (diff)
downloadrneovim-01fc0efdcaeefab5510a9c37ba4dd28e5638263a.tar.gz
rneovim-01fc0efdcaeefab5510a9c37ba4dd28e5638263a.tar.bz2
rneovim-01fc0efdcaeefab5510a9c37ba4dd28e5638263a.zip
Merge pull request #1341 from splinterofchaos/api-nul
Api: Improve Nul handling
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/buffer.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 982003a31a..0292e82038 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -132,7 +132,12 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer,
}
const char *bufstr = (char *) ml_get_buf(buf, (linenr_T) lnum, false);
- rv.items[i] = STRING_OBJ(cstr_to_string(bufstr));
+ Object str = STRING_OBJ(cstr_to_string(bufstr));
+
+ // Vim represents NULs as NLs, but this may confuse clients.
+ strchrsub(str.data.string.data, '\n', '\0');
+
+ rv.items[i] = str;
}
end:
@@ -201,7 +206,18 @@ void buffer_set_line_slice(Buffer buffer,
}
String l = replacement.items[i].data.string;
- lines[i] = xmemdupz(l.data, l.size);
+
+ // Fill lines[i] with l's contents. Disallow newlines in the middle of a
+ // line and convert NULs to newlines to avoid truncation.
+ lines[i] = xmallocz(l.size);
+ for (size_t j = 0; j < l.size; j++) {
+ if (l.data[j] == '\n') {
+ api_set_error(err, Exception, _("string cannot contain newlines"));
+ new_len = i + 1;
+ goto end;
+ }
+ lines[i][j] = (char) (l.data[j] == '\0' ? '\n' : l.data[j]);
+ }
}
try_start();