Patch 9.0.0810

3 views
Skip to first unread message

Bram Moolenaar

unread,
Oct 21, 2022, 6:26:17 AM10/21/22
to vim...@googlegroups.com

Patch 9.0.0810
Problem: readblob() returns empty when trying to read too much.
Solution: Return what is available.
Files: runtime/doc/builtin.txt, src/blob.c, src/testdir/test_blob.vim


*** ../vim-9.0.0809/runtime/doc/builtin.txt 2022-10-20 13:28:43.773615380 +0100
--- runtime/doc/builtin.txt 2022-10-21 11:21:08.784981677 +0100
***************
*** 6865,6872 ****
readblob('/dev/ttyS0', 0, 10)
< When the file can't be opened an error message is given and
the result is an empty |Blob|.
! When trying to read bytes beyond the end of the file the
! result is an empty blob.
Also see |readfile()| and |writefile()|.


--- 6864,6873 ----
readblob('/dev/ttyS0', 0, 10)
< When the file can't be opened an error message is given and
the result is an empty |Blob|.
! When the offset is beyond the end of the file the result is an
! empty blob.
! When trying to read more bytes than are available the result
! is truncated.
Also see |readfile()| and |writefile()|.


*** ../vim-9.0.0809/src/blob.c 2022-10-20 13:28:43.777615365 +0100
--- src/blob.c 2022-10-21 11:11:30.697952099 +0100
***************
*** 199,222 ****

if (offset >= 0)
{
! if (size == -1)
// size may become negative, checked below
size = st.st_size - offset;
whence = SEEK_SET;
}
else
{
! if (size == -1)
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))
return OK;
if (offset != 0 && vim_fseek(fd, offset, whence) != 0)
return OK;
--- 199,230 ----

if (offset >= 0)
{
! // 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
{
! // 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;
}
! if (size <= 0)
return OK;
if (offset != 0 && vim_fseek(fd, offset, whence) != 0)
return OK;
*** ../vim-9.0.0809/src/testdir/test_blob.vim 2022-10-20 13:28:43.777615365 +0100
--- src/testdir/test_blob.vim 2022-10-21 11:19:25.372629712 +0100
***************
*** 499,508 ****
VAR br6 = readblob('Xblob', -3, 2)
call assert_equal(b[-3 : -2], br6)

VAR br1e = readblob('Xblob', 10000)
call assert_equal(0z, br1e)
! VAR br2e = readblob('Xblob', -10000)
! call assert_equal(0z, br2e)

call delete('Xblob')
END
--- 499,515 ----
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)
!
! #" 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
*** ../vim-9.0.0809/src/version.c 2022-10-20 21:14:14.664098677 +0100
--- src/version.c 2022-10-21 11:21:28.445042427 +0100
***************
*** 697,698 ****
--- 697,700 ----
{ /* Add new patch number below this line */
+ /**/
+ 810,
/**/

--
SUPERIMPOSE "England AD 787". After a few more seconds we hear hoofbeats in
the distance. They come slowly closer. Then out of the mist comes KING
ARTHUR followed by a SERVANT who is banging two half coconuts together.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages