attached is the patch that's the reason why this morning i went through the
other places where toybox looks at errno without first checking that the
function call failed...

i think we should take this fix since POSIX allows errno to be clobbered by
a successful call to most functions ["The setting of errno after a
successful call to a function is unspecified unless the description of that
function specifies that errno shall not be modified"], and that certainly
happens in practice.

there's definitely something odd going on here though. the reason i've not
added a test case is that i can't reproduce this at all on glibc/x86-64,
nor on bionic/x86-64. i can reproduce it on bionic/arm64, but not quite as
reliably as the person who hit this in practice while doing real work (and
they only see it 1/100 runs). but what we do have in common is that we're
seeing errno set to ENOENT (!), and i have no explanation for how
getdelim() is clobbering errno with that specific value. so i think we have
more than one bug, but this is a bug regardless, so... patch attached.
From b5d5f3871657287248574b048f5ad8840dcd146b Mon Sep 17 00:00:00 2001
From: Robin Hsu <[email protected]>
Date: Thu, 11 Mar 2021 18:50:38 +0800
Subject: [PATCH] Fix grep bug testing errno before check status

It's legal for a system call to set non-zero errno and return a good
status. The caller (grep) should check return status first.

Test: 2000 loops of greping 1000+ lines each loop
Signed-off-by: Robin Hsu <[email protected]>
Change-Id: I55f7cd5d8a6289c5e8a21ed3057e995a75b9b4fa
---

diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index cd8928a..52d1013 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -154,7 +154,7 @@
     lcount++;
     errno = 0;
     ulen = len = getdelim(&line, &ulen, TT.indelim, file);
-    if (errno) perror_msg("%s", name);
+    if (len == -1 && errno) perror_msg("%s", name);
     if (len<1) break;
     if (line[ulen-1] == TT.indelim) line[--ulen] = 0;
 
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to