From 2f80360e9acdb32f63633e8408c00de2ef972b3b Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sun, 5 Mar 2017 22:20:48 +0100 Subject: [PATCH 1/3] vim-patch:7.4.2220 Problem: printf() gives an error when the argument for %s is not a string. (Ozaki Kiichi) Solution: Behave like invoking string() on the argument. (Ken Takata) https://github.com/vim/vim/commit/e5a8f35b4286135f3469f3b00a6c2220553d9658 --- src/nvim/strings.c | 29 ++++++++++++++++++++++++----- src/nvim/testdir/test_expr.vim | 27 +++++++++++++++++++++++++++ src/nvim/version.c | 2 +- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 5b6fbf75a9..6bfb63251c 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -45,6 +45,7 @@ #include "nvim/window.h" #include "nvim/os/os.h" #include "nvim/os/shell.h" +#include "nvim/eval/encode.h" /* * Copy "string" into newly allocated memory. @@ -588,18 +589,30 @@ static varnumber_T tv_nr(typval_T *tvs, int *idxp) /// value. /// @param[in,out] idxp Index in a list. Will be incremented. /// +/// @param[out] tofree If "tofree" is NULL get_tv_string_chk() is used. Some +/// types (e.g. List) are not converted to a string. If +/// "tofree" is not NULL encode_tv2echo() is used. All +/// types are converted to a string with the same format as +/// ":echo". The caller must free "*tofree". +/// /// @return String value or NULL in case of error. -static char *tv_str(typval_T *tvs, int *idxp) - FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree) + FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2) + FUNC_ATTR_WARN_UNUSED_RESULT { int idx = *idxp - 1; - char *s = NULL; + char *s = NULL; if (tvs[idx].v_type == VAR_UNKNOWN) { EMSG(_(e_printf)); } else { (*idxp)++; - s = (char *)get_tv_string_chk(&tvs[idx]); + if (tofree != NULL) { + s = encode_tv2echo(&tvs[idx], NULL); + *tofree = (char_u *)s; + } else { + s = (char *)get_tv_string_chk(&tvs[idx]); + } } return s; } @@ -813,6 +826,9 @@ int vim_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap, // current conversion specifier character char fmt_spec = '\0'; + // buffer for 's' and 'S' specs + char_u *tofree = NULL; + p++; // skip '%' // parse flags @@ -919,7 +935,8 @@ int vim_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap, case 's': case 'S': - str_arg = tvs ? tv_str(tvs, &arg_idx) : va_arg(ap, char *); + str_arg = tvs ? tv_str(tvs, &arg_idx, &tofree) + : va_arg(ap, char *); if (!str_arg) { str_arg = "[NULL]"; str_arg_l = 6; @@ -1370,6 +1387,8 @@ int vim_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap, str_l += pn; } } + + xfree(tofree); } } diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index 7ceef2834f..5aeb934e3a 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -107,6 +107,33 @@ func Test_setmatches() call assert_equal(exp, getmatches()) endfunc +function Test_printf_spec_s() + " number + call assert_equal("1234567890", printf('%s', 1234567890)) + + " string + call assert_equal("abcdefgi", printf('%s', "abcdefgi")) + + " float + if has('float') + call assert_equal("1.23", printf('%s', 1.23)) + endif + + " list + let value = [1, 'two', ['three', 4]] + call assert_equal(string(value), printf('%s', value)) + + " dict + let value = {'key1' : 'value1', 'key2' : ['list', 'value'], 'key3' : {'dict' : 'value'}} + call assert_equal(string(value), printf('%s', value)) + + " funcref + call assert_equal('printf', printf('%s', function('printf'))) + + " partial + call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s']))) +endfunc + func Test_substitute_expr() let g:val = 'XXX' call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', '')) diff --git a/src/nvim/version.c b/src/nvim/version.c index 8fd9b4b74f..1e7785507f 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -220,7 +220,7 @@ static int included_patches[] = { // 2223, // 2222, // 2221, - // 2220, + 2220, 2219, // 2218 NA 2217, From 04b91d6b89f12df314e0d209afe2d5f872c5caf2 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Mon, 6 Mar 2017 21:33:55 +0100 Subject: [PATCH 2/3] strings.c: Fix problems found during code review. --- src/nvim/strings.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 6bfb63251c..0dc27061f4 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -588,17 +588,14 @@ static varnumber_T tv_nr(typval_T *tvs, int *idxp) /// @param[in] tvs List of VimL values. List is terminated by VAR_UNKNOWN /// value. /// @param[in,out] idxp Index in a list. Will be incremented. -/// -/// @param[out] tofree If "tofree" is NULL get_tv_string_chk() is used. Some -/// types (e.g. List) are not converted to a string. If -/// "tofree" is not NULL encode_tv2echo() is used. All -/// types are converted to a string with the same format as -/// ":echo". The caller must free "*tofree". +/// @param[out] tofree If the idxp entry in tvs is not a String or a Number, +/// it will be converted to String in the same format +/// as ":echo" and stored in "*tofree". The caller must +/// free "*tofree". /// /// @return String value or NULL in case of error. -static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree) - FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2) - FUNC_ATTR_WARN_UNUSED_RESULT +static char *tv_str(typval_T *tvs, int *idxp, char ** const tofree) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { int idx = *idxp - 1; char *s = NULL; @@ -607,11 +604,12 @@ static char *tv_str(typval_T *tvs, int *idxp, char_u **tofree) EMSG(_(e_printf)); } else { (*idxp)++; - if (tofree != NULL) { - s = encode_tv2echo(&tvs[idx], NULL); - *tofree = (char_u *)s; - } else { + if (tvs[idx].v_type == VAR_STRING || tvs[idx].v_type == VAR_NUMBER) { s = (char *)get_tv_string_chk(&tvs[idx]); + *tofree = NULL; + } else { + s = encode_tv2echo(&tvs[idx], NULL); + *tofree = s; } } return s; @@ -827,7 +825,7 @@ int vim_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap, char fmt_spec = '\0'; // buffer for 's' and 'S' specs - char_u *tofree = NULL; + char *tofree = NULL; p++; // skip '%' From b9cea7f13fe706384fc34310252aa6ceaa6ce5fe Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Tue, 7 Mar 2017 07:56:51 +0100 Subject: [PATCH 3/3] vim-patch:7.4.2265 Problem: printf() isn't tested much. Solution: Add more tests for printf(). (Dominique Pelle) https://github.com/vim/vim/commit/76efafba2af36ae5f6c7b79b56c537fcbcdb386c --- src/nvim/testdir/test_expr.vim | 91 ++++++++++++++++++++++++++++++++-- src/nvim/version.c | 2 +- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index 5aeb934e3a..7f7811dc7a 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -115,9 +115,7 @@ function Test_printf_spec_s() call assert_equal("abcdefgi", printf('%s', "abcdefgi")) " float - if has('float') - call assert_equal("1.23", printf('%s', 1.23)) - endif + call assert_equal("1.23", printf('%s', 1.23)) " list let value = [1, 'two', ['three', 4]] @@ -134,6 +132,93 @@ function Test_printf_spec_s() call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s']))) endfunc +function Test_printf_misc() + call assert_equal('123', printf('%d', 123)) + call assert_equal('123', printf('%i', 123)) + call assert_equal('123', printf('%D', 123)) + call assert_equal('123', printf('%U', 123)) + call assert_equal('173', printf('%o', 123)) + call assert_equal('173', printf('%O', 123)) + call assert_equal('7b', printf('%x', 123)) + call assert_equal('7B', printf('%X', 123)) + call assert_equal('{', printf('%c', 123)) + call assert_equal('abc', printf('%s', 'abc')) + call assert_equal('abc', printf('%S', 'abc')) + + call assert_equal('+123', printf('%+d', 123)) + call assert_equal('-123', printf('%+d', -123)) + call assert_equal('+123', printf('%+ d', 123)) + call assert_equal(' 123', printf('% d', 123)) + call assert_equal(' 123', printf('% d', 123)) + call assert_equal('-123', printf('% d', -123)) + + call assert_equal('00123', printf('%.*d', 5, 123)) + call assert_equal(' 123', printf('% *d', 5, 123)) + call assert_equal(' +123', printf('%+ *d', 5, 123)) + + call assert_equal('123', printf('%2d', 123)) + call assert_equal(' 123', printf('%5d', 123)) + call assert_equal('00123', printf('%05d', 123)) + call assert_equal('123 ', printf('%-5d', 123)) + call assert_equal('0x7b', printf('%#x', 123)) + call assert_equal('0X7B', printf('%#X', 123)) + call assert_equal('0173', printf('%#o', 123)) + call assert_equal('0173', printf('%#O', 123)) + call assert_equal('abc', printf('%#s', 'abc')) + call assert_equal('abc', printf('%#S', 'abc')) + + call assert_equal(' 00123', printf('%6.5d', 123)) + call assert_equal(' 0007b', printf('%6.5x', 123)) + + call assert_equal('abc', printf('%2s', 'abc')) + call assert_equal('abc', printf('%2S', 'abc')) + call assert_equal('abc', printf('%.4s', 'abc')) + call assert_equal('abc', printf('%.4S', 'abc')) + call assert_equal('ab', printf('%.2s', 'abc')) + call assert_equal('ab', printf('%.2S', 'abc')) + call assert_equal('', printf('%.0s', 'abc')) + call assert_equal('', printf('%.s', 'abc')) + call assert_equal(' abc', printf('%4s', 'abc')) + call assert_equal(' abc', printf('%4S', 'abc')) + call assert_equal('0abc', printf('%04s', 'abc')) + call assert_equal('0abc', printf('%04S', 'abc')) + call assert_equal('abc ', printf('%-4s', 'abc')) + call assert_equal('abc ', printf('%-4S', 'abc')) + + call assert_equal('1%', printf('%d%%', 1)) +endfunc + +function Test_printf_float() + call assert_equal('1.230000', printf('%f', 1.23)) + call assert_equal('1.230000', printf('%F', 1.23)) + call assert_equal('1.23', printf('%g', 1.23)) + call assert_equal('1.23', printf('%G', 1.23)) + call assert_equal('1.230000e+00', printf('%e', 1.23)) + call assert_equal('1.230000E+00', printf('%E', 1.23)) + call assert_equal('1.200000e-02', printf('%e', 0.012)) + call assert_equal('-1.200000e-02', printf('%e', -0.012)) + call assert_equal('1.2', printf('%.1f', 1.23)) + + call assert_equal('inf', printf('%f', 1.0/0.0)) + + " This prints inf but shouldn't it print -inf instead? + call assert_match('^-\?inf$', printf('%f', -1.0/0.0)) + + " This prints -nan but shouldn't it print nan instead? + call assert_match('^-\?nan$', printf('%f', sqrt(-1.0))) + call assert_match('^-\?nan$', printf('%f', 0.0/0.0)) + + call assert_fails('echo printf("%f", "a")', 'E807:') +endfunc + +function Test_printf_errors() + call assert_fails('echo printf("%d", {})', 'E728:') + call assert_fails('echo printf("%d", [])', 'E745:') + call assert_fails('echo printf("%d", 1, 2)', 'E767:') + call assert_fails('echo printf("%*d", 1)', 'E766:') + call assert_fails('echo printf("%d", 1.2)', 'E805:') +endfunc + func Test_substitute_expr() let g:val = 'XXX' call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', '')) diff --git a/src/nvim/version.c b/src/nvim/version.c index 1e7785507f..9c816457c1 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -175,7 +175,7 @@ static int included_patches[] = { // 2268, // 2267 NA // 2266, - // 2265, + 2265, // 2264, // 2263, // 2262 NA