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