diff options
author | ZyX <kp-pav@yandex.ru> | 2017-04-12 00:31:01 +0300 |
---|---|---|
committer | ZyX <kp-pav@yandex.ru> | 2017-04-12 00:31:01 +0300 |
commit | 1d7fde39a6927d01de74aefb540ad445bfdfbfda (patch) | |
tree | e27d52063b322afcbcf9e1ab34153730a51bd697 /src/nvim/api/buffer.c | |
parent | 1bd39fb8d009f5ff62023d0f2fe86bbcdaeb3abc (diff) | |
download | rneovim-1d7fde39a6927d01de74aefb540ad445bfdfbfda.tar.gz rneovim-1d7fde39a6927d01de74aefb540ad445bfdfbfda.tar.bz2 rneovim-1d7fde39a6927d01de74aefb540ad445bfdfbfda.zip |
api/buffer: Validate replacement array in a separate cycle
Should not really change anything, but code should be more efficient by using
more optimized libc functions (memchrsub is not libc, but it uses memchr) in
place of a cycle.
Diffstat (limited to 'src/nvim/api/buffer.c')
-rw-r--r-- | src/nvim/api/buffer.c | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 611f29f1f2..0a7b7982e2 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -292,6 +292,23 @@ void nvim_buf_set_lines(uint64_t channel_id, return; } + for (size_t i = 0; i < replacement.size; i++) { + if (replacement.items[i].type != kObjectTypeString) { + api_set_error(err, + Validation, + _("All items in the replacement array must be strings")); + return; + } + // Disallow newlines in the middle of the line. + if (channel_id != VIML_INTERNAL_CALL) { + const String l = replacement.items[i].data.string; + if (memchr(l.data, NL, l.size)) { + api_set_error(err, Validation, _("string cannot contain newlines")); + return; + } + } + } + win_T *save_curwin = NULL; tabpage_T *save_curtab = NULL; size_t new_len = replacement.size; @@ -300,26 +317,12 @@ void nvim_buf_set_lines(uint64_t channel_id, char **lines = (new_len != 0) ? xcalloc(new_len, sizeof(char *)) : NULL; for (size_t i = 0; i < new_len; i++) { - if (replacement.items[i].type != kObjectTypeString) { - api_set_error(err, - Validation, - _("All items in the replacement array must be strings")); - goto end; - } - - String l = replacement.items[i].data.string; + const String l = replacement.items[i].data.string; - // 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' && channel_id != VIML_INTERNAL_CALL) { - 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]); - } + // Fill lines[i] with l's contents. Convert NULs to newlines as required by + // NL-used-for-NUL. + lines[i] = xmemdupz(l.data, l.size); + memchrsub(lines[i], NUL, NL, l.size); } try_start(); |