<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif">On Wed, Oct 26, 2022 at 4:03 AM Paul Eggert via tz <<a href="mailto:tz@iana.org">tz@iana.org</a>> wrote:</span><br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
The core dump occurred because GCC translates this:<br>
<br>
   qsort(links, nlinks, sizeof *links, qsort_linkcmp);<br>
<br>
as if it were this:<br>
<br>
   if (nlinks == 0)<br>
     __builtin_trap();<br>
   qsort(links, nlinks, sizeof *links, qsort_linkcmp);<br>
<br>
That is, if qsort's second argument is zero, the code generated by GCC <br>
doesn't call the qsort library function. Instead, it directly executes <br>
the ud2 instruction <<a href="https://www.felixcloutier.com/x86/ud" rel="noreferrer" target="_blank">https://www.felixcloutier.com/x86/ud</a>>, which raises <br>
the invalid opcode exception. Presumably this is because the GCC <br>
maintainers are in the faction that says a null pointer cannot be used <br>
to pass a size-zero object to a library function. This is likely the <br>
same faction that says "char *p = NULL; return p + 0;" has undefined <br>
behavior.<br></blockquote><div> </div></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">It seems odd that GCC is testing the counter nlinks and not the pointer links.  Passing a null pointer is invalid — §7.10.5, §7.10.5.2, §7.1.4 of the C standard (C99 or later) indicate that.  But passing nlinks as zero should not cause any trouble according to C99 or later versions of the C standard, or according to POSIX.  The C90 standard is silent on the issue of a zero count.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">However, I think that TZ code can sidestep the whole issue by using:</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">if (nlinks > 1)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">    qsort(links, nlinks, sizeof(*links), qsort_linkcmp);</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">There's no need to sort arrays of size 0 or 1.  This avoids any questions about whether a count of zero is valid as an argument to qsort().</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Jonathan Leffler <<a href="mailto:jonathan.leffler@gmail.com" target="_blank">jonathan.leffler@gmail.com</a>>  #include <disclaimer.h><br>Guardian of DBD::Informix - v2018.1031 - <a href="http://dbi.perl.org" target="_blank">http://dbi.perl.org</a><br>"Blessed are we who can laugh at ourselves, for we shall never cease to be amused."</div></div></div></div></div></div></div></div></div></div></div>