[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#980764: libc6-dev: wrong return value for fputs when STDOUT_FILENO was closed()



* Bérenger:

> When running following code:
>
> ```C
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> 	close( STDIN_FILENO );
> 	close( STDOUT_FILENO );
> 	int fd = dup( STDERR_FILENO );
> 	close( STDERR_FILENO );
> 	if( -1 == fprintf( stdout, "%d\n", fd ) )
> 	{
> 		return -1;
> 	}
>
> 	char s[] = "should fail\n";
> 	if( -1 == write( STDOUT_FILENO, s, sizeof( s ) ) )
> 	{
> 		return -2;
> 	}
> 	return EXIT_SUCCESS;
> }
> ```
>
> built with glibc, the program returns 254. When built with muslc, it
> returns the expected value of 255.
>
> I believe glibc's behavior here is wrong. From what I could get by using
> strace, it seems that the 1st printf's write() call is ran _after_ the
> 2nd one, even when adding a call to fflush( stdout ) right after the
> printf.

The reason for the glibc behavior is that stdout ends up as a buffered
stream because it is not a terminal.  Why do you think this is a bug?


Reply to: