diff options
-rw-r--r-- | .github/workflows/ci.yml | 46 | ||||
-rw-r--r-- | ci/common/suite.sh | 25 | ||||
-rwxr-xr-x | ci/run_lint.sh | 36 | ||||
-rwxr-xr-x | ci/run_tests.sh | 47 | ||||
-rw-r--r-- | runtime/doc/vim_diff.txt | 3 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 10 | ||||
-rw-r--r-- | src/nvim/globals.h | 2 | ||||
-rw-r--r-- | src/nvim/normal.c | 17 | ||||
-rw-r--r-- | src/nvim/ops.c | 48 | ||||
-rw-r--r-- | src/nvim/testdir/test_excmd.vim | 14 | ||||
-rw-r--r-- | src/nvim/testdir/test_normal.vim | 21 | ||||
-rw-r--r-- | src/nvim/testdir/test_put.vim | 44 | ||||
-rw-r--r-- | test/functional/legacy/excmd_spec.lua | 32 | ||||
-rw-r--r-- | test/functional/legacy/put_spec.lua | 45 |
14 files changed, 322 insertions, 68 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa77689d9f..34b73e8ef4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,13 +38,30 @@ jobs: path: | ${{ env.CACHE_NVIM_DEPS_DIR }} ~/.ccache - key: ${{ matrix.runner }}-lint-${{ matrix.cc }}-${{ hashFiles('cmake/*', 'third-party/**', '**/CMakeLists.txt') }}-${{ github.base_ref }} + key: lint-${{ hashFiles('cmake/*', 'third-party/**', '**/CMakeLists.txt') }}-${{ github.base_ref }} - name: Build third-party run: ./ci/before_script.sh - - name: Run lint - run: ./ci/script.sh + - if: "!cancelled()" + name: clint + run: ./ci/run_lint.sh clint + + - if: "!cancelled()" + name: lualint + run: ./ci/run_lint.sh lualint + + - if: "!cancelled()" + name: pylint + run: ./ci/run_lint.sh pylint + + - if: "!cancelled()" + name: shlint + run: ./ci/run_lint.sh shlint + + - if: "!cancelled()" + name: single-includes + run: ./ci/run_lint.sh single-includes - name: Cache dependencies if: ${{ success() }} @@ -126,8 +143,7 @@ jobs: brew install automake ccache perl cpanminus ninja - name: Setup interpreter packages - run: | - ./ci/install.sh + run: ./ci/install.sh - name: Cache dependencies uses: actions/cache@v2 @@ -140,8 +156,24 @@ jobs: - name: Build third-party run: ./ci/before_script.sh - - name: Build and test - run: ./ci/script.sh + - name: Build + run: ./ci/run_tests.sh build + + - if: matrix.flavor != 'tsan' && matrix.flavor != 'functionaltest-lua' && !cancelled() + name: Unittests + run: ./ci/run_tests.sh unittests + + - if: matrix.flavor != 'tsan' && !cancelled() + name: Functionaltests + run: ./ci/run_tests.sh functionaltests + + - if: "!cancelled()" + name: Oldtests + run: ./ci/run_tests.sh oldtests + + - if: "!cancelled()" + name: Install nvim + run: ./ci/run_tests.sh install_nvim - name: Cache dependencies if: ${{ success() }} diff --git a/ci/common/suite.sh b/ci/common/suite.sh index f33f8b89d1..5110e22ec2 100644 --- a/ci/common/suite.sh +++ b/ci/common/suite.sh @@ -11,37 +11,17 @@ FAIL_SUMMARY="" END_MARKER="$BUILD_DIR/.tests_finished" FAIL_SUMMARY_FILE="$BUILD_DIR/.test_errors" -ci_fold() { - if test "$GITHUB_ACTIONS" = "true"; then - local action="$1" - local name="$2" - case "$action" in - start) - echo "::group::${name}" - ;; - end) - echo "::endgroup::" - ;; - *) - :;; - esac - fi -} - enter_suite() { FAILED=0 rm -f "${END_MARKER}" local suite_name="$1" export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE}/$suite_name" - ci_fold "start" "$suite_name" } exit_suite() { if test $FAILED -ne 0 ; then echo "Suite ${NVIM_TEST_CURRENT_SUITE} failed, summary:" echo "${FAIL_SUMMARY}" - else - ci_fold "end" "" fi export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE%/*}" FAILED=0 @@ -66,6 +46,11 @@ ended_successfully() { if test -f "${FAIL_SUMMARY_FILE}" ; then echo 'Test failed, complete summary:' cat "${FAIL_SUMMARY_FILE}" + + if [[ "$GITHUB_ACTIONS" == "true" ]]; then + rm -f "$FAIL_SUMMARY_FILE" + fi + return 1 fi if ! test -f "${END_MARKER}" ; then diff --git a/ci/run_lint.sh b/ci/run_lint.sh index 607ffa233a..2fea7a40c0 100755 --- a/ci/run_lint.sh +++ b/ci/run_lint.sh @@ -8,10 +8,34 @@ CI_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CI_DIR}/common/build.sh" source "${CI_DIR}/common/suite.sh" -run_suite 'make clint-full' 'clint' -run_suite 'make lualint' 'lualint' -run_suite 'make pylint' 'pylint' -run_suite 'make shlint' 'shlint' -run_suite 'make check-single-includes' 'single-includes' +if [[ "$GITHUB_ACTIONS" != "true" ]]; then + run_suite 'make clint-full' 'clint' + run_suite 'make lualint' 'lualint' + run_suite 'make pylint' 'pylint' + run_suite 'make shlint' 'shlint' + run_suite 'make check-single-includes' 'single-includes' -end_tests + end_tests +else + case "$1" in + clint) + run_suite 'make clint-full' 'clint' + ;; + lualint) + run_suite 'make lualint' 'lualint' + ;; + pylint) + run_suite 'make pylint' 'pylint' + ;; + shlint) + run_suite 'make shlint' 'shlint' + ;; + single-includes) + run_suite 'make check-single-includes' 'single-includes' + ;; + *) + :;; + esac + + end_tests +fi diff --git a/ci/run_tests.sh b/ci/run_tests.sh index 1baeb090a8..ae85246ab6 100755 --- a/ci/run_tests.sh +++ b/ci/run_tests.sh @@ -8,17 +8,42 @@ source "${CI_DIR}/common/build.sh" source "${CI_DIR}/common/test.sh" source "${CI_DIR}/common/suite.sh" -run_suite 'build_nvim' 'build' -if test "$CLANG_SANITIZER" != "TSAN"; then - # Additional threads are only created when the builtin UI starts, which - # doesn't happen in the unit/functional tests - if test "${FUNCTIONALTEST}" != "functionaltest-lua"; then - run_suite run_unittests unittests +if [[ "$GITHUB_ACTIONS" != "true" ]]; then + run_suite 'build_nvim' 'build' + + if test "$CLANG_SANITIZER" != "TSAN"; then + # Additional threads are only created when the builtin UI starts, which + # doesn't happen in the unit/functional tests + if test "${FUNCTIONALTEST}" != "functionaltest-lua"; then + run_suite run_unittests unittests + fi + run_suite run_functionaltests functionaltests fi - run_suite run_functionaltests functionaltests -fi -run_suite run_oldtests oldtests -run_suite install_nvim install_nvim + run_suite run_oldtests oldtests + run_suite install_nvim install_nvim -end_tests + end_tests +else + case "$1" in + build) + run_suite 'build_nvim' 'build' + ;; + unittests) + run_suite 'run_unittests' 'unittests' + ;; + functionaltests) + run_suite 'run_functionaltests' 'functionaltests' + ;; + oldtests) + run_suite 'run_oldtests' 'oldtests' + ;; + install_nvim) + run_suite 'install_nvim' 'install_nvim' + ;; + *) + :;; + esac + + end_tests +fi diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 385ce34d70..5ea6a9c5dd 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -494,6 +494,9 @@ Eval: *js_encode()* *js_decode()* *v:none* (used by Vim to represent JavaScript "undefined"); use |v:null| instead. + *v:sizeofint* + *v:sizeoflong* + *v:sizeofpointer* Events: *SigUSR1* Use |Signal| to detect `SIGUSR1` signal instead. diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 9991584862..e8b8dc799c 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -4141,7 +4141,11 @@ static linenr_T get_address(exarg_T *eap, char_u **ptr, cmd_addr_T addr_type, in if (!ascii_isdigit(*cmd)) { // '+' is '+1', but '+0' is not '+1' n = 1; } else { - n = getdigits(&cmd, true, 0); + n = getdigits(&cmd, false, MAXLNUM); + if (n == MAXLNUM) { + emsg(_(e_line_number_out_of_range)); + goto error; + } } if (addr_type == ADDR_TABS_RELATIVE) { @@ -4160,6 +4164,10 @@ static linenr_T get_address(exarg_T *eap, char_u **ptr, cmd_addr_T addr_type, in if (i == '-') { lnum -= n; } else { + if (n >= LONG_MAX - lnum) { + emsg(_(e_line_number_out_of_range)); + goto error; + } lnum += n; } } diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 98a38c5fe2..5aa564623f 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -1002,6 +1002,8 @@ EXTERN char e_cannot_define_autocommands_for_all_events[] INIT(= N_("E1155: Cann EXTERN char e_resulting_text_too_long[] INIT(= N_("E1240: Resulting text too long")); +EXTERN char e_line_number_out_of_range[] INIT(= N_("E1247: Line number out of range")); + EXTERN char e_highlight_group_name_too_long[] INIT(= N_("E1249: Highlight group name too long")); EXTERN char top_bot_msg[] INIT(= N_("search hit TOP, continuing at BOTTOM")); diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 21c465434a..7fe6469527 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -824,15 +824,12 @@ static bool normal_get_command_count(NormalState *s) if (s->c == K_DEL || s->c == K_KDEL) { s->ca.count0 /= 10; del_from_showcmd(4); // delete the digit and ~@% + } else if (s->ca.count0 > 99999999L) { + s->ca.count0 = 999999999L; } else { s->ca.count0 = s->ca.count0 * 10 + (s->c - '0'); } - if (s->ca.count0 < 0) { - // overflow - s->ca.count0 = 999999999L; - } - // Set v:count here, when called from main() and not a stuffed // command, so that v:count can be used in an expression mapping // right after the count. Do set it for redo. @@ -1046,14 +1043,14 @@ static int normal_execute(VimState *state, int key) // If you give a count before AND after the operator, they are // multiplied. if (s->ca.count0) { - s->ca.count0 = (long)((uint64_t)s->ca.count0 * (uint64_t)s->ca.opcount); + if (s->ca.opcount >= 999999999L / s->ca.count0) { + s->ca.count0 = 999999999L; + } else { + s->ca.count0 *= s->ca.opcount; + } } else { s->ca.count0 = s->ca.opcount; } - if (s->ca.count0 < 0) { - // overflow - s->ca.count0 = 999999999L; - } } // Always remember the count. It will be set to zero (on the next call, diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 18facef13c..b5c7020dee 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -3314,18 +3314,28 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } } - // insert the new text + // Insert the new text. + // First check for multiplication overflow. + if (yanklen + spaces != 0 + && count > ((INT_MAX - (bd.startspaces + bd.endspaces)) / (yanklen + spaces))) { + emsg(_(e_resulting_text_too_long)); + break; + } + totlen = (size_t)(count * (yanklen + spaces) + bd.startspaces + bd.endspaces); int addcount = (int)totlen + lines_appended; newp = (char_u *)xmalloc(totlen + oldlen + 1); + // copy part up to cursor to new line ptr = newp; memmove(ptr, oldp, (size_t)bd.textcol); ptr += bd.textcol; + // may insert some spaces before the new text memset(ptr, ' ', (size_t)bd.startspaces); ptr += bd.startspaces; + // insert the new text for (long j = 0; j < count; j++) { memmove(ptr, y_array[i], (size_t)yanklen); @@ -3339,9 +3349,11 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) addcount -= spaces; } } + // may insert some spaces after the new text memset(ptr, ' ', (size_t)bd.endspaces); ptr += bd.endspaces; + // move the text after the cursor to the end of the line. int columns = (int)oldlen - bd.textcol - delcount + 1; assert(columns >= 0); @@ -3430,10 +3442,18 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } } - do { + if (count == 0 || yanklen == 0) { + if (VIsual_active) { + lnum = end_lnum; + } + } else if (count > INT_MAX / yanklen) { + // multiplication overflow + emsg(_(e_resulting_text_too_long)); + } else { totlen = (size_t)(count * yanklen); - if (totlen > 0) { + do { oldp = ml_get(lnum); + oldlen = STRLEN(oldp); if (lnum > start_lnum) { pos_T pos = { .lnum = lnum, @@ -3444,11 +3464,11 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) col = MAXCOL; } } - if (VIsual_active && col > (int)STRLEN(oldp)) { + if (VIsual_active && col > (colnr_T)oldlen) { lnum++; continue; } - newp = (char_u *)xmalloc((size_t)(STRLEN(oldp) + totlen + 1)); + newp = (char_u *)xmalloc(totlen + oldlen + 1); memmove(newp, oldp, (size_t)col); ptr = newp + col; for (i = 0; i < (size_t)count; i++) { @@ -3470,14 +3490,14 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) changed_bytes(lnum, col); extmark_splice_cols(curbuf, (int)lnum-1, col, 0, (int)totlen, kExtmarkUndo); - } - if (VIsual_active) { - lnum++; - } - } while (VIsual_active && lnum <= end_lnum); + if (VIsual_active) { + lnum++; + } + } while (VIsual_active && lnum <= end_lnum); - if (VIsual_active) { // reset lnum to the last visual line - lnum--; + if (VIsual_active) { // reset lnum to the last visual line + lnum--; + } } // put '] at the first byte of the last character @@ -5691,7 +5711,9 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, const char_u *str // When appending, copy the previous line and free it after. size_t extra = append ? STRLEN(pp[--lnum]) : 0; char_u *s = xmallocz(line_len + extra); - memcpy(s, pp[lnum], extra); + if (extra > 0) { + memcpy(s, pp[lnum], extra); + } memcpy(s + extra, start, line_len); size_t s_len = extra + line_len; diff --git a/src/nvim/testdir/test_excmd.vim b/src/nvim/testdir/test_excmd.vim index bbf8b4dfc8..8055a51a11 100644 --- a/src/nvim/testdir/test_excmd.vim +++ b/src/nvim/testdir/test_excmd.vim @@ -409,4 +409,18 @@ func Test_not_break_expression_register() call assert_equal('1+1', getreg('=', 1)) endfunc +func Test_address_line_overflow() + throw 'Skipped: v:sizeoflong is N/A' " use legacy/excmd_spec.lua instead + + if v:sizeoflong < 8 + throw 'Skipped: only works with 64 bit long ints' + endif + new + call setline(1, 'text') + call assert_fails('|.44444444444444444444444', 'E1247:') + call assert_fails('|.9223372036854775806', 'E1247:') + bwipe! +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index 5b7cf6fee5..e8eebb3fdd 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -2779,4 +2779,25 @@ func Test_normal_gj_on_extra_wide_char() bw! endfunc +func Test_normal_count_out_of_range() + new + call setline(1, 'text') + normal 44444444444| + call assert_equal(999999999, v:count) + normal 444444444444| + call assert_equal(999999999, v:count) + normal 4444444444444| + call assert_equal(999999999, v:count) + normal 4444444444444444444| + call assert_equal(999999999, v:count) + + normal 9y99999999| + call assert_equal(899999991, v:count) + normal 10y99999999| + call assert_equal(999999999, v:count) + normal 44444444444y44444444444| + call assert_equal(999999999, v:count) + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_put.vim b/src/nvim/testdir/test_put.vim index ed76709a56..65232175c6 100644 --- a/src/nvim/testdir/test_put.vim +++ b/src/nvim/testdir/test_put.vim @@ -138,6 +138,50 @@ func Test_p_with_count_leaves_mark_at_end() bwipe! endfunc +func Test_very_large_count() + new + " total put-length (21474837 * 100) brings 32 bit int overflow + let @" = repeat('x', 100) + call assert_fails('norm 21474837p', 'E1240:') + bwipe! +endfunc + +func Test_very_large_count_64bit() + throw 'Skipped: v:sizeoflong is N/A' " use legacy/put_spec.lua instead + + if v:sizeoflong < 8 + throw 'Skipped: only works with 64 bit long ints' + endif + + new + let @" = repeat('x', 100) + call assert_fails('norm 999999999p', 'E1240:') + bwipe! +endfunc + +func Test_very_large_count_block() + new + " total put-length (21474837 * 100) brings 32 bit int overflow + call setline(1, repeat('x', 100)) + exe "norm \<C-V>99ly" + call assert_fails('norm 21474837p', 'E1240:') + bwipe! +endfunc + +func Test_very_large_count_block_64bit() + throw 'Skipped: v:sizeoflong is N/A' " use legacy/put_spec.lua instead + + if v:sizeoflong < 8 + throw 'Skipped: only works with 64 bit long ints' + endif + + new + call setline(1, repeat('x', 100)) + exe "norm \<C-V>$y" + call assert_fails('norm 999999999p', 'E1240:') + bwipe! +endfunc + func Test_put_above_first_line() new let @" = 'text' diff --git a/test/functional/legacy/excmd_spec.lua b/test/functional/legacy/excmd_spec.lua new file mode 100644 index 0000000000..174f7d292e --- /dev/null +++ b/test/functional/legacy/excmd_spec.lua @@ -0,0 +1,32 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local meths = helpers.meths +local source = helpers.source +local eq = helpers.eq + +local function sizeoflong() + if not exec_lua('return pcall(require, "ffi")') then + pending('missing LuaJIT FFI') + end + return exec_lua('return require("ffi").sizeof(require("ffi").typeof("long"))') +end + +describe('Ex command', function() + before_each(clear) + after_each(function() eq({}, meths.get_vvar('errors')) end) + + it('checks for address line overflow', function() + if sizeoflong() < 8 then + pending('Skipped: only works with 64 bit long ints') + end + + source [[ + new + call setline(1, 'text') + call assert_fails('|.44444444444444444444444', 'E1247:') + call assert_fails('|.9223372036854775806', 'E1247:') + bwipe! + ]] + end) +end) diff --git a/test/functional/legacy/put_spec.lua b/test/functional/legacy/put_spec.lua new file mode 100644 index 0000000000..3ddf65490e --- /dev/null +++ b/test/functional/legacy/put_spec.lua @@ -0,0 +1,45 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local meths = helpers.meths +local source = helpers.source +local eq = helpers.eq + +local function sizeoflong() + if not exec_lua('return pcall(require, "ffi")') then + pending('missing LuaJIT FFI') + end + return exec_lua('return require("ffi").sizeof(require("ffi").typeof("long"))') +end + +describe('put', function() + before_each(clear) + after_each(function() eq({}, meths.get_vvar('errors')) end) + + it('very large count 64-bit', function() + if sizeoflong() < 8 then + pending('Skipped: only works with 64 bit long ints') + end + + source [[ + new + let @" = repeat('x', 100) + call assert_fails('norm 999999999p', 'E1240:') + bwipe! + ]] + end) + + it('very large count (visual block) 64-bit', function() + if sizeoflong() < 8 then + pending('Skipped: only works with 64 bit long ints') + end + + source [[ + new + call setline(1, repeat('x', 100)) + exe "norm \<C-V>$y" + call assert_fails('norm 999999999p', 'E1240:') + bwipe! + ]] + end) +end) |