On Sat, Feb 13, 2016 at 8:37 AM, Rob Landley <[email protected]> wrote:
> On 02/12/2016 10:16 AM, enh wrote:
>> 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).
>> ---
>>  toys/posix/wc.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> Attachment?

d'oh! attached.

-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
From 31f3da9db282bf6e600a202e2ef39da129412a79 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Fri, 12 Feb 2016 08:13:06 -0800
Subject: [PATCH] Fix wc -c optimization.

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).
---
 toys/posix/wc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/toys/posix/wc.c b/toys/posix/wc.c
index 62d1f7a..e7afc81 100644
--- a/toys/posix/wc.c
+++ b/toys/posix/wc.c
@@ -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;
     }
-- 
2.7.0.rc3.207.g0ac5344

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to