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

Jonathan Leffler jonathan.leffler at gmail.com
Wed Oct 26 16:41:41 UTC 2022


On Wed, Oct 26, 2022 at 8:54 AM Jonathan Leffler <jonathan.leffler at gmail.com>
wrote:

> However, I think that TZ code can sidestep the whole issue by using:
>
> if (nlinks > 1)
>     qsort(links, nlinks, sizeof(*links), qsort_linkcmp);
>
> 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().
>

Of course, I assume here that links will never be null when nlinks is 1 or
more — the only time links could possibly be null is if nlinks is 0.  If
there are other circumstances where links could be null, you could add to
the condition:

if (nlinks > 1 && links != NULL)
    qsort(links, nlinks, sizeof(*links), qsort_linkcmp);

-- 
Jonathan Leffler <jonathan.leffler at gmail.com>  #include <disclaimer.h>
Guardian of DBD::Informix - v2018.1031 - http://dbi.perl.org
"Blessed are we who can laugh at ourselves, for we shall never cease to be
amused."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mm.icann.org/pipermail/tz/attachments/20221026/9f819ef2/attachment.html>


More information about the tz mailing list