Skip to content

Commit 5fad16b

Browse files
AZero13millert
authored andcommitted
Properly check against errors against fwrite
fwrite is not the same as write; you have to explicitly compare against the length to detect errors, and sometimes number of items is mistaken for length.
1 parent e6cf241 commit 5fad16b

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

lib/iolog/iolog_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ iolog_write(struct iolog_file *iol, const void *buf, size_t len,
8484
#endif
8585
{
8686
ret = (ssize_t)fwrite(buf, 1, len, iol->fd.f);
87-
if (ret <= 0) {
87+
if (ret != (ssize_t)len) {
8888
ret = -1;
8989
if (errstr != NULL)
9090
*errstr = strerror(errno);

plugins/python/regress/iohelpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fwriteall(const char *file_path, const char *string)
5151
goto cleanup;
5252

5353
size_t size = strlen(string);
54-
if (fwrite(string, 1, size, file) < size) {
54+
if (fwrite(string, 1, size, file) != size) {
5555
goto cleanup;
5656
}
5757

plugins/sudoers/sudo_printf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ sudo_printf_int(int msg_type, const char * restrict fmt, ...)
6868
len = vasprintf(&buf, fmt, ap);
6969
va_end(ap);
7070
}
71-
if (len != -1) {
72-
if (fwrite(buf, 1, (size_t)len, ttyfp ? ttyfp : fp) == 0)
71+
if (len >= 0) {
72+
if (fwrite(buf, 1, (size_t)len, ttyfp ? ttyfp : fp) != (size_t)len)
7373
len = -1;
7474
if (buf != sbuf)
7575
free(buf);

src/conversation.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
120120
close(ttyfd);
121121
}
122122
if (!written) {
123-
if (len != 0 && fwrite(msg->msg, 1, len, fp) == 0)
123+
if (len != 0 && fwrite(msg->msg, 1, len, fp) != len)
124124
goto err;
125-
if (crnl != NULL && fwrite(crnl, 1, 2, fp) == 0)
125+
if (crnl != NULL && fwrite(crnl, 1, 2, fp) != 2)
126126
goto err;
127127
}
128128
}
@@ -218,8 +218,8 @@ sudo_conversation_printf(int msg_type, const char * restrict fmt, ...)
218218
len = vasprintf(&buf, fmt, ap);
219219
va_end(ap);
220220
}
221-
if (len != -1) {
222-
if (fwrite(buf, 1, (size_t)len, ttyfp ? ttyfp : fp) == 0)
221+
if (len >= 0) {
222+
if (fwrite(buf, 1, (size_t)len, ttyfp ? ttyfp : fp) != (size_t)len)
223223
len = -1;
224224
if (buf != sbuf)
225225
free(buf);

0 commit comments

Comments
 (0)