imap-send: correctly report errors reading from stdin

Previously, read_message() didn't distinguish between an error and eof when reading its input. This could have resulted in incorrect behavior if there was an error: (1) reporting "nothing to send" if no bytes were read or (2) sending an incomplete message if some bytes were read before the error. Change read_message() to return -1 on ferror()s and 0 on success, so that the caller can recognize that an error occurred. (The return value used to be the length of the input read, which was redundant because that is already available as the strbuf length. Change the caller to report errors correctly. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Nov 25, 2012 at 12:08 UTC 6360bee4cd902bdea5db5826821edffd6f367d89
1 file changed +7 -2
imap-send.c
+7 -2
@@ -1398,7 +1398,7 @@ static int read_message(FILE *f, struct strbuf *all_msgs)
1398 break;
1399 } while (!feof(f));
1400
1401 - return all_msgs->len;
1401 + return ferror(f) ? -1 : 0;
1402 }
1403
1404 static int count_messages(struct strbuf *all_msgs)
@@ -1537,7 +1537,12 @@ int main(int argc, char **argv)
1537 }
1538
1539 /* read the messages */
1540 - if (!read_message(stdin, &all_msgs)) {
1540 + if (read_message(stdin, &all_msgs)) {
1541 + fprintf(stderr, "error reading input\n");
1542 + return 1;
1543 + }
1544 +
1545 + if (all_msgs.len == 0) {
1546 fprintf(stderr, "nothing to send\n");
1547 return 1;
1548 }