mirror of
https://github.com/systemd/systemd.git
synced 2025-01-25 10:04:04 +03:00
fileio: fix truncated read handling in read_virtual_file()
We mishandled the case where the size we read from the file actually matched the maximum size fully. In that case we cannot really make a determination whether the file was fully read or only partially. In that case let's do another loop, so that we operate with a buffer, and we can detect the EOF (which will be signalled to us via a short read).
This commit is contained in:
parent
f782eee68a
commit
00bd9a4a82
@ -470,9 +470,14 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
|
|||||||
if (n <= size)
|
if (n <= size)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* If a maximum size is specified and we already read as much, no need to try again */
|
/* If a maximum size is specified and we already read more we know the file is larger, and
|
||||||
if (max_size != SIZE_MAX && n >= max_size) {
|
* can handle this as truncation case. Note that if the size of what we read equals the
|
||||||
n = max_size;
|
* maximum size then this doesn't mean truncation, the file might or might not end on that
|
||||||
|
* byte. We need to rerun the loop in that case, with a larger buffer size, so that we read
|
||||||
|
* at least one more byte to be able to distinguish EOF from truncation. */
|
||||||
|
if (max_size != SIZE_MAX && n > max_size) {
|
||||||
|
n = size; /* Make sure we never use more than what we sized the buffer for (so that
|
||||||
|
* we have one free byte in it for the trailing NUL we add below).*/
|
||||||
truncated = true;
|
truncated = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user