To: vim_dev@googlegroups.com Subject: Patch 9.0.0803 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0803 Problem: readblob() cannot read from character device. Solution: Use S_ISCHR() to not check the size. (Ken Takata, closes #11407) Files: runtime/doc/builtin.txt, src/blob.c, src/proto/blob.pro, src/testdir/test_blob.vim *** ../vim-9.0.0802/runtime/doc/builtin.txt 2022-10-19 14:02:34.957276577 +0100 --- runtime/doc/builtin.txt 2022-10-20 13:20:35.886088358 +0100 *************** *** 6858,6864 **** readblob('file.bin', 0, 100) < If {size} is -1 or omitted, the whole data starting from {offset} will be read. ! 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. --- 6857,6868 ---- readblob('file.bin', 0, 100) < If {size} is -1 or omitted, the whole data starting from {offset} will be read. ! This can be also used to read the data from a character device ! on Unix when {size} is explicitly set. Only if the device ! supports seeking {offset} can be used. Otherwise it should be ! zero. E.g. to read 10 bytes from a serial console: > ! 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. *** ../vim-9.0.0802/src/blob.c 2022-10-19 14:02:34.961276576 +0100 --- src/blob.c 2022-10-20 13:20:35.886088358 +0100 *************** *** 212,220 **** } // Trying to read bytes that aren't there results in an empty blob, not an // error. ! if (size < 0 || size > st.st_size) return OK; ! if (vim_fseek(fd, offset, whence) != 0) return OK; if (ga_grow(&blob->bv_ga, (int)size) == FAIL) --- 212,224 ---- } // 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; if (ga_grow(&blob->bv_ga, (int)size) == FAIL) *** ../vim-9.0.0802/src/proto/blob.pro 2022-10-19 14:02:34.961276576 +0100 --- src/proto/blob.pro 2022-10-20 13:20:35.886088358 +0100 *************** *** 10,16 **** void blob_set(blob_T *blob, int idx, int byte); void blob_set_append(blob_T *blob, int idx, int byte); int blob_equal(blob_T *b1, blob_T *b2); ! int read_blob(FILE *fd, typval_T *rettv, off_T offset, off_T size); int write_blob(FILE *fd, blob_T *blob); char_u *blob2string(blob_T *blob, char_u **tofree, char_u *numbuf); blob_T *string2blob(char_u *str); --- 10,16 ---- void blob_set(blob_T *blob, int idx, int byte); void blob_set_append(blob_T *blob, int idx, int byte); int blob_equal(blob_T *b1, blob_T *b2); ! int read_blob(FILE *fd, typval_T *rettv, off_T offset, off_T size_arg); int write_blob(FILE *fd, blob_T *blob); char_u *blob2string(blob_T *blob, char_u **tofree, char_u *numbuf); blob_T *string2blob(char_u *str); *** ../vim-9.0.0802/src/testdir/test_blob.vim 2022-10-19 14:02:34.961276576 +0100 --- src/testdir/test_blob.vim 2022-10-20 13:26:30.446022155 +0100 *************** *** 508,513 **** --- 508,518 ---- END call v9.CheckLegacyAndVim9Success(lines) + if filereadable('/dev/random') + let b = readblob('/dev/random', 0, 10) + call assert_equal(10, len(b)) + endif + call assert_fails("call readblob('notexist')", 'E484:') " TODO: How do we test for the E485 error? *** ../vim-9.0.0802/src/version.c 2022-10-20 13:11:12.234206115 +0100 --- src/version.c 2022-10-20 13:23:14.690058281 +0100 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 803, /**/ -- People who want to share their religious views with you almost never want you to share yours with them. /// Bram Moolenaar -- Bram@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 ///