From: Ammar Faizi <[email protected]>
To: "Paul E. McKenney" <[email protected]>, Willy Tarreau <[email protected]>
Cc: Ammar Faizi <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
Linux Kernel Mailing List <[email protected]>,
GNU/Weeb Mailing List <[email protected]>,
Facebook Kernel Team <[email protected]>
Subject: [PATCH v1 1/2] tools/nolibc/stdlib: Support overflow checking for older compiler versions
Date: Fri, 20 May 2022 00:21:15 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
Previously, we used __builtin_mul_overflow() to check for overflow in
the multiplication operation in the calloc() function. However, older
compiler versions don't support this built-in. This patch changes the
overflow checking mechanism to make it work on any compiler version
by using a division method to check for overflow. No functional change
intended. While in there, remove the unused variable `void *orig`.
Link: https://lore.kernel.org/lkml/[email protected]
Suggested-by: Willy Tarreau <[email protected]>
Cc: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
tools/include/nolibc/stdlib.h | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 8fd32eaf8037..92378c4b9660 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -128,10 +128,9 @@ void *malloc(size_t len)
static __attribute__((unused))
void *calloc(size_t size, size_t nmemb)
{
- void *orig;
- size_t res = 0;
+ size_t x = size * nmemb;
- if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) {
+ if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
SET_ERRNO(ENOMEM);
return NULL;
}
@@ -140,7 +139,7 @@ void *calloc(size_t size, size_t nmemb)
* No need to zero the heap, the MAP_ANONYMOUS in malloc()
* already does it.
*/
- return malloc(res);
+ return malloc(x);
}
static __attribute__((unused))
--
Ammar Faizi
next prev parent reply other threads:[~2022-05-19 17:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-19 17:21 [PATCH v1 0/2] nolibc updates for Linux 5.20 Ammar Faizi
2022-05-19 17:21 ` Ammar Faizi [this message]
2022-05-20 4:19 ` [PATCH v1 1/2] tools/nolibc/stdlib: Support overflow checking for older compiler versions Willy Tarreau
2022-05-20 11:29 ` Alviro Iskandar Setiawan
2022-05-20 17:32 ` Paul E. McKenney
2022-05-19 17:21 ` [PATCH v1 2/2] tools/nolibc/stdio: Add format attribute to enable printf warnings Ammar Faizi
2022-05-20 4:21 ` Willy Tarreau
2022-05-20 4:23 ` [PATCH v1 0/2] nolibc updates for Linux 5.20 Willy Tarreau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox