diff options
Diffstat (limited to 'gl/xsize.h')
| -rw-r--r-- | gl/xsize.h | 20 |
1 files changed, 15 insertions, 5 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | /* xsize.h -- Checked size_t computations. | 1 | /* xsize.h -- Checked size_t computations. |
| 2 | 2 | ||
| 3 | Copyright (C) 2003, 2008-2024 Free Software Foundation, Inc. | 3 | Copyright (C) 2003, 2008-2025 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | This file is free software: you can redistribute it and/or modify | 5 | This file is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU Lesser General Public License as | 6 | it under the terms of the GNU Lesser General Public License as |
| @@ -26,7 +26,7 @@ | |||
| 26 | /* Get size_t. */ | 26 | /* Get size_t. */ |
| 27 | #include <stddef.h> | 27 | #include <stddef.h> |
| 28 | 28 | ||
| 29 | /* Get SIZE_MAX. */ | 29 | /* Get INT_MAX, SIZE_MAX. */ |
| 30 | #include <limits.h> | 30 | #include <limits.h> |
| 31 | #if HAVE_STDINT_H | 31 | #if HAVE_STDINT_H |
| 32 | # include <stdint.h> | 32 | # include <stdint.h> |
| @@ -61,7 +61,8 @@ extern "C" { | |||
| 61 | void *p = (size_in_bounds_p (size) ? malloc (size) : NULL); | 61 | void *p = (size_in_bounds_p (size) ? malloc (size) : NULL); |
| 62 | */ | 62 | */ |
| 63 | 63 | ||
| 64 | /* Convert an arbitrary value >= 0 to type size_t. */ | 64 | /* Convert an arbitrary N >= 0 to type size_t. |
| 65 | N should not have side effects. */ | ||
| 65 | #define xcast_size_t(N) \ | 66 | #define xcast_size_t(N) \ |
| 66 | ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX) | 67 | ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX) |
| 67 | 68 | ||
| @@ -69,8 +70,15 @@ extern "C" { | |||
| 69 | XSIZE_INLINE size_t ATTRIBUTE_PURE | 70 | XSIZE_INLINE size_t ATTRIBUTE_PURE |
| 70 | xsum (size_t size1, size_t size2) | 71 | xsum (size_t size1, size_t size2) |
| 71 | { | 72 | { |
| 72 | size_t sum = size1 + size2; | 73 | if (INT_MAX < SIZE_MAX) |
| 73 | return (sum >= size1 ? sum : SIZE_MAX); | 74 | { |
| 75 | /* Optimize for the common case where size_t arithmetic wraps | ||
| 76 | around without undefined behavior. */ | ||
| 77 | size_t sum = size1 + size2; | ||
| 78 | return size1 <= sum ? sum : SIZE_MAX; | ||
| 79 | } | ||
| 80 | |||
| 81 | return size1 <= SIZE_MAX - size2 ? size1 + size2 : SIZE_MAX; | ||
| 74 | } | 82 | } |
| 75 | 83 | ||
| 76 | /* Sum of three sizes, with overflow check. */ | 84 | /* Sum of three sizes, with overflow check. */ |
| @@ -98,6 +106,8 @@ xmax (size_t size1, size_t size2) | |||
| 98 | 106 | ||
| 99 | /* Multiplication of a count with an element size, with overflow check. | 107 | /* Multiplication of a count with an element size, with overflow check. |
| 100 | The count must be >= 0 and the element size must be > 0. | 108 | The count must be >= 0 and the element size must be > 0. |
| 109 | Arguments should not have side effects. | ||
| 110 | The element size's type should be no wider than size_t. | ||
| 101 | This is a macro, not a function, so that it works correctly even | 111 | This is a macro, not a function, so that it works correctly even |
| 102 | when N is of a wider type and N > SIZE_MAX. */ | 112 | when N is of a wider type and N > SIZE_MAX. */ |
| 103 | #define xtimes(N, ELSIZE) \ | 113 | #define xtimes(N, ELSIZE) \ |
