Mercurial > vim
comparison src/list.c @ 28273:fff70771d4bb v8.2.4662
patch 8.2.4662: no error for using out of range list index
Commit: https://github.com/vim/vim/commit/22ebd172e48ba060c8a7bae3dbf6480b7596d937
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Apr 1 15:26:58 2022 +0100
patch 8.2.4662: no error for using out of range list index
Problem: No error for using out of range list index.
Solution: Check list index at script level like in compiled function.
(closes #10051)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 01 Apr 2022 16:30:03 +0200 |
parents | 183ae001d99d |
children | 175eacde28b8 |
comparison
equal
deleted
inserted
replaced
28272:bb583103b95b | 28273:fff70771d4bb |
---|---|
758 } | 758 } |
759 } | 759 } |
760 | 760 |
761 /* | 761 /* |
762 * Get the list item in "l" with index "n1". "n1" is adjusted if needed. | 762 * Get the list item in "l" with index "n1". "n1" is adjusted if needed. |
763 * In Vim9, it is at the end of the list, add an item. | 763 * In Vim9, it is at the end of the list, add an item if "can_append" is TRUE. |
764 * Return NULL if there is no such item. | 764 * Return NULL if there is no such item. |
765 */ | 765 */ |
766 listitem_T * | 766 listitem_T * |
767 check_range_index_one(list_T *l, long *n1, int quiet) | 767 check_range_index_one(list_T *l, long *n1, int can_append, int quiet) |
768 { | 768 { |
769 listitem_T *li = list_find_index(l, n1); | 769 long orig_n1 = *n1; |
770 listitem_T *li = list_find_index(l, n1); | |
770 | 771 |
771 if (li == NULL) | 772 if (li == NULL) |
772 { | 773 { |
773 // Vim9: Allow for adding an item at the end. | 774 // Vim9: Allow for adding an item at the end. |
774 if (in_vim9script() && *n1 == l->lv_len && l->lv_lock == 0) | 775 if (can_append && in_vim9script() |
776 && *n1 == l->lv_len && l->lv_lock == 0) | |
775 { | 777 { |
776 list_append_number(l, 0); | 778 list_append_number(l, 0); |
777 li = list_find_index(l, n1); | 779 li = list_find_index(l, n1); |
778 } | 780 } |
779 if (li == NULL) | 781 if (li == NULL) |
780 { | 782 { |
781 if (!quiet) | 783 if (!quiet) |
782 semsg(_(e_list_index_out_of_range_nr), *n1); | 784 semsg(_(e_list_index_out_of_range_nr), orig_n1); |
783 return NULL; | 785 return NULL; |
784 } | 786 } |
785 } | 787 } |
786 return li; | 788 return li; |
787 } | 789 } |