mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-14 01:04:38 +03:00
hexdump: Improve error handling
In particular, hexdump can now handle read errors and reads that completely fill up the buffer.
This commit is contained in:
parent
3582184d8c
commit
c706b2c142
Notes:
sideshowbarker
2024-07-18 01:25:22 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/c706b2c1428 Pull-request: https://github.com/SerenityOS/serenity/pull/10830
@ -62,8 +62,10 @@ int main(int argc, char** argv)
|
||||
size_t contents_size = 0;
|
||||
|
||||
int nread;
|
||||
do {
|
||||
while (true) {
|
||||
nread = file->read(&contents[contents_size], BUFSIZ - contents_size);
|
||||
if (nread <= 0)
|
||||
break;
|
||||
contents_size += nread;
|
||||
// Print as many complete lines as we can (possibly none).
|
||||
size_t offset;
|
||||
@ -71,9 +73,13 @@ int main(int argc, char** argv)
|
||||
print_line(&contents[offset], LINE_LENGTH_BYTES);
|
||||
}
|
||||
contents_size -= offset;
|
||||
// Regions cannot overlap due to above static_assert.
|
||||
memcpy(&contents[0], &contents[offset], contents_size);
|
||||
} while (nread);
|
||||
VERIFY(contents_size < LINE_LENGTH_BYTES);
|
||||
// If we managed to make the buffer exactly full, &contents[BUFSIZ] would blow up.
|
||||
if (contents_size > 0) {
|
||||
// Regions cannot overlap due to above static_assert.
|
||||
memcpy(&contents[0], &contents[offset], contents_size);
|
||||
}
|
||||
}
|
||||
VERIFY(contents_size <= LINE_LENGTH_BYTES - 1);
|
||||
if (contents_size > 0)
|
||||
print_line(&contents[0], contents_size);
|
||||
|
Loading…
Reference in New Issue
Block a user