Bug 231019 - stat returns incorrect exit status
stat returns incorrect exit status
Status: CLOSED ERRATA
Product: Red Hat Enterprise Linux 4
Classification: Red Hat
Component: coreutils (Show other bugs)
4.4
All Linux
medium Severity medium
: ---
: ---
Assigned To: Tim Waugh
:
: 249465 (view as bug list)
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2007-03-05 12:14 EST by Bryn M. Reeves
Modified: 2010-10-22 09:30 EDT (History)
2 users (show)

See Also:
Fixed In Version: RHBA-2007-0786
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2007-11-15 11:09:22 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
patch to set G_fail=1 in error branches (679 bytes, patch)
2007-03-05 12:14 EST, Bryn M. Reeves
no flags Details | Diff

  None (edit)
Description Bryn M. Reeves 2007-03-05 12:14:54 EST
Description of problem:
Under a couple of error conditions, the stat(1) command incorrectly returns with
EXIT_SUCCESS instead of EXIT_FAILURE:

1. xreadlink returns NULL:
          char *linkname = xreadlink (filename);
          if (linkname == NULL)
            { 
              error (0, errno, _("cannot read symbolic link %s"),
                     quote (filename));
              return;
            }

2. statfs fails:
  int i = statfs (filename, &statfsbuf);

  if (i == -1)
    { 
      error (0, errno, _("cannot read file system information for %s"),
             quote (filename));
      return;
    }

3. stat fails:
  int i = ((follow_links == 1)
           ? stat (filename, &statbuf)
           : lstat (filename, &statbuf));

  if (i == -1)
    { 
      error (0, errno, _("cannot stat %s"), quote (filename));
      return;
    }


Version-Release number of selected component (if applicable):
coreutils-5.2.1-31.6

How reproducible:
100%

Steps to Reproduce:
Example for 3 above:
1. $ stat /foo/bar (/foo/bar must not exist)
2. $ echo $?
  
Actual results:
$ stat /foo/bar
stat: cannot stat `/foo/bar': No such file or directory
$ echo $?
0


Expected results:
$ stat /foo/bar
stat: cannot stat `/foo/bar': No such file or directory
$ echo $?
1


Additional info:
This bug does not exist in FC6's coreutils. Upstream has moved to using bool
return types for do_stat, print_stat & friends. This is propagated back to main
and checked here:

  for (i = optind; i < argc; i++)
    ok &= (fs
           ? do_statfs (argv[i], terse, secure, format)
           : do_stat (argv[i], follow_links, terse, secure, format));

  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);

In RHEL-4 there is a global G_fail that is checked in a similar way:

  for (i = optind; i < argc; i++)
    { 
      if (fs == 0)
        do_stat (argv[i], follow_links, terse, secure, format);
      else
        do_statfs (argv[i], terse, secure, format);
    }

  exit (G_fail ? EXIT_FAILURE : EXIT_SUCCESS);

Proposed patch attached that used G_fail to propagate the error back to main
where we exit with EXIT_FAILURE.
Comment 1 Bryn M. Reeves 2007-03-05 12:14:55 EST
Created attachment 149269 [details]
patch to set G_fail=1 in error branches
Comment 4 Tim Waugh 2007-07-30 10:29:06 EDT
*** Bug 249465 has been marked as a duplicate of this bug. ***
Comment 11 errata-xmlrpc 2007-11-15 11:09:22 EST
An advisory has been issued which should help the problem
described in this bug report. This report is therefore being
closed with a resolution of ERRATA. For more information
on the solution and/or where to find the updated files,
please follow the link below. You may reopen this bug report
if the solution does not work for you.

http://rhn.redhat.com/errata/RHBA-2007-0786.html

Note You need to log in before you can comment on or make changes to this bug.