[uClibc]fdopen() problem

Magick magick at dds.nl
Mon Mar 19 21:38:23 UTC 2001


On Tue, 13 Mar 2001 17:05:49 Manuel Novoa III wrote:
> Michael,
> 
> On Tue, 13 Mar 2001, michaels at jungo.com wrote:
> > While using netkit's ftp client compiled against uClibc, I get an error
> > on fdopen.
> > I tried to track down the problem and arrived to a conclusion that the
> > follwoing code causes the return value of fdopen to be -1:
> > ------------------------
> > stdio.c at line 710
> > 
> > 	} else {   /* fdopen -- check mode is compatible. */
> > 		cur_mode = fcntl(fd, F_GETFL);
> > 		if ((cur_mode == -1) || ((cur_mode ^ open_mode) &
> O_ACCMODE)) {
> > 			fd = -1;
> > 		}
> > ---------------------
> > I don't understand the ((cur_mode ^ open_mode) & O_ACCMODE) importance
> > to failure.
> 
> It is meant to check that the mode passed to fdopen is consistent with
> the
> current mode of the file descriptor.  So, it fails if fcntl reports an
> error or
> if the modes are inconsistent.  You don't want the FILE to think that the
> file
> descriptor is open for reading or writing when it isn't.
> 
> What mode is being passed to fdopen?  How was the file descriptor opened?
> 
> > Can anyone claify this for me? 
> > Anyway I think the condition must be inverse: !((cur_mode ^ open_mode)
> &
> > O_ACCMODE)
> 
> (1 ^ 1) & 1 == 0      bits same -- ok
> (0 ^ 0) & 1 == 0      
> (1 ^ 0) & 1 == 1      bits different -- set fd = -1 to signal an error
> (0 ^ 1) & 1 == 1
> (x ^ x)  & 0 == 0      don't care about these
> 
> I don't know... looks good to me, but I've been up all night rewriting
> scanf.
I hit something simular with libz. It does a fdopen of stdout, which could
be opened read+write. And i gave it a good night sleep ;-p
And this is what i came up with:

#define O_ACCMODE          0003
#define O_RDONLY             00
#define O_WRONLY             01
#define O_RDWR               02

(cur_mode ^ open_mode)

(2 ^ 0) & 3 == 2
(2 ^ 1) & 3 == 3
The bits differ, but a legal mode. Your using a fd
opened RDWR for reading OR writing, I don't think thats a problem.

I think it should be changed to:
if ((cur_mode == -1) || (cur_mode == O_RDWR) && ((cur_mode ^ open_mode) &
O_ACCMODE))

Bart
-- 
Attack is the secret of defense; defense is the planning of an attack.
 -- Sun Tzu, The Art Of War
GPG key = 1024D/4B086D06 
Fingerprint = CD4D 5601 287D F075 6F96  6157 99F9 E56A 4B08 6D06







More information about the uClibc mailing list