Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <D3MZKNBDS3GW.3UAIFLI4ISHCA@icloud.com>
Date: Thu, 22 Aug 2024 20:55:01 -0700
From: "Ryan Rhee" <ryanrhee@...oud.com>
To: <musl@...ts.openwall.com>
Subject: Possible bug with fputs and fgets mix

Hello,

I'm running into a problem when calling fputs on stdout before fgets on stdin 
results in the program calling the read syscall before the write syscall.

Example code:

	#include <stdio.h>
	int main() {
		fputs("Enter in something: ", stdout);

		char buf[32];
		fgets(buf, sizeof buf, stdin);

		return 0;
	}

When built with musl-gcc[1] and run, it appears to first poll stdin before writing
to stdout. For example, when built with musl and ran with strace, the following
happens:

	$ strace ./test
	execve("./test", ["./test"], 0x7ffcb2b5a5d0 /* 59 vars */) = 0
	arch_prctl(ARCH_SET_FS, 0x405b78)       = 0
	set_tid_address(0x405cb0)               = 7306
	read(0, hello
	"hello\n", 1024)                = 6
	ioctl(1, TIOCGWINSZ, {ws_row=38, ws_col=110, ws_xpixel=1210, ws_ypixel=1026}) = 0
	writev(1, [{iov_base="Enter in something: ", iov_len=20}, {iov_base=NULL, iov_len=0}], 2Enter in something: ) = 20
	exit_group(0)                           = ?
	+++ exited with 0 +++
	$

As you can see, the read happens before the writev for some reason.

When an fflush(stdout) is placed after the fputs, the output appears before the
input as expected. When the fputs call is replaced with a puts call, the
program works as expected (yet this is not viable, as puts includes a newline
after the string is printed, which is undesired).

Is this a bug with musl, or is a fflush required with fputs unlike with puts?

I am not subscribed to the mailing list, so please CC me with any replies.

Thanks in advance!

[1] Exact command: musl-gcc -std=c11 -o test test.c -static
    musl package version: 1.2.5-2
    Distro: Arch Linux
    gcc version: 14.2.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.