From owner-freebsd-bugs@freebsd.org Mon Sep 28 15:55:01 2020 Return-Path: Delivered-To: freebsd-bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 6CB30425A21 for ; Mon, 28 Sep 2020 15:55:01 +0000 (UTC) (envelope-from colomar.6.4.3@gmail.com) Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4C0RtD4nN9z4WGk for ; Mon, 28 Sep 2020 15:55:00 +0000 (UTC) (envelope-from colomar.6.4.3@gmail.com) Received: by mail-wr1-x444.google.com with SMTP id e16so1948537wrm.2 for ; Mon, 28 Sep 2020 08:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lPOwEktm/a1+miJXV4DjTyQrmZwM4wuV0jls+bo30zk=; b=gZxslsB0uhcACXQftOb245oFo6yXBJzBWtnKj0uEW8rUzEF3Y1zghPGdYdgkpHd7aG UpbhZCP+NDLIAoGRcgB95Ekg/UiDXxZfs2NjAR+dlZE9hs/ML3KReUBN8K8hRXkV5scS sTqmwqBJSO4j59L6qJIk5w7pgb+szE7hBGijBbH0XErSrpqVgMI73Y6HCiFE7twpJILC 4dchVdVWfFRqJz1PsDZXqVt7/a0T62OLFFJcU/aou3uHOJ1g0LVlRnv1p62SxfUbHADR kO3793NxGnNu0G7XG/59FepXPf/lUggVSlqS3/9MH1vQkONfvS1oPwmmtSjNt+2/fy9l xrIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lPOwEktm/a1+miJXV4DjTyQrmZwM4wuV0jls+bo30zk=; b=jlFW4zU0KaYc+d6x1FWVSwFc/QpXWk1nnwoPQqfzyXZQgIM3c8S1FHX5dbZT7Ug+Je uyujDrMIKipRJ5JPfcGCaADr7Oh3MDYpHoXUmc67SWSdikMXws/XfJU8PMOTzZrYGy58 etRFfugOKL8iQGK47ZMCRyJSVuF8G4y34GE7AdXOrcIw4jP8jFcbNwTWo9OCAD8wiTCw fNuz/ndyHf7ahgFPM0YSDPR6W88ZoOMELWLmos8pFLG6/r/jJNZF2SSst4V+FFyQBtQm uNTuamOeF+wajr7PqDkBYcyMykAhVpUnARnFabYttleWa4/qQ8uakAkgPMMihOOJH8x2 KXag== X-Gm-Message-State: AOAM533CGS77TetlFJXx2hpHp/1RfbX7JMddiCnHZlUAss0qC25lqEEL g+u+Lm4LsUMPTH11LjtYvUIbC2yDU/o= X-Google-Smtp-Source: ABdhPJxXDRSEDzKS+wwMtmWRrD2t7gJUKEDA5fXLORjxW2CXCzc4DaDEifIuI3cUIj+qDVcJtjO7PA== X-Received: by 2002:a5d:6886:: with SMTP id h6mr2525335wru.374.1601308498528; Mon, 28 Sep 2020 08:54:58 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id y5sm1777137wmg.21.2020.09.28.08.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 08:54:57 -0700 (PDT) From: Alejandro Colomar To: freebsd-bugs@freebsd.org Cc: Alejandro Colomar Subject: [PATCH] : Add safety to nitems(): check that argument is an array Date: Mon, 28 Sep 2020 17:54:33 +0200 Message-Id: <20200928155432.22245-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4C0RtD4nN9z4WGk X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=gZxslsB0; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of colomar643@gmail.com designates 2a00:1450:4864:20::444 as permitted sender) smtp.mailfrom=colomar643@gmail.com X-Spamd-Result: default: False [0.64 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; R_MISSING_CHARSET(2.50)[]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; BROKEN_CONTENT_TYPE(1.50)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; NEURAL_HAM_SHORT(-0.42)[-0.423]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; MID_RHS_MATCH_FROM(0.00)[]; TAGGED_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.94)[-0.936]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; RECEIVED_SPAMHAUS_PBL(0.00)[170.253.60.68:received]; FROM_HAS_DN(0.00)[]; NEURAL_HAM_LONG(-1.00)[-0.997]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-bugs@freebsd.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::444:from]; FREEMAIL_CC(0.00)[gmail.com]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[freebsd-bugs] X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Sep 2020 15:55:01 -0000 'nitems()' calculates the length of an array in number of items. It is safe: if a pointer is passed to the macro (or function, in C++), the compilation is broken due to: - In >= C11: _Static_assert() - In C89, C99: Negative anonymous bitfield - In C++: The template requires an array This patch also adds some other macros, which are required by 'nitems()': __is_same_type(_a, _b): Returns non-zero if the two input arguments are of the same type. __is_array(_a): Returns non-zero if the input argument is of an array type. __must_be(_e, _msg): Allows using _Static_assert() everywhere an expression can be used. It evaluates '(int)0' or breaks the compilation. __must_be_array(_a): It evaluates to '(int)0' if the argument is of an array type. Else, it breaks compilation. __nitems(_a): It implements the basic sizeof division needed to calculate the array length. It does what nitems(_a) did before this patch. I'd like to put the contents of this patch in the public domain. Feel free to do anything with it. Signed-off-by: Alejandro Colomar --- Hi, I sent a patch like this one to glibc a few days ago. I hope it's good for you too :-) Disclaimer: I don't have a FreeBSD system, so the code is not tested there. Please test that it doesn't break anything. Cheers, Alex sys/sys/param.h | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/sys/sys/param.h b/sys/sys/param.h index d03f64f13a6..d512d21768a 100644 --- a/sys/sys/param.h +++ b/sys/sys/param.h @@ -299,7 +299,6 @@ #ifndef howmany #define howmany(x, y) (((x)+((y)-1))/(y)) #endif -#define nitems(x) (sizeof((x)) / sizeof((x)[0])) #define rounddown(x, y) (((x)/(y))*(y)) #define rounddown2(x, y) ((x)&(~((y)-1))) /* if y is power of two */ #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ @@ -310,6 +309,54 @@ #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) +/* Macros related to the types of variables */ +#define __is_same_type(_a, _b) ( \ + __builtin_types_compatible_p(__typeof__(_a), __typeof__(_b)) \ +) +#define __is_array(_a) (!__is_same_type((_a), &(_a)[0])) + +/* Macros for embedding _Static_assert() in expressions */ +#if __STDC_VERSION__ >= 201112L +# define __must_be(_e, _msg) ( \ + 0 * (int)sizeof( \ + struct { \ + _Static_assert((_e), _msg); \ + char_ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#else /* __STDC_VERSION__ < 201112L */ +# define __must_be(_e, _msg) ( \ + 0 * (int)sizeof( \ + struct { \ + int : (-!(_e)); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#endif /* __STDC_VERSION__ < 201112L */ +#define __must_be_array(_a) __must_be(__is_array(_a), "Must be an array!") + +/* Macros for array sizes */ +#if defined(__cplusplus) +# if __cplusplus >= 201103L +template + constexpr inline std::size_t + nitems(const _tp(&)[_len]) noexcept +{ + return _len; +} +# else /* __cplusplus < 201103L */ +template + char + (&__nitems_chararr(const _Tp(&)[_len]))[_len]; +# define nitems(_a) (sizeof(__nitems_chararr(_a))) +# endif /* __cplusplus < 201103L */ +#else /* !defined(__cplusplus) */ +# define __nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) +# define nitems(_a) (__nitems(_a) + __must_be_array(_a)) +#endif /* !defined(__cplusplus) */ + #ifdef _KERNEL /* * Basic byte order function prototypes for non-inline functions. -- 2.28.0