mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] cuserid: support invocation with a NULL pointer argument
Date: Tue, 28 Jan 2020 16:43:56 -0500	[thread overview]
Message-ID: <20200128214356.GH30412@brightrain.aerifal.cx> (raw)
In-Reply-To: <20200128194032.4370-1-soeren+git@soeren-tempel.net>

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

      reply	other threads:[~2020-01-28 21:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-28 19:40 Sören Tempel
2020-01-28 21:43 ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200128214356.GH30412@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).