comparison src/blob.c @ 30952:61558a67630a v9.0.0810

patch 9.0.0810: readblob() returns empty when trying to read too much Commit: https://github.com/vim/vim/commit/5b2a3d77d320d76f12b1666938a9d58c2a848205 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Oct 21 11:25:30 2022 +0100 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.
author Bram Moolenaar <Bram@vim.org>
date Fri, 21 Oct 2022 12:30:05 +0200
parents 84f6f91ca02a
children 360f286b5869
comparison
equal deleted inserted replaced
30951:f2dd117ca94c 30952:61558a67630a
197 if (fstat(fileno(fd), &st) < 0) 197 if (fstat(fileno(fd), &st) < 0)
198 return FAIL; // can't read the file, error 198 return FAIL; // can't read the file, error
199 199
200 if (offset >= 0) 200 if (offset >= 0)
201 { 201 {
202 if (size == -1) 202 // The size defaults to the whole file. If a size is given it is
203 // limited to not go past the end of the file.
204 if (size == -1 || (size > st.st_size - offset
205 #ifdef S_ISCHR
206 && !S_ISCHR(st.st_mode)
207 #endif
208 ))
203 // size may become negative, checked below 209 // size may become negative, checked below
204 size = st.st_size - offset; 210 size = st.st_size - offset;
205 whence = SEEK_SET; 211 whence = SEEK_SET;
206 } 212 }
207 else 213 else
208 { 214 {
209 if (size == -1) 215 // limit the offset to not go before the start of the file
216 if (-offset > st.st_size
217 #ifdef S_ISCHR
218 && !S_ISCHR(st.st_mode)
219 #endif
220 )
221 offset = -st.st_size;
222 // Size defaults to reading until the end of the file.
223 if (size == -1 || size > -offset)
210 size = -offset; 224 size = -offset;
211 whence = SEEK_END; 225 whence = SEEK_END;
212 } 226 }
213 // Trying to read bytes that aren't there results in an empty blob, not an 227 if (size <= 0)
214 // error.
215 if (size <= 0 || (
216 #ifdef S_ISCHR
217 !S_ISCHR(st.st_mode) &&
218 #endif
219 size > st.st_size))
220 return OK; 228 return OK;
221 if (offset != 0 && vim_fseek(fd, offset, whence) != 0) 229 if (offset != 0 && vim_fseek(fd, offset, whence) != 0)
222 return OK; 230 return OK;
223 231
224 if (ga_grow(&blob->bv_ga, (int)size) == FAIL) 232 if (ga_grow(&blob->bv_ga, (int)size) == FAIL)