From nobody Wed Apr 19 13:35:50 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q1hd23lRXz45ynV; Wed, 19 Apr 2023 13:35:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q1hd22sTJz4WND; Wed, 19 Apr 2023 13:35:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681911350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K+OGjD6cAx32usjbu76bw7lJHob95+Rp0/dYS1uPVJ0=; b=bLoN9X2131xZ/Adod1TNnd3DpEqNf5XT0SORhvQRtkU5Djo816fnbGqOxYkGbGS1RJn808 Em6nhoTGyyG7G1mEzNcJY9by7e3lHQhvYwczdSSafE+EsysU37Ar0FIFS8XHcQ1u8TNudy olMAY5XxMTNpbExjq4KLg9xa0WPCfJtqwNdksPYiaEDCY77D9Sw1mqreXvzo5hkwDz8/qW 7qQkIADZnLcFM2iRZ8sywFyCHj5q8+Al0gIpxwyX65vrvi1O27BULMrjkTX0S04V1fqYVJ BCNlAPLFssc4iYFXkN2CqX1Qbj3m5p80if7FXFeCsKePzKJqXBEYUX8cT6ExUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681911350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K+OGjD6cAx32usjbu76bw7lJHob95+Rp0/dYS1uPVJ0=; b=xvlNXrOMbVlIiJH2dnUPTScu8BNYyHWIvL4Lin22Ehldq8hRxxq+ERhdxypH/ScHxONNYi VxpFLtxxRz2fiCNocvjRQNywdW8wB4As6PSdteZPXavAcb3hqF9Oj/PjYdq3vx2TKbZSuI g9MI+VnXOR69IpsmnK+WC5jNovwEWC203hXUiowMVHKinsYZgi4oYJf/P06wJJJm14eQAq sWriuy+3cBjXP9TvN6BRJ/TGltVq5yXGX44sOGCc2Dws6z8liJm8DuxeKjXfWzCKaO1OOf IAfH+oFWl6mh/CY6VPiuAml2m0HFndxe301bRA5jfZOhIyLr1Dr++WMhg6E0bw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1681911350; a=rsa-sha256; cv=none; b=Wv6kAww5MWOwUn1Ki3MQYntvANECjQOhbAZM+mBt7l1ij19YH86CyQtWLUtoiM+fVDCKQr Mn3c3xYb7DhhEhypRmW4LoIAv00/14qeEWFSoLwLnaTpgpB1sYpvzaAkuieQre2QThryA6 XEVqmOax6Sat/Q4pjV/29kcgbt1B8wHAZltFZDRyRtC+RvpNNiTK3v0ee9UypPCEf09Fvh gPyPjB33fFfLCljiJkUcILcVeK/xTm0+GjmCdglfk/lEqq/6uIBlo7jfJpSwyhP095Tm3R /Cua8O0fVbKbOigwFns1IHFZgVAYfFbY9J8Cnedr0Vbjamh518g3jaf3wT04hw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q1hd21gGfzwHc; Wed, 19 Apr 2023 13:35:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33JDZoLt010134; Wed, 19 Apr 2023 13:35:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33JDZoVV010133; Wed, 19 Apr 2023 13:35:50 GMT (envelope-from git) Date: Wed, 19 Apr 2023 13:35:50 GMT Message-Id: <202304191335.33JDZoVV010133@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hans Petter Selasky Subject: git: 27bb0d337c0d - main - libc: Add missing object size check to qsort_s(3) List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hselasky X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 27bb0d337c0d82a1a4f310315840236eb239963c Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=27bb0d337c0d82a1a4f310315840236eb239963c commit 27bb0d337c0d82a1a4f310315840236eb239963c Author: Hans Petter Selasky AuthorDate: 2023-04-19 10:22:11 +0000 Commit: Hans Petter Selasky CommitDate: 2023-04-19 13:35:14 +0000 libc: Add missing object size check to qsort_s(3) When sorting, both the C11 standard (ISO/IEC 9899:2011, K.3.6.3.2) and the ISO/IEC JTC1 SC22 WG14 N1172 standard, does not define objects of zero size as undefined behaviour. However Microsoft's cpp-docs does. Add proper checks for this. Found while working on bsort(3). Reviewed by: kib@ and emaste@ MFC after: 1 week Sponsored by: NVIDIA Networking Differential Revision: https://reviews.freebsd.org/D39687 --- lib/libc/stdlib/qsort.3 | 10 +++++++--- lib/libc/stdlib/qsort.c | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/libc/stdlib/qsort.3 b/lib/libc/stdlib/qsort.3 index 6449849fc490..01689dbafec8 100644 --- a/lib/libc/stdlib/qsort.3 +++ b/lib/libc/stdlib/qsort.3 @@ -32,7 +32,7 @@ .\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd September 30, 2022 +.Dd April 19, 2023 .Dt QSORT 3 .Os .Sh NAME @@ -260,7 +260,7 @@ The order of arguments to .Fa compar is different .It -if +If .Fa nmemb or .Fa size @@ -270,7 +270,11 @@ or .Fa nmemb is not zero and .Fa compar -is NULL, then the runtime-constraint handler is called, and +is +.Dv NULL +or +.Fa size +is zero, then the runtime-constraint handler is called, and .Fn qsort_s returns an error. Note that the handler is called before diff --git a/lib/libc/stdlib/qsort.c b/lib/libc/stdlib/qsort.c index 11a3c5508143..425edd562420 100644 --- a/lib/libc/stdlib/qsort.c +++ b/lib/libc/stdlib/qsort.c @@ -247,6 +247,10 @@ qsort_s(void *a, rsize_t n, rsize_t es, cmp_t *cmp, void *thunk) __throw_constraint_handler_s("qsort_s : cmp == NULL", EINVAL); return (EINVAL); + } else if (es <= 0) { + __throw_constraint_handler_s("qsort_s : es <= 0", + EINVAL); + return (EINVAL); } }