changeset 15494:74a8f44b5322 v8.1.0755

patch 8.1.0755: error message for get() on a Blob with invalid index commit https://github.com/vim/vim/commit/2ea773b468a1143214c2f12b91ab5e1e7abb4a14 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jan 15 22:16:42 2019 +0100 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.
author Bram Moolenaar <Bram@vim.org>
date Tue, 15 Jan 2019 22:30:06 +0100
parents 93691fbecb9a
children 617ca1eb76f8
files src/evalfunc.c src/testdir/test_blob.vim src/version.c
diffstat 3 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -4408,10 +4408,15 @@ f_get(typval_T *argvars, typval_T *rettv
 	if (!error)
 	{
 	    rettv->v_type = VAR_NUMBER;
-	    if (idx >= blob_len(argvars[0].vval.v_blob))
-		semsg(_(e_blobidx), idx);
+	    if (idx < 0)
+		idx = blob_len(argvars[0].vval.v_blob) + idx;
+	    if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
+		rettv->vval.v_number = -1;
 	    else
+	    {
 		rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
+		tv = rettv;
+	    }
 	}
     }
     else if (argvars[0].v_type == VAR_LIST)
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -20,7 +20,6 @@ func Test_blob_create()
 
   call assert_equal(0xDE, get(b, 0))
   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 = 0z1x', 'E973:')
@@ -79,6 +78,18 @@ func Test_blob_get_range()
   call assert_equal(0z, b[5:6])
 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()
   let b = 0zDEADBEEF
   call assert_equal('[0xDE,0xAD,0xBE,0xEF]', string(b))
--- a/src/version.c
+++ b/src/version.c
@@ -796,6 +796,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    755,
+/**/
     754,
 /**/
     753,