Skip to contentSkip to conversation

patch for "bizarre read bug" in klibc dash

PATCH Joshua Hudson 2 participants 3 messages Jun 15, 2006 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

I had forked ash awhile back, patched up a few things to behave the way I wanted them.
While I was at it, I fixed the "echo X | read X ; echo $X" bug that echoed a blank line.
I tried awhile ago to raise who was responsible for the code and got nowhere.

I suppose you think the above code is a bizarre corner case. It is not. I use very
complex shell expressions (think loops and subshells) in pipes, with read in the end to
get the variable I was looking for. The work-around is particularly ugly.

I was quite surprised that this bug never got squashed in dash. So, I backported it.

Well, here's the patch.

dash/eval.c 2006-06-14 20:07:58.000000000 -0700 renamed from dash/eval.c.orig 2006-06-14 19:48:47.000000000 -0700 +11 -1
@@ -539,7 +539,17 @@
                               sh_error("Pipe call failed");
                       }
               }
-               if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
+               if (!lp->next) {
+                       extern void tempredir0(int prevfd);
+                       /* Fix for "bizarre read bug" */
+                       if (prevfd > 0) {
+                               tempredir0(prevfd);
+                               close(prevfd);
+                       }
+                       evaltree(lp->n, 0);
+                       if (prevfd > 0)
+                               popredir(0);
+               } else if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
                       INTON;
                       if (pip[1] >= 0) {
                               close(pip[0]);
dash/redir.c 2006-06-14 20:03:38.000000000 -0700 renamed from dash/redir.c.orig 2006-06-14 19:59:21.000000000 -0700 +0 -0
@@ -310,6 +310,21 @@

}

+void tempredir0(int fd)
+{

+       struct redirtab *sv = redirlist;
+       int i;
+       sv = ckmalloc(sizeof (struct redirtab));
+       for (i = 0 ; i < 10 ; i++)
+               sv->renamed[i] = EMPTY;
+       sv->next = redirlist;
+       redirlist = sv;
+       INTOFF;
+       sv->renamed[0] = copyfd(0, 10);
+       close(0);
+       copyfd(fd, 0);
+       INTON;

+}

/*
* Undo the effects of the last redirection.

#1 lore raw
Quoted 5 lines · Joshua Hudson

I had forked ash awhile back, patched up a few things to behave the way I wanted them.
While I was at it, I fixed the "echo X | read X ; echo $X" bug that echoed a blank line.
I tried awhile ago to raise who was responsible for the code and got nowhere.

Have you tested your line with bash? Here it prints an empty line. You really should use "echo X | ( read X ; echo $X )".

So I don't think that ash is broken, and thus cannot acknowledge the patch.

Signature · 1 line
Alexander E. Patrakov

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 11 lines · Joshua Hudson

Oh, and the original code that found this long ago:
grep MemFree /proc/meminfo | read J MEMFREE K case $MEMFREE in
...
(MEMFREE used several times below)

Had to convert to:

MEMFREE=grep MemFree /proc/meminfo | (READ J M K ; echo $M) case $MEMFREE in
...

Pure-shell implementation:

while read J M K ; do
case "$J" in
MemFree:)
MEMFREE="$M"
esac
done </proc/meminfo
case $MEMFREE in
...

Signature · 1 line
Alexander E. Patrakov