From patchwork Tue Dec 29 07:32:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Gribov X-Patchwork-Id: 10158 Received: (qmail 15993 invoked by alias); 29 Dec 2015 07:31:53 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 15975 invoked by uid 89); 29 Dec 2015 07:31:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=no version=3.3.2 spammy=leaders, mandated, H*r:Tue, Hx-languages-length:2522 X-HELO: mailout3.w1.samsung.com Received: from mailout3.w1.samsung.com (HELO mailout3.w1.samsung.com) (210.118.77.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 29 Dec 2015 07:31:50 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0O03001SUZKYSD90@mailout3.w1.samsung.com> for gdb-patches@sourceware.org; Tue, 29 Dec 2015 07:31:46 +0000 (GMT) Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id C3.71.16778.2E632865; Tue, 29 Dec 2015 07:31:46 +0000 (GMT) Received: from [106.109.9.145] by eusync1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0O0300AD7ZKRL030@eusync1.samsung.com>; Tue, 29 Dec 2015 07:31:46 +0000 (GMT) Subject: [PATCH][PING][PR gdb/19361] Fix invalid comparison functions References: <566FFEE2.4010300@samsung.com> To: gdb-patches@sourceware.org Cc: Stan Shebs , Paul Pluzhnikov From: Yury Gribov X-Forwarded-Message-Id: <566FFEE2.4010300@samsung.com> Message-id: <56823702.6020804@samsung.com> Date: Tue, 29 Dec 2015 10:32:18 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-version: 1.0 In-reply-to: <566FFEE2.4010300@samsung.com> Content-type: multipart/mixed; boundary=------------090000080208090305010102 Hi all, The attached patch fixes bugs in comparison functions qsort_cmp and compare_processes. I've tested the patch on x86_64-pc-linux-gnu (no regressions in testsuite except for flakiness in gdb.threads and bigcore.exp). These functions are passed to qsort(3) but do not obey standard symmetry requirements mandated by the standard (grep for "total ordering" in http://pubs.opengroup.org/onlinepubs/009695399/functions/qsort.html). This causes undefined behavior at runtime which can e.g. cause qsort to produce invalid results. Compare_processes fails to properly compare process group leaders which is probably a serious problem (e.g. resulting in invalid sort). Qsort_cmp fails to produce proper result when comparing same element. Sane qsort implementation probably don't call comparison callback on same element so this may not be a big problem in practice but I think it should still be fixed. NB1: please Cc: me explicitly, I'm not subscribed to the list. NB2: Bugs were found with SortChecker tool (https://github.com/yugr/sortcheck). Best regards, Yury Gribov >From 5bc187e060a75f0dff83e2803adb53f41c2e6dcb Mon Sep 17 00:00:00 2001 From: Yury Gribov Date: Tue, 15 Dec 2015 10:56:06 +0300 Subject: [PATCH] Fix invalid comparison functions. 2015-12-15 Yury Gribov PR gdb/19361 * nat/linux-osdata.c (compare_processes): Fix asymmetry in comparison function. * objfiles.c (qsort_cmp): Ditto. --- gdb/nat/linux-osdata.c | 4 ++-- gdb/objfiles.c | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 56a8fe6..25a310f 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -420,9 +420,9 @@ compare_processes (const void *process1, const void *process2) else { /* Process group leaders always come first, else sort by PID. */ - if (pid1 == pgid1) + if (pid1 == pgid1 && pid2 != pgid2) return -1; - else if (pid2 == pgid2) + else if (pid1 != pgid1 && pid2 == pgid2) return 1; else if (pid1 < pid2) return -1; diff --git a/gdb/objfiles.c b/gdb/objfiles.c index d33379f..4aa600b 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -1140,6 +1140,9 @@ qsort_cmp (const void *a, const void *b) const CORE_ADDR sect1_addr = obj_section_addr (sect1); const CORE_ADDR sect2_addr = obj_section_addr (sect2); + if (sect1 == sect2) + return 0; + if (sect1_addr < sect2_addr) return -1; else if (sect1_addr > sect2_addr) -- 1.9.1