From: Joey Hess Date: Thu, 2 Sep 2010 20:39:51 +0000 (-0400) Subject: sponge: Correct bad use of fread that caused a trailing quantity of soaked data to... X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a456619d189cef2933aefef86606ae4b29516c85;p=moreutils sponge: Correct bad use of fread that caused a trailing quantity of soaked data to be silently discarded when a temp file was used and sponge output to stdout. Closes: #595220 This bug was sorta introduced by 6f31909ff74c064ea0b5126219b3e8f7b8826bee. Actually, the buggy fread was there before also, and would have happened on quantities of data not evenly divisible by 8. --- diff --git a/debian/changelog b/debian/changelog index f298d91..349a21e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,6 +2,9 @@ moreutils (0.41) UNRELEASED; urgency=low * ifdata.docbook: Mark interface as required in synopsis. Closes: #588397 * Add missing AUTHOR section to docbook man pages. + * sponge: Correct bad use of fread that caused a trailing quantity of + soaked data to be silently discarded when a temp file was used + and sponge output to stdout. Closes: #595220 -- Joey Hess Thu, 08 Jul 2010 14:40:07 -0400 diff --git a/sponge.c b/sponge.c index 80733a2..242298d 100644 --- a/sponge.c +++ b/sponge.c @@ -199,15 +199,16 @@ static void write_buff_out(char* buff, size_t length, FILE *fd) { } static void copy_tmpfile(FILE *tmpfile, FILE *outfile, char *buf, size_t size) { - if (fseek(tmpfile, 0, SEEK_SET)) { + ssize_t i; + if (lseek(fileno(tmpfile), 0, SEEK_SET)) { perror("could to seek to start of temporary file"); fclose(tmpfile); exit(1); } - while (fread(buf, size, 1, tmpfile) > 0) { - write_buff_out(buf, size, outfile); + while ((i = read(fileno(tmpfile), buf, size)) > 0) { + write_buff_out(buf, i, outfile); } - if (ferror(tmpfile)) { + if (i == -1) { perror("read temporary file"); fclose(tmpfile); exit(1);