mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 11:28:22 +00:00
vim-patch:8.1.0755: error message for get() on a Blob with invalid index
Problem: Error message for get() on a Blob with invalid index.
Solution: Return an empty Blob, like get() on a List does.
2ea773b468
This commit is contained in:
@@ -2809,14 +2809,18 @@ static void f_get(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
|||||||
|
|
||||||
if (argvars[0].v_type == VAR_BLOB) {
|
if (argvars[0].v_type == VAR_BLOB) {
|
||||||
bool error = false;
|
bool error = false;
|
||||||
const int idx = tv_get_number_chk(&argvars[1], &error);
|
int idx = tv_get_number_chk(&argvars[1], &error);
|
||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
rettv->v_type = VAR_NUMBER;
|
rettv->v_type = VAR_NUMBER;
|
||||||
if (idx >= tv_blob_len(argvars[0].vval.v_blob)) {
|
if (idx < 0) {
|
||||||
EMSGN(_(e_blobidx), idx);
|
idx = tv_blob_len(argvars[0].vval.v_blob) + idx;
|
||||||
|
}
|
||||||
|
if (idx < 0 || idx >= tv_blob_len(argvars[0].vval.v_blob)) {
|
||||||
|
rettv->vval.v_number = -1;
|
||||||
} else {
|
} else {
|
||||||
rettv->vval.v_number = tv_blob_get(argvars[0].vval.v_blob, idx);
|
rettv->vval.v_number = tv_blob_get(argvars[0].vval.v_blob, idx);
|
||||||
|
tv = rettv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (argvars[0].v_type == VAR_LIST) {
|
} else if (argvars[0].v_type == VAR_LIST) {
|
||||||
|
@@ -20,7 +20,6 @@ func Test_blob_create()
|
|||||||
|
|
||||||
call assert_equal(0xDE, get(b, 0))
|
call assert_equal(0xDE, get(b, 0))
|
||||||
call assert_equal(0xEF, get(b, 3))
|
call assert_equal(0xEF, get(b, 3))
|
||||||
call assert_fails('let x = get(b, 4)')
|
|
||||||
|
|
||||||
call assert_fails('let b = 0z1', 'E973:')
|
call assert_fails('let b = 0z1', 'E973:')
|
||||||
call assert_fails('let b = 0z1x', 'E973:')
|
call assert_fails('let b = 0z1x', 'E973:')
|
||||||
@@ -79,6 +78,18 @@ func Test_blob_get_range()
|
|||||||
call assert_equal(0z, b[5:6])
|
call assert_equal(0z, b[5:6])
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_blob_get()
|
||||||
|
let b = 0z0011223344
|
||||||
|
call assert_equal(0x00, get(b, 0))
|
||||||
|
call assert_equal(0x22, get(b, 2, 999))
|
||||||
|
call assert_equal(0x44, get(b, 4))
|
||||||
|
call assert_equal(0x44, get(b, -1))
|
||||||
|
call assert_equal(-1, get(b, 5))
|
||||||
|
call assert_equal(999, get(b, 5, 999))
|
||||||
|
call assert_equal(-1, get(b, -8))
|
||||||
|
call assert_equal(999, get(b, -8, 999))
|
||||||
|
endfunc
|
||||||
|
|
||||||
func Test_blob_to_string()
|
func Test_blob_to_string()
|
||||||
let b = 0zDEADBEEF
|
let b = 0zDEADBEEF
|
||||||
call assert_equal('[0xDE,0xAD,0xBE,0xEF]', string(b))
|
call assert_equal('[0xDE,0xAD,0xBE,0xEF]', string(b))
|
||||||
|
Reference in New Issue
Block a user