From patchwork Fri Nov 17 18:44:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 80170 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4B1C7385B83C for ; Fri, 17 Nov 2023 18:44:45 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E57853858006 for ; Fri, 17 Nov 2023 18:44:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E57853858006 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E57853858006 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700246655; cv=none; b=MFR/A7oohd6o5x5DPsU5SyFgCh+oJmzoUPeXVmMqjo//7fU6vlrhSbAiERLA7Bd/IZm1t90JTymQixlm7V2me7aGH+jKuL2DFulhd7F/lfMd4IhwH4xexXHTaMu6IbThPo6p2xJUO6p5oiqs6Y1qHV60iptfGfDSlnEmEayDnwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700246655; c=relaxed/simple; bh=2JymALvV080LEpyZ2ZrYpLiBI3UUJdFsCSYp4KNmj8c=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=gouGDYgQG+SNTvhehuSI9jkzPCZuTiQnMreppDNeALWGxpv4DfdX2CTrFf7f9+uLtqk3d0y5uMnf4cWajvPows2z/wmClG6zQQz8lzqRrMU0aD1CPgrDcz6tQ1b/+vskMDcpDmNetixqbpWrIw9Is2W7K5p18b6Ybd3qfzq8xq4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700246654; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=MSSPhdVUZm2JD53sl0YsUFq2OxjU/2R9Hfmxpx8AIpU=; b=PW1rLF70TVAq7WjNguW5EUWnlajCiWbhuThpL/V4eDu6KPvJzX3BZjH9uxvk7XxoGpT2mV LOpm7oQ2VaL+si24B1FqQ9DzgAeQeeZ8ZRJhs10joIgQwm6PnLCH7ID+9z40W568K2U2jT mBdGstDTttCMqHIsrkTfR7i1Zd/4Y1I= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-264--LXSPE2rMSypjM5LhrD-Dw-1; Fri, 17 Nov 2023 13:44:13 -0500 X-MC-Unique: -LXSPE2rMSypjM5LhrD-Dw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F00C08007B3 for ; Fri, 17 Nov 2023 18:44:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9C6362026D66 for ; Fri, 17 Nov 2023 18:44:12 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 1/3] stdlib: Avoid another self-comparison in qsort In-Reply-To: Message-ID: <58229f369d7bb3eb1883de9ba0ff2ad974853c5b.1700246487.git.fweimer@redhat.com> References: X-From-Line: 58229f369d7bb3eb1883de9ba0ff2ad974853c5b Mon Sep 17 00:00:00 2001 Date: Fri, 17 Nov 2023 19:44:10 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org In the insertion phase, we could run off the start of the array if the comparison function never runs zero. In that case, it never finds the initial element that terminates the iteration. Reviewed-by: Adhemerval Zanella --- stdlib/qsort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index ad110e8a89..6d0c4447ec 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -217,7 +217,7 @@ insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, while ((run_ptr += size) <= end_ptr) { tmp_ptr = run_ptr - size; - while (cmp (run_ptr, tmp_ptr, arg) < 0) + while (run_ptr != tmp_ptr && cmp (run_ptr, tmp_ptr, arg) < 0) tmp_ptr -= size; tmp_ptr += size;