Skip to content

Commit

Permalink
Fix wc -c optimization.
Browse files Browse the repository at this point in the history
Check the fstat(2) return value rather than read uninitialized memory if
it failed, and add a special case for files that claim to be zero-length
but aren't (as is common in /proc on Linux).
  • Loading branch information
enh-google authored and landley committed Feb 14, 2016
1 parent 363659c commit 09d9547
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions toys/posix/wc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ static void do_wc(int fd, char *name)
if (toys.optflags == FLAG_c) {
struct stat st;

fstat(fd, &st);
if (S_ISREG(st.st_mode)) {
// On Linux, files in /proc often report their size as 0.
if (!fstat(fd, &st) && S_ISREG(st.st_mode) && st.st_size > 0) {
lengths[2] = st.st_size;
goto show;
}
Expand Down

0 comments on commit 09d9547

Please sign in to comment.