patch 9.0.0810: readblob() returns empty when trying to read too much
Problem: readblob() returns empty when trying to read too much.
Solution: Return what is available.
diff --git a/src/blob.c b/src/blob.c
index a94316c..311b87e 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -199,24 +199,32 @@
if (offset >= 0)
{
- if (size == -1)
+ // The size defaults to the whole file. If a size is given it is
+ // limited to not go past the end of the file.
+ if (size == -1 || (size > st.st_size - offset
+#ifdef S_ISCHR
+ && !S_ISCHR(st.st_mode)
+#endif
+ ))
// size may become negative, checked below
size = st.st_size - offset;
whence = SEEK_SET;
}
else
{
- if (size == -1)
+ // limit the offset to not go before the start of the file
+ if (-offset > st.st_size
+#ifdef S_ISCHR
+ && !S_ISCHR(st.st_mode)
+#endif
+ )
+ offset = -st.st_size;
+ // Size defaults to reading until the end of the file.
+ if (size == -1 || size > -offset)
size = -offset;
whence = SEEK_END;
}
- // Trying to read bytes that aren't there results in an empty blob, not an
- // error.
- if (size <= 0 || (
-#ifdef S_ISCHR
- !S_ISCHR(st.st_mode) &&
-#endif
- size > st.st_size))
+ if (size <= 0)
return OK;
if (offset != 0 && vim_fseek(fd, offset, whence) != 0)
return OK;
diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim
index 47315ab..4a957aa 100644
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -499,10 +499,17 @@
VAR br6 = readblob('Xblob', -3, 2)
call assert_equal(b[-3 : -2], br6)
+ #" reading past end of file, empty result
VAR br1e = readblob('Xblob', 10000)
call assert_equal(0z, br1e)
- VAR br2e = readblob('Xblob', -10000)
- call assert_equal(0z, br2e)
+
+ #" reading too much, result is truncated
+ VAR blong = readblob('Xblob', -1000)
+ call assert_equal(b, blong)
+ LET blong = readblob('Xblob', -10, 8)
+ call assert_equal(b, blong)
+ LET blong = readblob('Xblob', 0, 10)
+ call assert_equal(b, blong)
call delete('Xblob')
END
diff --git a/src/version.c b/src/version.c
index 94e5d5c..4017c47 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 810,
+/**/
809,
/**/
808,