Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 03 Jul 2023 18:30:28 -0400
From: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
To: musl@...ts.openwall.com
Subject: Re: fix various warnings/theoretical UB

Excerpts from Rich Felker's message of July 3, 2023 3:59 pm:
> On Mon, Jul 03, 2023 at 01:55:57PM -0400, Alex Xu (Hello71) wrote:
>> From b98f243e7921ddff6978ee9b0ce9f08efaa17951 Mon Sep 17 00:00:00 2001
>> From: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
>> Date: Sun, 2 Jul 2023 20:29:41 -0400
>> Subject: [PATCH 2/4] __year_to_secs: fix dangling pointer
>> 
>> C11 6.5.2.5p5:
>> 
>> > If the compound literal occurs outside the body of a function, the
>> > object has static storage duration; otherwise, it has automatic
>> > storage duration associated with the enclosing block.
>> 
>> gcc also warns about this.
>> ---
>>  src/time/__year_to_secs.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>> 
>> diff --git a/src/time/__year_to_secs.c b/src/time/__year_to_secs.c
>> index 2824ec6d..d215880a 100644
>> --- a/src/time/__year_to_secs.c
>> +++ b/src/time/__year_to_secs.c
>> @@ -10,9 +10,9 @@ long long __year_to_secs(long long year, int *is_leap)
>>  		return 31536000*(y-70) + 86400*leaps;
>>  	}
>>  
>> -	int cycles, centuries, leaps, rem;
>> +	int cycles, centuries, leaps, rem, tmp;
>>  
>> -	if (!is_leap) is_leap = &(int){0};
>> +	if (!is_leap) is_leap = &tmp;
>>  	cycles = (year-100) / 400;
>>  	rem = (year-100) % 400;
>>  	if (rem < 0) {
>> -- 
>> 2.41.0
> 
> Seems like a bogus warning. The enclosing block is the whole function,
> the same as the lifetime of the pointer. This might merit
> investigation on whether GCC is doing something wrong though..

As Jens says, an if statement "is a block whose scope is a strict subset 
of the scope of its enclosing block. Each associated substatement is 
also a block whose scope is a strict subset of the scope of the 
selection statement.".

>> From a30c4ab397af040d10d978d97dd4a6835d4b99a8 Mon Sep 17 00:00:00 2001
>> From: "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
>> Date: Sun, 2 Jul 2023 20:54:45 -0400
>> Subject: [PATCH 3/4] fix mismatched VLA parameter types
>> 
>> gcc warns about this, and it's probably technically UB
>> ---
>>  src/internal/procfdname.c | 2 +-
>>  src/prng/seed48.c         | 2 +-
>>  2 files changed, 2 insertions(+), 2 deletions(-)
>> 
>> diff --git a/src/internal/procfdname.c b/src/internal/procfdname.c
>> index fd7306ab..bfa3e7e5 100644
>> --- a/src/internal/procfdname.c
>> +++ b/src/internal/procfdname.c
>> @@ -1,6 +1,6 @@
>>  #include "syscall.h"
>>  
>> -void __procfdname(char *buf, unsigned fd)
>> +void __procfdname(char buf[static 15+3*sizeof(int)], unsigned fd)
>>  {
>>  	unsigned i, j;
>>  	for (i=0; (buf[i] = "/proc/self/fd/"[i]); i++);
> 
> This was raised/proposed before and is probably an okay change, but
> I'd like to understand what the reason "it's probably technically UB"
> is.
> 
>> diff --git a/src/prng/seed48.c b/src/prng/seed48.c
>> index bce7b339..7b789086 100644
>> --- a/src/prng/seed48.c
>> +++ b/src/prng/seed48.c
>> @@ -2,7 +2,7 @@
>>  #include <string.h>
>>  #include "rand48.h"
>>  
>> -unsigned short *seed48(unsigned short *s)
>> +unsigned short *seed48(unsigned short s[3])
>>  {
>>  	static unsigned short p[3];
>>  	memcpy(p, __seed48, sizeof p);
>> -- 
> 
> This one is almost surely not UB because there's no static and the 3
> is ignored. The question is just whether the static produces a
> difference in the declaration type that makes them clash.

After reading the function declarations section in the C2x draft, I 
think you're right. These are both well-defined because they are 
adjusted to the same pointer type, because neither the static nor 
non-static sizes are actually propagated to the pointer type.

Thanks,
Alex.

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.