From patchwork Tue Dec 15 11:52:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Gribov X-Patchwork-Id: 10013 Received: (qmail 114690 invoked by alias); 15 Dec 2015 11:51:59 -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 114679 invoked by uid 89); 15 Dec 2015 11:51:58 -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, SPF_HELO_PASS, T_MANY_HDRS_LCASE, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mailout1.w1.samsung.com Received: from mailout1.w1.samsung.com (HELO mailout1.w1.samsung.com) (210.118.77.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 15 Dec 2015 11:51:56 +0000 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NZE0038BEAG3B90@mailout1.w1.samsung.com> for gdb-patches@sourceware.org; Tue, 15 Dec 2015 11:51:52 +0000 (GMT) Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id D3.E6.16778.8DEFF665; Tue, 15 Dec 2015 11:51:52 +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 <0NZE000PHEAF0I70@eusync1.samsung.com>; Tue, 15 Dec 2015 11:51:52 +0000 (GMT) To: gdb-patches@sourceware.org From: Yury Gribov Subject: [PATCH][PR gdb/19361] Fix invalid comparison functions Cc: Paul Pluzhnikov , Stan Shebs Message-id: <566FFEE2.4010300@samsung.com> Date: Tue, 15 Dec 2015 14:52:02 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-version: 1.0 Content-type: multipart/mixed; boundary=------------010208060606050508070508 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