mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] [PATCH] cuserid: support invocation with a NULL pointer argument
@ 2020-01-28 19:40 Sören Tempel
  2020-01-28 21:43 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Sören Tempel @ 2020-01-28 19:40 UTC (permalink / raw)
  To: musl

I did not manage to find a copy of IEEE 1003.1-1988 (the last POSIX
version where cuserid was last standardized) the Single UNIX
specification version 2 does state the following though [1]:

	If s is a null pointer, this representation is generated in an
	area that may be static (and thus overwritten by subsequent
	calls to cuserid()), the address of which is returned.

Even though this a legacy function it would therefore be nice for musl
to support usage with a NULL pointer. I ran into this on Alpine Linux
when using cdparanoia [2] which uses cuserid like this and therefore
caused a crash on my system.

[1]: https://pubs.opengroup.org/onlinepubs/7908799/xsh/cuserid.html
[2]: https://xiph.org/paranoia/index.html
---
 src/legacy/cuserid.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/legacy/cuserid.c b/src/legacy/cuserid.c
index 4e78798d..19206ba4 100644
--- a/src/legacy/cuserid.c
+++ b/src/legacy/cuserid.c
@@ -5,10 +5,12 @@
 
 char *cuserid(char *buf)
 {
+	static char *usridbuf[L_cuserid];
 	struct passwd pw, *ppw;
 	long pwb[256];
 	if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
 		return 0;
+	buf = (buf) ? buf : usridbuf;
 	snprintf(buf, L_cuserid, "%s", pw.pw_name);
 	return buf;
 }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] [PATCH] cuserid: support invocation with a NULL pointer argument
  2020-01-28 19:40 [musl] [PATCH] cuserid: support invocation with a NULL pointer argument Sören Tempel
@ 2020-01-28 21:43 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2020-01-28 21:43 UTC (permalink / raw)
  To: musl

On Tue, Jan 28, 2020 at 08:40:32PM +0100, Sören Tempel wrote:
> I did not manage to find a copy of IEEE 1003.1-1988 (the last POSIX
> version where cuserid was last standardized) the Single UNIX
> specification version 2 does state the following though [1]:
> 
> 	If s is a null pointer, this representation is generated in an
> 	area that may be static (and thus overwritten by subsequent
> 	calls to cuserid()), the address of which is returned.
> 
> Even though this a legacy function it would therefore be nice for musl
> to support usage with a NULL pointer. I ran into this on Alpine Linux
> when using cdparanoia [2] which uses cuserid like this and therefore
> caused a crash on my system.
> 
> [1]: https://pubs.opengroup.org/onlinepubs/7908799/xsh/cuserid.html
> [2]: https://xiph.org/paranoia/index.html

I'm not sure whether to adopt this or not, but thanks for posting on
the list for discussion. In any case it's something we should try to
get fixed in apps that are using it, since this is no longer portable
usage and is gratuitously thread-unsafe.

> ---
>  src/legacy/cuserid.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/legacy/cuserid.c b/src/legacy/cuserid.c
> index 4e78798d..19206ba4 100644
> --- a/src/legacy/cuserid.c
> +++ b/src/legacy/cuserid.c
> @@ -5,10 +5,12 @@
>  
>  char *cuserid(char *buf)
>  {
> +	static char *usridbuf[L_cuserid];

Surely the * there is misplaced; the following should not compile with
it there:

>  	struct passwd pw, *ppw;
>  	long pwb[256];
>  	if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
>  		return 0;
> +	buf = (buf) ? buf : usridbuf;
>  	snprintf(buf, L_cuserid, "%s", pw.pw_name);
>  	return buf;
>  }

Also no need for parens around (buf); the more idiomatic way to write
this would be:

if (!buf) buf = usridbuf;

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-01-28 21:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-28 19:40 [musl] [PATCH] cuserid: support invocation with a NULL pointer argument Sören Tempel
2020-01-28 21:43 ` Rich Felker

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git