[tz] [PROPOSED 1/7] Avoid undefined behavior if no Link lines

Ian Abbott abbotti at mev.co.uk
Wed Oct 26 14:16:39 UTC 2022


On 26/10/2022 11:03, Paul Eggert wrote:
> The core dump occurred because GCC translates this:
> 
>    qsort(links, nlinks, sizeof *links, qsort_linkcmp);
> 
> as if it were this:
> 
>    if (nlinks == 0)
>      __builtin_trap();
>    qsort(links, nlinks, sizeof *links, qsort_linkcmp);
> 
> That is, if qsort's second argument is zero, the code generated by GCC 
> doesn't call the qsort library function. Instead, it directly executes 
> the ud2 instruction <https://www.felixcloutier.com/x86/ud>, which raises 
> the invalid opcode exception. Presumably this is because the GCC 
> maintainers are in the faction that says a null pointer cannot be used 
> to pass a size-zero object to a library function. This is likely the 
> same faction that says "char *p = NULL; return p + 0;" has undefined 
> behavior.

Does it also call __builtin_trap() if nlinks is 0 and links is a valid 
pointer to modifiable storage?  Because it should allow nlinks to be 0 
in that case, according to the C standard.

-- 
-=( Ian Abbott <abbotti at mev.co.uk> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-



More information about the tz mailing list