Merge pull request #289 from NieDzejkob/warning-fix

pb12: check the return value of write
This commit is contained in:
Lior Halphon 2020-09-17 21:22:22 +03:00 committed by GitHub
commit e0d6aa59ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <err.h>
void opts(uint8_t byte, uint8_t *options) void opts(uint8_t byte, uint8_t *options)
{ {
@ -13,6 +14,17 @@ void opts(uint8_t byte, uint8_t *options)
*(options++) = byte & (byte >> 1); *(options++) = byte & (byte >> 1);
} }
void write_all(int fd, const void *buf, size_t count) {
while (count) {
ssize_t written = write(fd, buf, count);
if (written < 0) {
err(1, "write");
}
count -= written;
buf += written;
}
}
int main() int main()
{ {
static uint8_t source[0x4000]; static uint8_t source[0x4000];
@ -76,15 +88,15 @@ int main()
if (bits >= 8) { if (bits >= 8) {
uint8_t outctl = control >> (bits - 8); uint8_t outctl = control >> (bits - 8);
assert(outctl != 1); assert(outctl != 1);
write(STDOUT_FILENO, &outctl, 1); write_all(STDOUT_FILENO, &outctl, 1);
write(STDOUT_FILENO, literals, literals_size); write_all(STDOUT_FILENO, literals, literals_size);
bits -= 8; bits -= 8;
control &= (1 << bits) - 1; control &= (1 << bits) - 1;
literals_size = 0; literals_size = 0;
} }
} }
uint8_t end_byte = 1; uint8_t end_byte = 1;
write(STDOUT_FILENO, &end_byte, 1); write_all(STDOUT_FILENO, &end_byte, 1);
return 0; return 0;
} }