[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()



Package: libc6-dev
Version: 2.28-10
Severity: normal

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.

A way to make the code behaving as one would expect is to add a fprintf
call before closing descriptor. It then behaves as expected with both
libCs.

-- System Information:
Debian Release: 10.7
  APT prefers stable
  APT policy: (990, 'stable'), (500, 'stable-updates')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 4.19.0-13-amd64 (SMP w/4 CPU cores)
Kernel taint flags: TAINT_OOT_MODULE, TAINT_UNSIGNED_MODULE
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8), LANGUAGE=fr_FR.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: runit (via /run/runit.stopit)

Versions of packages libc6-dev depends on:
ii  libc-dev-bin    2.28-10
ii  libc6           2.28-10
ii  linux-libc-dev  4.19.160-2

libc6-dev recommends no packages.

Versions of packages libc6-dev suggests:
pn  glibc-doc     <none>
ii  manpages-dev  4.16-2

-- no debconf information


Reply to: