|
|
9bb5d6 |
Enhance setuid-tunables test
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
Instead of passing GLIBC_TUNABLES via the environment, pass the
|
|
|
9bb5d6 |
environment variable from parent to child. This allows us to test
|
|
|
9bb5d6 |
multiple variables to ensure better coverage.
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
The test list currently only includes the case that's already being
|
|
|
9bb5d6 |
tested. More tests will be added later.
|
|
|
9bb5d6 |
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
(cherry picked from commit 061fe3f8add46a89b7453e87eabb9c4695005ced)
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
Also add intprops.h from 2.29 from commit 8e6fd2bdb21efe2cc1ae7571ff8fb2599db6a05a
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
diff --git a/elf/Makefile b/elf/Makefile
|
|
|
9bb5d6 |
index fc9c685b9d23bb6c..2093cefa7e73349e 100644
|
|
|
9bb5d6 |
--- a/elf/Makefile
|
|
|
9bb5d6 |
+++ b/elf/Makefile
|
|
|
9bb5d6 |
@@ -1597,8 +1597,6 @@ $(objpfx)tst-nodelete-dlclose.out: $(objpfx)tst-nodelete-dlclose-dso.so \
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
tst-env-setuid-ENV = MALLOC_CHECK_=2 MALLOC_MMAP_THRESHOLD_=4096 \
|
|
|
9bb5d6 |
LD_HWCAP_MASK=0x1
|
|
|
9bb5d6 |
-tst-env-setuid-tunables-ENV = \
|
|
|
9bb5d6 |
- GLIBC_TUNABLES=glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
$(objpfx)tst-debug1: $(libdl)
|
|
|
9bb5d6 |
$(objpfx)tst-debug1.out: $(objpfx)tst-debug1mod1.so
|
|
|
9bb5d6 |
diff --git a/elf/tst-env-setuid-tunables.c b/elf/tst-env-setuid-tunables.c
|
|
|
9bb5d6 |
index d7c4f0d5742cd526..a48281b175af6920 100644
|
|
|
9bb5d6 |
--- a/elf/tst-env-setuid-tunables.c
|
|
|
9bb5d6 |
+++ b/elf/tst-env-setuid-tunables.c
|
|
|
9bb5d6 |
@@ -25,35 +25,50 @@
|
|
|
9bb5d6 |
#include "config.h"
|
|
|
9bb5d6 |
#undef _LIBC
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
-#define test_parent test_parent_tunables
|
|
|
9bb5d6 |
-#define test_child test_child_tunables
|
|
|
9bb5d6 |
-
|
|
|
9bb5d6 |
-static int test_child_tunables (void);
|
|
|
9bb5d6 |
-static int test_parent_tunables (void);
|
|
|
9bb5d6 |
-
|
|
|
9bb5d6 |
-#include "tst-env-setuid.c"
|
|
|
9bb5d6 |
+#include <errno.h>
|
|
|
9bb5d6 |
+#include <fcntl.h>
|
|
|
9bb5d6 |
+#include <stdlib.h>
|
|
|
9bb5d6 |
+#include <stdint.h>
|
|
|
9bb5d6 |
+#include <stdio.h>
|
|
|
9bb5d6 |
+#include <string.h>
|
|
|
9bb5d6 |
+#include <sys/stat.h>
|
|
|
9bb5d6 |
+#include <sys/wait.h>
|
|
|
9bb5d6 |
+#include <unistd.h>
|
|
|
9bb5d6 |
+#include <intprops.h>
|
|
|
9bb5d6 |
+#include <array_length.h>
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#include <support/check.h>
|
|
|
9bb5d6 |
+#include <support/support.h>
|
|
|
9bb5d6 |
+#include <support/test-driver.h>
|
|
|
9bb5d6 |
+#include <support/capture_subprocess.h>
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+const char *teststrings[] =
|
|
|
9bb5d6 |
+{
|
|
|
9bb5d6 |
+ "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
|
|
|
9bb5d6 |
+};
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
-#define CHILD_VALSTRING_VALUE "glibc.malloc.mmap_threshold=4096"
|
|
|
9bb5d6 |
-#define PARENT_VALSTRING_VALUE \
|
|
|
9bb5d6 |
- "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096"
|
|
|
9bb5d6 |
+const char *resultstrings[] =
|
|
|
9bb5d6 |
+{
|
|
|
9bb5d6 |
+ "glibc.malloc.mmap_threshold=4096",
|
|
|
9bb5d6 |
+};
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
static int
|
|
|
9bb5d6 |
-test_child_tunables (void)
|
|
|
9bb5d6 |
+test_child (int off)
|
|
|
9bb5d6 |
{
|
|
|
9bb5d6 |
const char *val = getenv ("GLIBC_TUNABLES");
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
#if HAVE_TUNABLES
|
|
|
9bb5d6 |
- if (val != NULL && strcmp (val, CHILD_VALSTRING_VALUE) == 0)
|
|
|
9bb5d6 |
+ if (val != NULL && strcmp (val, resultstrings[off]) == 0)
|
|
|
9bb5d6 |
return 0;
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
if (val != NULL)
|
|
|
9bb5d6 |
- printf ("Unexpected GLIBC_TUNABLES VALUE %s\n", val);
|
|
|
9bb5d6 |
+ printf ("[%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
return 1;
|
|
|
9bb5d6 |
#else
|
|
|
9bb5d6 |
if (val != NULL)
|
|
|
9bb5d6 |
{
|
|
|
9bb5d6 |
- printf ("GLIBC_TUNABLES not cleared\n");
|
|
|
9bb5d6 |
+ printf ("[%d] GLIBC_TUNABLES not cleared\n", off);
|
|
|
9bb5d6 |
return 1;
|
|
|
9bb5d6 |
}
|
|
|
9bb5d6 |
return 0;
|
|
|
9bb5d6 |
@@ -61,15 +76,48 @@ test_child_tunables (void)
|
|
|
9bb5d6 |
}
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
static int
|
|
|
9bb5d6 |
-test_parent_tunables (void)
|
|
|
9bb5d6 |
+do_test (int argc, char **argv)
|
|
|
9bb5d6 |
{
|
|
|
9bb5d6 |
- const char *val = getenv ("GLIBC_TUNABLES");
|
|
|
9bb5d6 |
+ /* Setgid child process. */
|
|
|
9bb5d6 |
+ if (argc == 2)
|
|
|
9bb5d6 |
+ {
|
|
|
9bb5d6 |
+ if (getgid () == getegid ())
|
|
|
9bb5d6 |
+ /* This can happen if the file system is mounted nosuid. */
|
|
|
9bb5d6 |
+ FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
|
|
|
9bb5d6 |
+ (intmax_t) getgid ());
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
- if (val != NULL && strcmp (val, PARENT_VALSTRING_VALUE) == 0)
|
|
|
9bb5d6 |
- return 0;
|
|
|
9bb5d6 |
+ int ret = test_child (atoi (argv[1]));
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
- if (val != NULL)
|
|
|
9bb5d6 |
- printf ("Unexpected GLIBC_TUNABLES VALUE %s\n", val);
|
|
|
9bb5d6 |
+ if (ret != 0)
|
|
|
9bb5d6 |
+ exit (1);
|
|
|
9bb5d6 |
|
|
|
9bb5d6 |
- return 1;
|
|
|
9bb5d6 |
+ exit (EXIT_SUCCESS);
|
|
|
9bb5d6 |
+ }
|
|
|
9bb5d6 |
+ else
|
|
|
9bb5d6 |
+ {
|
|
|
9bb5d6 |
+ int ret = 0;
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ /* Spawn tests. */
|
|
|
9bb5d6 |
+ for (int i = 0; i < array_length (teststrings); i++)
|
|
|
9bb5d6 |
+ {
|
|
|
9bb5d6 |
+ char buf[INT_BUFSIZE_BOUND (int)];
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ printf ("Spawned test for %s (%d)\n", teststrings[i], i);
|
|
|
9bb5d6 |
+ snprintf (buf, sizeof (buf), "%d\n", i);
|
|
|
9bb5d6 |
+ if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
|
|
|
9bb5d6 |
+ exit (1);
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ int status = support_capture_subprogram_self_sgid (buf);
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ /* Bail out early if unsupported. */
|
|
|
9bb5d6 |
+ if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
|
|
|
9bb5d6 |
+ return EXIT_UNSUPPORTED;
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ ret |= status;
|
|
|
9bb5d6 |
+ }
|
|
|
9bb5d6 |
+ return ret;
|
|
|
9bb5d6 |
+ }
|
|
|
9bb5d6 |
}
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#define TEST_FUNCTION_ARGV do_test
|
|
|
9bb5d6 |
+#include <support/test-driver.c>
|
|
|
9bb5d6 |
diff --git a/include/intprops.h b/include/intprops.h
|
|
|
9bb5d6 |
new file mode 100644
|
|
|
9bb5d6 |
index 0000000000000000..9702aec4c6e3c80a
|
|
|
9bb5d6 |
--- /dev/null
|
|
|
9bb5d6 |
+++ b/include/intprops.h
|
|
|
9bb5d6 |
@@ -0,0 +1,455 @@
|
|
|
9bb5d6 |
+/* intprops.h -- properties of integer types
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Copyright (C) 2001-2018 Free Software Foundation, Inc.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ This program is free software: you can redistribute it and/or modify it
|
|
|
9bb5d6 |
+ under the terms of the GNU Lesser General Public License as published
|
|
|
9bb5d6 |
+ by the Free Software Foundation; either version 2.1 of the License, or
|
|
|
9bb5d6 |
+ (at your option) any later version.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ This program is distributed in the hope that it will be useful,
|
|
|
9bb5d6 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
9bb5d6 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
9bb5d6 |
+ GNU Lesser General Public License for more details.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ You should have received a copy of the GNU Lesser General Public License
|
|
|
9bb5d6 |
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Written by Paul Eggert. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#ifndef _GL_INTPROPS_H
|
|
|
9bb5d6 |
+#define _GL_INTPROPS_H
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#include <limits.h>
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return a value with the common real type of E and V and the value of V.
|
|
|
9bb5d6 |
+ Do not evaluate E. */
|
|
|
9bb5d6 |
+#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
|
|
|
9bb5d6 |
+ <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
|
|
|
9bb5d6 |
+#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* The extra casts in the following macros work around compiler bugs,
|
|
|
9bb5d6 |
+ e.g., in Cray C 5.0.3.0. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* True if the arithmetic type T is an integer type. bool counts as
|
|
|
9bb5d6 |
+ an integer. */
|
|
|
9bb5d6 |
+#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* True if the real type T is signed. */
|
|
|
9bb5d6 |
+#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if the real expression E, after promotion, has a
|
|
|
9bb5d6 |
+ signed or floating type. Do not evaluate E. */
|
|
|
9bb5d6 |
+#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Minimum and maximum values for integer types and expressions. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* The width in bits of the integer type or expression T.
|
|
|
9bb5d6 |
+ Do not evaluate T.
|
|
|
9bb5d6 |
+ Padding bits are not supported; this is checked at compile-time below. */
|
|
|
9bb5d6 |
+#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* The maximum and minimum values for the integer type T. */
|
|
|
9bb5d6 |
+#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
|
|
|
9bb5d6 |
+#define TYPE_MAXIMUM(t) \
|
|
|
9bb5d6 |
+ ((t) (! TYPE_SIGNED (t) \
|
|
|
9bb5d6 |
+ ? (t) -1 \
|
|
|
9bb5d6 |
+ : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* The maximum and minimum values for the type of the expression E,
|
|
|
9bb5d6 |
+ after integer promotion. E is not evaluated. */
|
|
|
9bb5d6 |
+#define _GL_INT_MINIMUM(e) \
|
|
|
9bb5d6 |
+ (EXPR_SIGNED (e) \
|
|
|
9bb5d6 |
+ ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
|
|
|
9bb5d6 |
+ : _GL_INT_CONVERT (e, 0))
|
|
|
9bb5d6 |
+#define _GL_INT_MAXIMUM(e) \
|
|
|
9bb5d6 |
+ (EXPR_SIGNED (e) \
|
|
|
9bb5d6 |
+ ? _GL_SIGNED_INT_MAXIMUM (e) \
|
|
|
9bb5d6 |
+ : _GL_INT_NEGATE_CONVERT (e, 1))
|
|
|
9bb5d6 |
+#define _GL_SIGNED_INT_MAXIMUM(e) \
|
|
|
9bb5d6 |
+ (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Work around OpenVMS incompatibility with C99. */
|
|
|
9bb5d6 |
+#if !defined LLONG_MAX && defined __INT64_MAX
|
|
|
9bb5d6 |
+# define LLONG_MAX __INT64_MAX
|
|
|
9bb5d6 |
+# define LLONG_MIN __INT64_MIN
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* This include file assumes that signed types are two's complement without
|
|
|
9bb5d6 |
+ padding bits; the above macros have undefined behavior otherwise.
|
|
|
9bb5d6 |
+ If this is a problem for you, please let us know how to fix it for your host.
|
|
|
9bb5d6 |
+ This assumption is tested by the intprops-tests module. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Does the __typeof__ keyword work? This could be done by
|
|
|
9bb5d6 |
+ 'configure', but for now it's easier to do it by hand. */
|
|
|
9bb5d6 |
+#if (2 <= __GNUC__ \
|
|
|
9bb5d6 |
+ || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
|
|
|
9bb5d6 |
+ || (0x5110 <= __SUNPRO_C && !__STDC__))
|
|
|
9bb5d6 |
+# define _GL_HAVE___TYPEOF__ 1
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL_HAVE___TYPEOF__ 0
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if the integer type or expression T might be signed. Return 0
|
|
|
9bb5d6 |
+ if it is definitely unsigned. This macro does not evaluate its argument,
|
|
|
9bb5d6 |
+ and expands to an integer constant expression. */
|
|
|
9bb5d6 |
+#if _GL_HAVE___TYPEOF__
|
|
|
9bb5d6 |
+# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL_SIGNED_TYPE_OR_EXPR(t) 1
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Bound on length of the string representing an unsigned integer
|
|
|
9bb5d6 |
+ value representable in B bits. log10 (2.0) < 146/485. The
|
|
|
9bb5d6 |
+ smallest value of B where this bound is not tight is 2621. */
|
|
|
9bb5d6 |
+#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Bound on length of the string representing an integer type or expression T.
|
|
|
9bb5d6 |
+ Subtract 1 for the sign bit if T is signed, and then add 1 more for
|
|
|
9bb5d6 |
+ a minus sign if needed.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
|
|
|
9bb5d6 |
+ signed, this macro may overestimate the true bound by one byte when
|
|
|
9bb5d6 |
+ applied to unsigned types of size 2, 4, 16, ... bytes. */
|
|
|
9bb5d6 |
+#define INT_STRLEN_BOUND(t) \
|
|
|
9bb5d6 |
+ (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
|
|
|
9bb5d6 |
+ + _GL_SIGNED_TYPE_OR_EXPR (t))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Bound on buffer size needed to represent an integer type or expression T,
|
|
|
9bb5d6 |
+ including the terminating null. */
|
|
|
9bb5d6 |
+#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Range overflow checks.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
|
|
|
9bb5d6 |
+ operators might not yield numerically correct answers due to
|
|
|
9bb5d6 |
+ arithmetic overflow. They do not rely on undefined or
|
|
|
9bb5d6 |
+ implementation-defined behavior. Their implementations are simple
|
|
|
9bb5d6 |
+ and straightforward, but they are a bit harder to use than the
|
|
|
9bb5d6 |
+ INT_<op>_OVERFLOW macros described below.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Example usage:
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ long int i = ...;
|
|
|
9bb5d6 |
+ long int j = ...;
|
|
|
9bb5d6 |
+ if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
|
|
|
9bb5d6 |
+ printf ("multiply would overflow");
|
|
|
9bb5d6 |
+ else
|
|
|
9bb5d6 |
+ printf ("product is %ld", i * j);
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Restrictions on *_RANGE_OVERFLOW macros:
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros do not check for all possible numerical problems or
|
|
|
9bb5d6 |
+ undefined or unspecified behavior: they do not check for division
|
|
|
9bb5d6 |
+ by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros may evaluate their arguments zero or multiple times,
|
|
|
9bb5d6 |
+ so the arguments should not have side effects. The arithmetic
|
|
|
9bb5d6 |
+ arguments (including the MIN and MAX arguments) must be of the same
|
|
|
9bb5d6 |
+ integer type after the usual arithmetic conversions, and the type
|
|
|
9bb5d6 |
+ must have minimum value MIN and maximum MAX. Unsigned types should
|
|
|
9bb5d6 |
+ use a zero MIN of the proper type.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros are tuned for constant MIN and MAX. For commutative
|
|
|
9bb5d6 |
+ operations such as A + B, they are also tuned for constant B. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. */
|
|
|
9bb5d6 |
+#define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((b) < 0 \
|
|
|
9bb5d6 |
+ ? (a) < (min) - (b) \
|
|
|
9bb5d6 |
+ : (max) - (b) < (a))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. */
|
|
|
9bb5d6 |
+#define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((b) < 0 \
|
|
|
9bb5d6 |
+ ? (max) + (b) < (a) \
|
|
|
9bb5d6 |
+ : (a) < (min) + (b))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. */
|
|
|
9bb5d6 |
+#define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 \
|
|
|
9bb5d6 |
+ ? (a) < - (max) \
|
|
|
9bb5d6 |
+ : 0 < (a))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. Avoid && and || as they tickle
|
|
|
9bb5d6 |
+ bugs in Sun C 5.11 2010/08/13 and other compilers; see
|
|
|
9bb5d6 |
+ <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
|
|
|
9bb5d6 |
+#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((b) < 0 \
|
|
|
9bb5d6 |
+ ? ((a) < 0 \
|
|
|
9bb5d6 |
+ ? (a) < (max) / (b) \
|
|
|
9bb5d6 |
+ : (b) == -1 \
|
|
|
9bb5d6 |
+ ? 0 \
|
|
|
9bb5d6 |
+ : (min) / (b) < (a)) \
|
|
|
9bb5d6 |
+ : (b) == 0 \
|
|
|
9bb5d6 |
+ ? 0 \
|
|
|
9bb5d6 |
+ : ((a) < 0 \
|
|
|
9bb5d6 |
+ ? (a) < (min) / (b) \
|
|
|
9bb5d6 |
+ : (max) / (b) < (a)))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. Do not check for division by zero. */
|
|
|
9bb5d6 |
+#define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 && (b) == -1 && (a) < - (max))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. Do not check for division by zero.
|
|
|
9bb5d6 |
+ Mathematically, % should never overflow, but on x86-like hosts
|
|
|
9bb5d6 |
+ INT_MIN % -1 traps, and the C standard permits this, so treat this
|
|
|
9bb5d6 |
+ as an overflow too. */
|
|
|
9bb5d6 |
+#define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
|
|
|
9bb5d6 |
+ See above for restrictions. Here, MIN and MAX are for A only, and B need
|
|
|
9bb5d6 |
+ not be of the same type as the other arguments. The C standard says that
|
|
|
9bb5d6 |
+ behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
|
|
|
9bb5d6 |
+ A is negative then A << B has undefined behavior and A >> B has
|
|
|
9bb5d6 |
+ implementation-defined behavior, but do not check these other
|
|
|
9bb5d6 |
+ restrictions. */
|
|
|
9bb5d6 |
+#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((a) < 0 \
|
|
|
9bb5d6 |
+ ? (a) < (min) >> (b) \
|
|
|
9bb5d6 |
+ : (max) >> (b) < (a))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
|
|
|
9bb5d6 |
+#if 5 <= __GNUC__ && !defined __ICC
|
|
|
9bb5d6 |
+# define _GL_HAS_BUILTIN_OVERFLOW 1
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL_HAS_BUILTIN_OVERFLOW 0
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* True if __builtin_add_overflow_p (A, B, C) works. */
|
|
|
9bb5d6 |
+#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* The _GL*_OVERFLOW macros have the same restrictions as the
|
|
|
9bb5d6 |
+ *_RANGE_OVERFLOW macros, except that they do not assume that operands
|
|
|
9bb5d6 |
+ (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
|
|
|
9bb5d6 |
+ that the result (e.g., A + B) has that type. */
|
|
|
9bb5d6 |
+#if _GL_HAS_BUILTIN_OVERFLOW_P
|
|
|
9bb5d6 |
+# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
|
|
|
9bb5d6 |
+# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
|
|
|
9bb5d6 |
+# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL_ADD_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
9bb5d6 |
+ : (a) < 0 ? (b) <= (a) + (b) \
|
|
|
9bb5d6 |
+ : (b) < 0 ? (a) <= (a) + (b) \
|
|
|
9bb5d6 |
+ : (a) + (b) < (b))
|
|
|
9bb5d6 |
+# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
|
|
|
9bb5d6 |
+ : (a) < 0 ? 1 \
|
|
|
9bb5d6 |
+ : (b) < 0 ? (a) - (b) <= (a) \
|
|
|
9bb5d6 |
+ : (a) < (b))
|
|
|
9bb5d6 |
+# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
|
|
|
9bb5d6 |
+ || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
|
|
9bb5d6 |
+ : (a) < 0 ? (b) <= (a) + (b) - 1 \
|
|
|
9bb5d6 |
+ : (b) < 0 && (a) + (b) <= (a))
|
|
|
9bb5d6 |
+#define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
|
|
|
9bb5d6 |
+ ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
|
|
|
9bb5d6 |
+ : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
|
|
|
9bb5d6 |
+ : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return a nonzero value if A is a mathematical multiple of B, where
|
|
|
9bb5d6 |
+ A is unsigned, B is negative, and MAX is the maximum value of A's
|
|
|
9bb5d6 |
+ type. A's type must be the same as (A % B)'s type. Normally (A %
|
|
|
9bb5d6 |
+ -B == 0) suffices, but things get tricky if -B would overflow. */
|
|
|
9bb5d6 |
+#define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
|
|
|
9bb5d6 |
+ (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
|
|
|
9bb5d6 |
+ ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
|
|
|
9bb5d6 |
+ ? (a) \
|
|
|
9bb5d6 |
+ : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
|
|
|
9bb5d6 |
+ : (a) % - (b)) \
|
|
|
9bb5d6 |
+ == 0)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Check for integer overflow, and report low order bits of answer.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
|
|
|
9bb5d6 |
+ might not yield numerically correct answers due to arithmetic overflow.
|
|
|
9bb5d6 |
+ The INT_<op>_WRAPV macros also store the low-order bits of the answer.
|
|
|
9bb5d6 |
+ These macros work correctly on all known practical hosts, and do not rely
|
|
|
9bb5d6 |
+ on undefined behavior due to signed arithmetic overflow.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Example usage, assuming A and B are long int:
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ if (INT_MULTIPLY_OVERFLOW (a, b))
|
|
|
9bb5d6 |
+ printf ("result would overflow\n");
|
|
|
9bb5d6 |
+ else
|
|
|
9bb5d6 |
+ printf ("result is %ld (no overflow)\n", a * b);
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Example usage with WRAPV flavor:
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ long int result;
|
|
|
9bb5d6 |
+ bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
|
|
|
9bb5d6 |
+ printf ("result is %ld (%s)\n", result,
|
|
|
9bb5d6 |
+ overflow ? "after overflow" : "no overflow");
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Restrictions on these macros:
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros do not check for all possible numerical problems or
|
|
|
9bb5d6 |
+ undefined or unspecified behavior: they do not check for division
|
|
|
9bb5d6 |
+ by zero, for bad shift counts, or for shifting negative numbers.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros may evaluate their arguments zero or multiple times, so the
|
|
|
9bb5d6 |
+ arguments should not have side effects.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ The WRAPV macros are not constant expressions. They support only
|
|
|
9bb5d6 |
+ +, binary -, and *. The result type must be signed.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ These macros are tuned for their last argument being a constant.
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
|
|
|
9bb5d6 |
+ A % B, and A << B would overflow, respectively. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#define INT_ADD_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_SUBTRACT_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
|
|
|
9bb5d6 |
+#if _GL_HAS_BUILTIN_OVERFLOW_P
|
|
|
9bb5d6 |
+# define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define INT_NEGATE_OVERFLOW(a) \
|
|
|
9bb5d6 |
+ INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+#define INT_MULTIPLY_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_DIVIDE_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_REMAINDER_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
|
|
|
9bb5d6 |
+ INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
|
|
|
9bb5d6 |
+ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return 1 if the expression A <op> B would overflow,
|
|
|
9bb5d6 |
+ where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
|
|
|
9bb5d6 |
+ assuming MIN and MAX are the minimum and maximum for the result type.
|
|
|
9bb5d6 |
+ Arguments should be free of side effects. */
|
|
|
9bb5d6 |
+#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
|
|
|
9bb5d6 |
+ op_result_overflow (a, b, \
|
|
|
9bb5d6 |
+ _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
|
|
|
9bb5d6 |
+ _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
|
|
|
9bb5d6 |
+ Return 1 if the result overflows. See above for restrictions. */
|
|
|
9bb5d6 |
+#define INT_ADD_WRAPV(a, b, r) \
|
|
|
9bb5d6 |
+ _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_SUBTRACT_WRAPV(a, b, r) \
|
|
|
9bb5d6 |
+ _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
|
|
|
9bb5d6 |
+#define INT_MULTIPLY_WRAPV(a, b, r) \
|
|
|
9bb5d6 |
+ _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
|
|
|
9bb5d6 |
+ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
|
|
|
9bb5d6 |
+ https://llvm.org/bugs/show_bug.cgi?id=25390
|
|
|
9bb5d6 |
+ For now, assume all versions of GCC-like compilers generate bogus
|
|
|
9bb5d6 |
+ warnings for _Generic. This matters only for older compilers that
|
|
|
9bb5d6 |
+ lack __builtin_add_overflow. */
|
|
|
9bb5d6 |
+#if __GNUC__
|
|
|
9bb5d6 |
+# define _GL__GENERIC_BOGUS 1
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL__GENERIC_BOGUS 0
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Store the low-order bits of A <op> B into *R, where OP specifies
|
|
|
9bb5d6 |
+ the operation. BUILTIN is the builtin operation, and OVERFLOW the
|
|
|
9bb5d6 |
+ overflow predicate. Return 1 if the result overflows. See above
|
|
|
9bb5d6 |
+ for restrictions. */
|
|
|
9bb5d6 |
+#if _GL_HAS_BUILTIN_OVERFLOW
|
|
|
9bb5d6 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
|
|
|
9bb5d6 |
+#elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
|
|
|
9bb5d6 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
|
|
|
9bb5d6 |
+ (_Generic \
|
|
|
9bb5d6 |
+ (*(r), \
|
|
|
9bb5d6 |
+ signed char: \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ signed char, SCHAR_MIN, SCHAR_MAX), \
|
|
|
9bb5d6 |
+ short int: \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ short int, SHRT_MIN, SHRT_MAX), \
|
|
|
9bb5d6 |
+ int: \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ int, INT_MIN, INT_MAX), \
|
|
|
9bb5d6 |
+ long int: \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
9bb5d6 |
+ long int, LONG_MIN, LONG_MAX), \
|
|
|
9bb5d6 |
+ long long int: \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
|
|
|
9bb5d6 |
+ long long int, LLONG_MIN, LLONG_MAX)))
|
|
|
9bb5d6 |
+#else
|
|
|
9bb5d6 |
+# define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
|
|
|
9bb5d6 |
+ (sizeof *(r) == sizeof (signed char) \
|
|
|
9bb5d6 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ signed char, SCHAR_MIN, SCHAR_MAX) \
|
|
|
9bb5d6 |
+ : sizeof *(r) == sizeof (short int) \
|
|
|
9bb5d6 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ short int, SHRT_MIN, SHRT_MAX) \
|
|
|
9bb5d6 |
+ : sizeof *(r) == sizeof (int) \
|
|
|
9bb5d6 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
|
|
|
9bb5d6 |
+ int, INT_MIN, INT_MAX) \
|
|
|
9bb5d6 |
+ : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
|
|
|
9bb5d6 |
+# ifdef LLONG_MAX
|
|
|
9bb5d6 |
+# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
|
|
|
9bb5d6 |
+ (sizeof *(r) == sizeof (long int) \
|
|
|
9bb5d6 |
+ ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
9bb5d6 |
+ long int, LONG_MIN, LONG_MAX) \
|
|
|
9bb5d6 |
+ : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
|
|
|
9bb5d6 |
+ long long int, LLONG_MIN, LLONG_MAX))
|
|
|
9bb5d6 |
+# else
|
|
|
9bb5d6 |
+# define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
|
|
|
9bb5d6 |
+ _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
|
|
|
9bb5d6 |
+ long int, LONG_MIN, LONG_MAX)
|
|
|
9bb5d6 |
+# endif
|
|
|
9bb5d6 |
+#endif
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Store the low-order bits of A <op> B into *R, where the operation
|
|
|
9bb5d6 |
+ is given by OP. Use the unsigned type UT for calculation to avoid
|
|
|
9bb5d6 |
+ overflow problems. *R's type is T, with extrema TMIN and TMAX.
|
|
|
9bb5d6 |
+ T must be a signed integer type. Return 1 if the result overflows. */
|
|
|
9bb5d6 |
+#define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
|
|
|
9bb5d6 |
+ (sizeof ((a) op (b)) < sizeof (t) \
|
|
|
9bb5d6 |
+ ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
|
|
|
9bb5d6 |
+ : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
|
|
|
9bb5d6 |
+#define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
|
|
|
9bb5d6 |
+ ((overflow (a, b) \
|
|
|
9bb5d6 |
+ || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
|
|
|
9bb5d6 |
+ || (tmax) < ((a) op (b))) \
|
|
|
9bb5d6 |
+ ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
|
|
|
9bb5d6 |
+ : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+/* Return the low-order bits of A <op> B, where the operation is given
|
|
|
9bb5d6 |
+ by OP. Use the unsigned type UT for calculation to avoid undefined
|
|
|
9bb5d6 |
+ behavior on signed integer overflow, and convert the result to type T.
|
|
|
9bb5d6 |
+ UT is at least as wide as T and is no narrower than unsigned int,
|
|
|
9bb5d6 |
+ T is two's complement, and there is no padding or trap representations.
|
|
|
9bb5d6 |
+ Assume that converting UT to T yields the low-order bits, as is
|
|
|
9bb5d6 |
+ done in all known two's-complement C compilers. E.g., see:
|
|
|
9bb5d6 |
+ https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+ According to the C standard, converting UT to T yields an
|
|
|
9bb5d6 |
+ implementation-defined result or signal for values outside T's
|
|
|
9bb5d6 |
+ range. However, code that works around this theoretical problem
|
|
|
9bb5d6 |
+ runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
|
|
|
9bb5d6 |
+ https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
|
|
|
9bb5d6 |
+ As the compiler bug is real, don't try to work around the
|
|
|
9bb5d6 |
+ theoretical problem. */
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
|
|
|
9bb5d6 |
+ ((t) ((ut) (a) op (ut) (b)))
|
|
|
9bb5d6 |
+
|
|
|
9bb5d6 |
+#endif /* _GL_INTPROPS_H */
|