* [musl] [PATCH] getopt: fix null pointer arithmetic ub
@ 2023-03-10 16:10 Alexey Izbyshev
2023-03-10 16:28 ` Alexander Monakov
0 siblings, 1 reply; 3+ messages in thread
From: Alexey Izbyshev @ 2023-03-10 16:10 UTC (permalink / raw)
To: musl
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;
optpos = 0;
}
if (optind > argc) {
--
2.39.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [musl] [PATCH] getopt: fix null pointer arithmetic ub
2023-03-10 16:10 [musl] [PATCH] getopt: fix null pointer arithmetic ub Alexey Izbyshev
@ 2023-03-10 16:28 ` Alexander Monakov
2023-03-10 16:59 ` Alexey Izbyshev
0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2023-03-10 16:28 UTC (permalink / raw)
To: musl
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [musl] [PATCH] getopt: fix null pointer arithmetic ub
2023-03-10 16:28 ` Alexander Monakov
@ 2023-03-10 16:59 ` Alexey Izbyshev
0 siblings, 0 replies; 3+ messages in thread
From: Alexey Izbyshev @ 2023-03-10 16:59 UTC (permalink / raw)
To: musl
On 2023-03-10 19:28, Alexander Monakov wrote:
> 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.
>
Yes, "if (optpos) ..." is actually what I initially wrote before
changing it to the submitted variant. I'm fine with changing it back;
thanks for the codegen check.
> (also (unlike the quoted variant) would allow undefined behavior
> instrumentation to catch attempted NULL pointer arithmetic)
>
Yes, a good point too.
Alexey
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-03-10 16:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-10 16:10 [musl] [PATCH] getopt: fix null pointer arithmetic ub Alexey Izbyshev
2023-03-10 16:28 ` Alexander Monakov
2023-03-10 16:59 ` Alexey Izbyshev
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).