NetBSD Problem Report #47643

From www@NetBSD.org  Tue Mar 12 12:04:52 2013
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	by www.NetBSD.org (Postfix) with ESMTP id 5AD3F63F1B0
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 12 Mar 2013 12:04:52 +0000 (UTC)
Message-Id: <20130312120451.37DFB63F1B0@www.NetBSD.org>
Date: Tue, 12 Mar 2013 12:04:51 +0000 (UTC)
From: anthony.mallet@laas.fr
Reply-To: anthony.mallet@laas.fr
To: gnats-bugs@NetBSD.org
Subject: nawk close() function does not report errors when closing pipes
X-Send-Pr-Version: www-1.0

>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 */

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.