Bug 4559 - freopen failed if asked to open /dev/stdin
Summary: freopen failed if asked to open /dev/stdin
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.4
: P2 minor
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-29 17:53 UTC by alain williams
Modified: 2014-07-04 16:26 UTC (History)
1 user (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 alain williams 2007-05-29 17:53:44 UTC
Because fdopen() closes the file handle first the code:
freopen("/dev/stdin", "r", stdin)
fails -- /dev/stdin can't be opened.

If it opened a new file descriptor first and then closed this would work.

The real need is simple minded programs that naively use freopen() and then use
stdin or stdout.
Comment 1 Jakub Jelinek 2007-05-30 21:42:22 UTC
Don't do it then?
There is a standard POSIX supported way of doing this, just pass NULL
as the first argument to freopen.
Comment 2 alain williams 2007-05-30 21:55:36 UTC
That works if you are writing a C program. If someprogram that uses freopen()
and you give it a command line argument of /dev/stdin, expecting it to process
stdin, you will get a nasty surprise.
For this to work the program itself would need to specially recognise /dev/stdin
and /dev/fd/0 and ....

I am trying to make life easy for the application writer.
Comment 3 Ulrich Drepper 2007-05-30 22:07:38 UTC
(In reply to comment #2)
> I am trying to make life easy for the application writer.

By adding undue burden which negatively impacts everybody who doesn't need to
use this nonstandard way?  And the fact that the behavior is implemented like
this for 10+ years shows it is not (widely) used.

The standard is very clear:

The freopen() function shall first attempt to flush the stream and close any
file descriptor associated with stream.


The descriptor is closed and *then* we work on opening the new file.
Comment 4 alain williams 2007-05-30 22:12:50 UTC
I quite agree that the freopen() manual is very clear, but a user of an
application is not going to read that. They will read the application manual
that will say something like:

foobar <inputFile> <outputFile>

and so expect to type:

someCommand | foobar /dev/stdin /tmp/SomeOutputFile

There are a few programs that have that sort of syntax, they don't work with
/dev/stdin.