Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933238AbcLTLKd (ORCPT ); Tue, 20 Dec 2016 06:10:33 -0500 Received: from terminus.zytor.com ([198.137.202.10]:44978 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757845AbcLTLKa (ORCPT ); Tue, 20 Dec 2016 06:10:30 -0500 Subject: Re: [tip:x86/urgent] x86/tools: Fix gcc-7 warning in relocs.c To: Markus Trippelsdorf References: <20161215124513.GA289@x4> <20161220100056.GA289@x4> Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org, mingo@kernel.org, linux-tip-commits@vger.kernel.org From: "H. Peter Anvin" Message-ID: <2966d559-2cea-fd95-76d4-df34192a9b88@zytor.com> Date: Tue, 20 Dec 2016 03:10:15 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: <20161220100056.GA289@x4> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1218 Lines: 35 On 12/20/16 02:00, Markus Trippelsdorf wrote: > On 2016.12.20 at 01:30 -0800, H. Peter Anvin wrote: >> I'd strongly prefer a non-data-dependent solution, specifically adding >> at the top of sort_relocs(): >> >> if (!r->count) >> return; >> >> However, by my reading of the C and POSIX standards, this is a gcc >> error: qsort() should do nothing if the count is zero. > > No, it is invoking undefined behavior. H > Notice the nonnull attribute in /usr/include/stdlib.h: > > 739 /* Sort NMEMB elements of BASE, of SIZE bytes each, > 740 using COMPAR to perform the comparisons. */ > 741 extern void qsort (void *__base, size_t __nmemb, size_t __size, > 742 __compar_fn_t __compar) __nonnull ((1, 4)); > > But feel free to revert my patch and add your solution. Well, s/gcc/glibc/ then. > The qsort() function shall sort an array of nel objects, the > initial element of which is pointed to by base. The size of > each object, in bytes, is specified by the width argument. If > the nel argument has the value zero, the comparison function > pointed to by compar shall not be called and no rearrangement > shall take place. -hpa