Bug 11741 - printf doesn't return a negative value in case of output error
Summary: printf doesn't return a negative value in case of output error
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 2.11
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-23 08:44 UTC by Vincent Lefèvre
Modified: 2014-06-30 17:45 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2010-06-23 08:44:34 UTC
For fprintf (thus printf), the C standard says:

  The fprintf function returns the number of characters transmitted,
  or a negative value if an output or encoding error occurred.

But printf doesn't return a negative value in case of output error (at least not
in some cases). For instance:

#include <stdio.h>

int main (void)
{
  int r;
  r = printf ("%10000s\n", "foo");
  fprintf (stderr, "%d\n", r);
  return 0;
}

Then I get:

$ ./out > /dev/null
10001
$ ./out >&-
8196

The first case is correct (anything can be written to /dev/null), but in the
second case (where the fd is closed), there's obviously an output error (the
return value is not 10001), but in such a case, r should have been negative.

Tested with glibc 2.11.2 on a Debian/unstable machine and on Fedora release 12
machine.

This bug looks like bug 1146, but that one was fixed 5 years ago.