NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/47643: nawk close() function does not report errors when closing pipes



>Number:         47643
>Category:       bin
>Synopsis:       nawk close() function does not report errors when closing pipes
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 12 12:05:01 +0000 2013
>Originator:     Anthony Mallet
>Release:        6.0.1
>Organization:
>Environment:
NetBSD fluffy64-netbsd6 6.0.1 NetBSD 6.0.1 (FLUFFY) #2: Sat Mar  2 20:12:01 CET 
2013  root@fluffy64-netbsd6:/usr/obj/sys/arch/amd64/compile/FLUFFY amd64

>Description:
/usr/bin/awk close() function does not report errors when closing a pipe.
I think the problem is trivial, though (see attached patch).

In the pipe case, the patch just makes the close function return the
pclose(3) error (like what gawk seems to do). This makes it possible
to switch on return code.

The patch also removes the "WARNING()" message for the pipe case. I think
it makes sense to expect errors while opening a pipe
(e.g. "test -d /nonexistent", or "test -f /somedir").

I added a \n to the message in the file case, this makes the output less
cluttered (but I'm not sure the message makes sense at all, even for the
file case).

>How-To-Repeat:
fluffy64-netbsd6[~] > cat close.awk 
BEGIN {
    cmd = "cd /nonexistent 2>&1"
    while ((cmd | getline l) > 0) { print "out: " l; }
    s = close(cmd)
    if (s) { print "Fatal error: " s; exit 2 }
    exit 0;
}
fluffy64-netbsd6[~]!> /usr/bin/awk -f close.awk
out: cd: can't cd to /nonexistent
fluffy64-netbsd6[~] > echo $?
0
fluffy64-netbsd6[~] > /usr/pkg/bin/gawk -f close.awk 
out: cd: can't cd to /nonexistent
Fatal error: 512
fluffy64-netbsd6[~]!> echo $?
2

>Fix:

Index: run.c
===================================================================
RCS file: /cvsroot/src/external/historical/nawk/dist/run.c,v
retrieving revision 1.6
diff -u -r1.6 run.c
--- run.c       29 Dec 2012 02:44:26 -0000      1.6
+++ run.c       12 Mar 2013 12:00:55 -0000
@@ -1838,14 +1838,15 @@
                        if (ferror(files[i].fp))
                                WARNING( "i/o error occurred on %s", 
files[i].fname );
                        if (files[i].mode == '|' || files[i].mode == LE)
-                               stat = pclose(files[i].fp) == -1;
-                       else
+                               stat = pclose(files[i].fp);
+                       else {
                                stat = fclose(files[i].fp) == EOF;
-                       if (stat) {
-                               stat = -1;
-                               WARNING( "i/o error occurred closing %s",
-                                   files[i].fname );
-                       }
+                               if (stat) {
+                                       stat = -1;
+                                       WARNING( "i/o error occurred closing 
%s\n",
+                                           files[i].fname );
+                               }
+                        }
                        if (i > 2)      /* don't do /dev/std... */
                                free(__UNCONST(files[i].fname));
                        files[i].fname = NULL;  /* watch out for ref thru this 
*/



Home | Main Index | Thread Index | Old Index