Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1 | From 1c01a5bc9ac7f8aaa484b1a8e0e74aa5f8899d0e Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Sun, 8 Nov 2020 11:17:59 -0800 |
| 4 | Subject: [PATCH] malloc: Check for excessive values to malloc |
| 5 | |
| 6 | with whole program optimizers like lto smalloc() |
| 7 | is inlined the excessive constant argument is propagated to |
| 8 | malloc() and ultimately triggers the warning. |
| 9 | |
| 10 | malloc.c:15:9: error: argument 1 range [18446744065119617024, 18446744073709551580] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] |
| 11 | |
| 12 | therefore add a check before excessive constant argument before calling |
| 13 | malloc |
| 14 | |
| 15 | Note that this will not happen with normal compile since they happen to |
| 16 | be in different translation units and compiler can not semantically |
| 17 | analyze as much |
| 18 | |
| 19 | Upstream-Status: Pending |
| 20 | |
| 21 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 22 | --- |
| 23 | malloc.c | 3 +++ |
| 24 | 1 file changed, 3 insertions(+) |
| 25 | |
| 26 | diff --git a/malloc.c b/malloc.c |
| 27 | index a7fa7c5..520377c 100644 |
| 28 | --- a/malloc.c |
| 29 | +++ b/malloc.c |
| 30 | @@ -2,6 +2,7 @@ |
| 31 | * malloc.c: safe wrappers around malloc, realloc, free, strdup |
| 32 | */ |
| 33 | |
| 34 | +#include <stdint.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include "puzzles.h" |
| 38 | @@ -12,6 +13,8 @@ |
| 39 | */ |
| 40 | void *smalloc(size_t size) { |
| 41 | void *p; |
| 42 | + if (size > PTRDIFF_MAX) |
| 43 | + fatal("exceeds maximum object size"); |
| 44 | p = malloc(size); |
| 45 | if (!p) |
| 46 | fatal("out of memory"); |
| 47 | -- |
| 48 | 2.29.2 |
| 49 | |