This is the mail archive of the
mailing list for the GCC project.
Re: C++ interop issue due to non-null pointers
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Florian Weimer <fweimer at redhat dot com>
- Cc: Trevor Saunders <tsaunders at mozilla dot com>, Jonathan Wakely <jwakely dot gcc at gmail dot com>, GCC <gcc at gcc dot gnu dot org>
- Date: Wed, 18 Jun 2014 09:24:28 +0200
- Subject: Re: C++ interop issue due to non-null pointers
- Authentication-results: sourceware.org; auth=none
- References: <53997584 dot 8080706 at redhat dot com> <CAH6eHdRua1KTs1QXf2Pu39orxCx6agdzwdzVvcPkcNVEEkRErA at mail dot gmail dot com> <53A03782 dot 9020208 at redhat dot com> <20140617142403 dot GA25179 at tsaunders-iceball dot corp dot tor1 dot mozilla dot com> <53A051E8 dot 5000105 at redhat dot com> <20140617143939 dot GC31640 at tucnak dot redhat dot com> <53A0562D dot 1010604 at redhat dot com> <20140617150039 dot GD31640 at tucnak dot redhat dot com> <53A13D32 dot 6070404 at redhat dot com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Jun 18, 2014 at 09:18:10AM +0200, Florian Weimer wrote:
> On 06/17/2014 05:00 PM, Jakub Jelinek wrote:
> >>>GCC will likely not optimize it away at this point, but having code with
> >>>undefined behavior is just asking for future trouble. Just use "" instead?
> >>It's always const and may lack sufficient alignment. The former isn't a
> >>problem in C++ (I think), but the alignment is an issue.
> >Alignment for what? You don't specify any alignment to the C qsort,
> You're returning a T *, not a void *, and C++ requires that pointers are
> properly aligned even if they aren't dereferenced.
C qsort doesn't return anything and the comparison function returns int.
extern void qsort (void *__base, size_t __nmemb, size_t __size,
__compar_fn_t __compar) __nonnull ((1, 4));
It is the C qsort that has undefined behavior if the 1st or 4th argument is
NULL. So I don't see what is wrong on:
qsort (ptr ? (void *) ptr : "", nmemb, sizeof (*ptr), compar);