lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250426105842.GF17549@1wt.eu>
Date: Sat, 26 Apr 2025 12:58:42 +0200
From: Willy Tarreau <w@....eu>
To: Thomas Weißschuh <thomas.weissschuh@...utronix.de>
Cc: Thomas Weißschuh <linux@...ssschuh.net>,
        Shuah Khan <shuah@...nel.org>, linux-kernel@...r.kernel.org,
        linux-kselftest@...r.kernel.org
Subject: Re: [PATCH 13/15] tools/nolibc: add fopen()

On Wed, Apr 23, 2025 at 05:01:43PM +0200, Thomas Weißschuh wrote:
> +static __attribute__((unused))
> +FILE *fopen(const char *pathname, const char *mode)
> +{
> +	int flags, fd;
> +
> +	if (!strcmp(mode, "r"))
> +		flags = O_RDONLY;
> +	else if (!strcmp(mode, "w"))
> +		flags = O_WRONLY | O_CREAT | O_TRUNC;
> +	else if (!strcmp(mode, "a"))
> +		flags = O_WRONLY | O_CREAT | O_APPEND;
> +	else if (!strcmp(mode, "r+"))
> +		flags = O_RDWR;
> +	else if (!strcmp(mode, "w+"))
> +		flags = O_RDWR | O_CREAT | O_TRUNC;
> +	else if (!strcmp(mode, "a+"))
> +		flags = O_RDWR | O_CREAT | O_APPEND;
> +	else {
> +		SET_ERRNO(EINVAL);
> +		return NULL;
> +	}

I'm concerned by the size of the function due to the repeated strcmp()
calls (also I find strcmp()==0 more readable that !strcmp() which I tend
to read as "not string compares").

I have not tried the code below but I think it could cover it in a maybe
lighter way:

	switch (*mode) {
		case 'r": flags = O_RDONLY; break;
		case 'w': flags = O_WRONLY | O_CREAT | O_TRUNC; break;
		case 'a': flags = O_WRONLY | O_CREAT | O_APPEND; break;
        	default : SET_ERRNO(EINVAL); return NULL;
        }

	if (mode[1] == '+')
		flags = (flags & ~(O_RDONLY|O_WRONLY)) | O_RDWR;

I think it does the same but should be significantly lighter.

Willy

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ