mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alexander Monakov <amonakov@ispras.ru>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] getopt: fix null pointer arithmetic ub
Date: Fri, 10 Mar 2023 19:28:18 +0300 (MSK)	[thread overview]
Message-ID: <a3ed1efc-7a3e-4c69-b9c2-9ed6e9a97d69@ispras.ru> (raw)
In-Reply-To: <20230310161022.108593-1-izbyshev@ispras.ru>

Hi,

On Fri, 10 Mar 2023, Alexey Izbyshev wrote:

> When an option that requires an argument is the last character of
> argv[argc-1], getopt computes argv[argc] + optpos. While optpos
> is always zero in this case, adding it to null pointer is still
> undefined.
> ---
>  src/misc/getopt.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/src/misc/getopt.c b/src/misc/getopt.c
> index c3f66995..af12973a 100644
> --- a/src/misc/getopt.c
> +++ b/src/misc/getopt.c
> @@ -87,7 +87,8 @@ int getopt(int argc, char * const argv[], const char *optstring)
>  	if (optstring[i] == ':') {
>  		optarg = 0;
>  		if (optstring[i+1] != ':' || optpos) {
> -			optarg = argv[optind++] + optpos;
> +			optarg = argv[optind++];
> +			if (optarg) optarg += optpos;

Can this be written as 'if (optpos) optarg += optpos;' instead? That will be
folded back into plain addition by the compiler.

(also (unlike the quoted variant) would allow undefined behavior
instrumentation to catch attempted NULL pointer arithmetic)

Alexander

  reply	other threads:[~2023-03-10 16:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10 16:10 Alexey Izbyshev
2023-03-10 16:28 ` Alexander Monakov [this message]
2023-03-10 16:59   ` Alexey Izbyshev

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=a3ed1efc-7a3e-4c69-b9c2-9ed6e9a97d69@ispras.ru \
    --to=amonakov@ispras.ru \
    --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).