You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

874 lines
40 KiB

  1. diff --git a/liboath/gl/fflush.c b/liboath/gl/fflush.c
  2. index 3664842..a140b7a 100644
  3. --- a/liboath/gl/fflush.c
  4. +++ b/liboath/gl/fflush.c
  5. @@ -1,18 +1,18 @@
  6. /* fflush.c -- allow flushing input streams
  7. - Copyright (C) 2007-2016 Free Software Foundation, Inc.
  8. + Copyright (C) 2007-2018 Free Software Foundation, Inc.
  9. This program is free software: you can redistribute it and/or modify
  10. - it under the terms of the GNU Lesser General Public License as published by
  11. - the Free Software Foundation; either version 2.1 of the License, or
  12. + it under the terms of the GNU General Public License as published by
  13. + the Free Software Foundation; either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. - GNU Lesser General Public License for more details.
  19. + GNU General Public License for more details.
  20. - You should have received a copy of the GNU Lesser General Public License
  21. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  22. + You should have received a copy of the GNU General Public License
  23. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  24. /* Written by Eric Blake. */
  25. @@ -33,7 +33,8 @@
  26. #undef fflush
  27. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  28. +#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  29. +/* GNU libc, BeOS, Haiku, Linux libc5 */
  30. /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
  31. static void
  32. @@ -51,7 +52,7 @@ static void
  33. clear_ungetc_buffer (FILE *fp)
  34. {
  35. # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  36. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  37. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  38. if (HASUB (fp))
  39. {
  40. fp_->_p += fp_->_r;
  41. @@ -63,7 +64,7 @@ clear_ungetc_buffer (FILE *fp)
  42. fp->_ungetc_count = 0;
  43. fp->_rcount = - fp->_rcount;
  44. }
  45. -# elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  46. +# elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
  47. /* Nothing to do. */
  48. # else /* other implementations */
  49. fseeko (fp, 0, SEEK_CUR);
  50. @@ -72,10 +73,11 @@ clear_ungetc_buffer (FILE *fp)
  51. #endif
  52. -#if ! (defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */)
  53. +#if ! (defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1)
  54. +/* GNU libc, BeOS, Haiku, Linux libc5 */
  55. # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
  56. -/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  57. +/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  58. static int
  59. disable_seek_optimization (FILE *fp)
  60. @@ -98,7 +100,7 @@ update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER,
  61. off_t pos _GL_UNUSED_PARAMETER)
  62. {
  63. # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  64. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  65. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  66. # if defined __CYGWIN__
  67. /* fp_->_offset is typed as an integer. */
  68. fp_->_offset = pos;
  69. @@ -148,7 +150,8 @@ rpl_fflush (FILE *stream)
  70. if (stream == NULL || ! freading (stream))
  71. return fflush (stream);
  72. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  73. +#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  74. + /* GNU libc, BeOS, Haiku, Linux libc5 */
  75. clear_ungetc_buffer_preserving_position (stream);
  76. @@ -199,7 +202,7 @@ rpl_fflush (FILE *stream)
  77. }
  78. # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
  79. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  80. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  81. {
  82. /* Disable seek optimization for the next fseeko call. This tells the
  83. diff --git a/liboath/gl/fpurge.c b/liboath/gl/fpurge.c
  84. index acf5905..f9c2d25 100644
  85. --- a/liboath/gl/fpurge.c
  86. +++ b/liboath/gl/fpurge.c
  87. @@ -62,7 +62,7 @@ fpurge (FILE *fp)
  88. /* Most systems provide FILE as a struct and the necessary bitmask in
  89. <stdio.h>, because they need it for implementing getc() and putc() as
  90. fast macros. */
  91. -# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  92. +# if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  93. fp->_IO_read_end = fp->_IO_read_ptr;
  94. fp->_IO_write_ptr = fp->_IO_write_base;
  95. /* Avoid memory leak when there is an active ungetc buffer. */
  96. diff --git a/liboath/gl/freading.c b/liboath/gl/freading.c
  97. index 8ab19fd..54c3d5a 100644
  98. --- a/liboath/gl/freading.c
  99. +++ b/liboath/gl/freading.c
  100. @@ -31,7 +31,7 @@ freading (FILE *fp)
  101. /* Most systems provide FILE as a struct and the necessary bitmask in
  102. <stdio.h>, because they need it for implementing getc() and putc() as
  103. fast macros. */
  104. -# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  105. +# if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  106. return ((fp->_flags & _IO_NO_WRITES) != 0
  107. || ((fp->_flags & (_IO_NO_READS | _IO_CURRENTLY_PUTTING)) == 0
  108. && fp->_IO_read_base != NULL));
  109. diff --git a/liboath/gl/fseeko.c b/liboath/gl/fseeko.c
  110. index 67bb9ec..5616221 100644
  111. --- a/liboath/gl/fseeko.c
  112. +++ b/liboath/gl/fseeko.c
  113. @@ -47,7 +47,7 @@ fseeko (FILE *fp, off_t offset, int whence)
  114. #endif
  115. /* These tests are based on fpurge.c. */
  116. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  117. +#if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  118. if (fp->_IO_read_end == fp->_IO_read_ptr
  119. && fp->_IO_write_ptr == fp->_IO_write_base
  120. && fp->_IO_save_base == NULL)
  121. @@ -123,7 +123,7 @@ fseeko (FILE *fp, off_t offset, int whence)
  122. return -1;
  123. }
  124. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  125. +#if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  126. fp->_flags &= ~_IO_EOF_SEEN;
  127. fp->_offset = pos;
  128. #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
  129. diff --git a/liboath/gl/stdio-impl.h b/liboath/gl/stdio-impl.h
  130. index 4c02c9f..393ef0c 100644
  131. --- a/liboath/gl/stdio-impl.h
  132. +++ b/liboath/gl/stdio-impl.h
  133. @@ -1,23 +1,29 @@
  134. /* Implementation details of FILE streams.
  135. - Copyright (C) 2007-2008, 2010-2016 Free Software Foundation, Inc.
  136. + Copyright (C) 2007-2008, 2010-2018 Free Software Foundation, Inc.
  137. This program is free software: you can redistribute it and/or modify
  138. - it under the terms of the GNU Lesser General Public License as published by
  139. - the Free Software Foundation; either version 2.1 of the License, or
  140. + it under the terms of the GNU General Public License as published by
  141. + the Free Software Foundation; either version 3 of the License, or
  142. (at your option) any later version.
  143. This program is distributed in the hope that it will be useful,
  144. but WITHOUT ANY WARRANTY; without even the implied warranty of
  145. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  146. - GNU Lesser General Public License for more details.
  147. + GNU General Public License for more details.
  148. - You should have received a copy of the GNU Lesser General Public License
  149. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  150. + You should have received a copy of the GNU General Public License
  151. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  152. /* Many stdio implementations have the same logic and therefore can share
  153. the same implementation of stdio extension API, except that some fields
  154. have different naming conventions, or their access requires some casts. */
  155. +/* Glibc 2.28 made _IO_IN_BACKUP private. For now, work around this
  156. + problem by defining it ourselves. FIXME: Do not rely on glibc
  157. + internals. */
  158. +#if !defined _IO_IN_BACKUP && defined _IO_EOF_SEEN
  159. +# define _IO_IN_BACKUP 0x100
  160. +#endif
  161. /* BSD stdio derived implementations. */
  162. @@ -29,10 +35,10 @@
  163. #include <errno.h> /* For detecting Plan9. */
  164. #if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  165. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  166. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  167. # if defined __DragonFly__ /* DragonFly */
  168. - /* See <http://www.dragonflybsd.org/cvsweb/src/lib/libc/stdio/priv_stdio.h?rev=HEAD&content-type=text/x-cvsweb-markup>. */
  169. + /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/lib/libc/stdio/priv_stdio.h>. */
  170. # define fp_ ((struct { struct __FILE_public pub; \
  171. struct { unsigned char *_base; int _size; } _bf; \
  172. void *cookie; \
  173. @@ -49,30 +55,84 @@
  174. fpos_t _offset; \
  175. /* More fields, not relevant here. */ \
  176. } *) fp)
  177. - /* See <http://www.dragonflybsd.org/cvsweb/src/include/stdio.h?rev=HEAD&content-type=text/x-cvsweb-markup>. */
  178. + /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/include/stdio.h>. */
  179. # define _p pub._p
  180. # define _flags pub._flags
  181. # define _r pub._r
  182. # define _w pub._w
  183. +# elif defined __ANDROID__ /* Android */
  184. + /* Up to this commit from 2015-10-12
  185. + <https://android.googlesource.com/platform/bionic.git/+/f0141dfab10a4b332769d52fa76631a64741297a>
  186. + the innards of FILE were public, and fp_ub could be defined like for OpenBSD,
  187. + see <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/fileext.h>
  188. + and <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/local.h>.
  189. + After this commit, the innards of FILE are hidden. */
  190. +# define fp_ ((struct { unsigned char *_p; \
  191. + int _r; \
  192. + int _w; \
  193. + int _flags; \
  194. + int _file; \
  195. + struct { unsigned char *_base; size_t _size; } _bf; \
  196. + int _lbfsize; \
  197. + void *_cookie; \
  198. + void *_close; \
  199. + void *_read; \
  200. + void *_seek; \
  201. + void *_write; \
  202. + struct { unsigned char *_base; size_t _size; } _ext; \
  203. + unsigned char *_up; \
  204. + int _ur; \
  205. + unsigned char _ubuf[3]; \
  206. + unsigned char _nbuf[1]; \
  207. + struct { unsigned char *_base; size_t _size; } _lb; \
  208. + int _blksize; \
  209. + fpos_t _offset; \
  210. + /* More fields, not relevant here. */ \
  211. + } *) fp)
  212. # else
  213. # define fp_ fp
  214. # endif
  215. -# if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __ANDROID__ /* NetBSD >= 1.5ZA, OpenBSD, Android */
  216. +# if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __minix /* NetBSD >= 1.5ZA, OpenBSD, Minix 3 */
  217. /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
  218. - and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
  219. + and <https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
  220. + and <https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/lib/libc/stdio/fileext.h> */
  221. struct __sfileext
  222. {
  223. struct __sbuf _ub; /* ungetc buffer */
  224. /* More fields, not relevant here. */
  225. };
  226. # define fp_ub ((struct __sfileext *) fp->_ext._base)->_ub
  227. -# else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin, Android */
  228. +# elif defined __ANDROID__ /* Android */
  229. + struct __sfileext
  230. + {
  231. + struct { unsigned char *_base; size_t _size; } _ub; /* ungetc buffer */
  232. + /* More fields, not relevant here. */
  233. + };
  234. +# define fp_ub ((struct __sfileext *) fp_->_ext._base)->_ub
  235. +# else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin */
  236. # define fp_ub fp_->_ub
  237. # endif
  238. # define HASUB(fp) (fp_ub._base != NULL)
  239. +# if defined __ANDROID__ /* Android */
  240. + /* Needed after this commit from 2016-01-25
  241. + <https://android.googlesource.com/platform/bionic.git/+/e70e0e9267d069bf56a5078c99307e08a7280de7> */
  242. +# ifndef __SEOF
  243. +# define __SLBF 1
  244. +# define __SNBF 2
  245. +# define __SRD 4
  246. +# define __SWR 8
  247. +# define __SRW 0x10
  248. +# define __SEOF 0x20
  249. +# define __SERR 0x40
  250. +# endif
  251. +# ifndef __SOFF
  252. +# define __SOFF 0x1000
  253. +# endif
  254. +# endif
  255. +
  256. #endif
  257. @@ -81,7 +141,7 @@
  258. #ifdef __TANDEM /* NonStop Kernel */
  259. # ifndef _IOERR
  260. /* These values were determined by the program 'stdioext-flags' at
  261. - <http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00165.html>. */
  262. + <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
  263. # define _IOERR 0x40
  264. # define _IOREAD 0x80
  265. # define _IOWRT 0x4
  266. @@ -99,6 +159,8 @@
  267. int _file; \
  268. unsigned int _flag; \
  269. } *) fp)
  270. +# elif defined __VMS /* OpenVMS */
  271. +# define fp_ ((struct _iobuf *) fp)
  272. # else
  273. # define fp_ fp
  274. # endif
  275. @@ -110,4 +172,31 @@
  276. # define _flag __flag
  277. # endif
  278. +#elif defined _WIN32 && ! defined __CYGWIN__ /* newer Windows with MSVC */
  279. +
  280. +/* <stdio.h> does not define the innards of FILE any more. */
  281. +# define WINDOWS_OPAQUE_FILE
  282. +
  283. +struct _gl_real_FILE
  284. +{
  285. + /* Note: Compared to older Windows and to mingw, it has the fields
  286. + _base and _cnt swapped. */
  287. + unsigned char *_ptr;
  288. + unsigned char *_base;
  289. + int _cnt;
  290. + int _flag;
  291. + int _file;
  292. + int _charbuf;
  293. + int _bufsiz;
  294. +};
  295. +# define fp_ ((struct _gl_real_FILE *) fp)
  296. +
  297. +/* These values were determined by a program similar to the one at
  298. + <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
  299. +# define _IOREAD 0x1
  300. +# define _IOWRT 0x2
  301. +# define _IORW 0x4
  302. +# define _IOEOF 0x8
  303. +# define _IOERR 0x10
  304. +
  305. #endif
  306. diff --git a/libpskc/gl/intprops.h b/libpskc/gl/intprops.h
  307. index feb02c3..af456ff 100644
  308. --- a/libpskc/gl/intprops.h
  309. +++ b/libpskc/gl/intprops.h
  310. @@ -1,6 +1,6 @@
  311. /* intprops.h -- properties of integer types
  312. - Copyright (C) 2001-2016 Free Software Foundation, Inc.
  313. + Copyright (C) 2001-2018 Free Software Foundation, Inc.
  314. This program is free software: you can redistribute it and/or modify it
  315. under the terms of the GNU Lesser General Public License as published
  316. @@ -13,7 +13,7 @@
  317. GNU Lesser General Public License for more details.
  318. You should have received a copy of the GNU Lesser General Public License
  319. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  320. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  321. /* Written by Paul Eggert. */
  322. @@ -21,13 +21,12 @@
  323. #define _GL_INTPROPS_H
  324. #include <limits.h>
  325. -#include <verify.h>
  326. /* Return a value with the common real type of E and V and the value of V. */
  327. #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
  328. /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
  329. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
  330. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
  331. #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
  332. /* The extra casts in the following macros work around compiler bugs,
  333. @@ -47,12 +46,16 @@
  334. /* Minimum and maximum values for integer types and expressions. */
  335. +/* The width in bits of the integer type or expression T.
  336. + Padding bits are not supported; this is checked at compile-time below. */
  337. +#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  338. +
  339. /* The maximum and minimum values for the integer type T. */
  340. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  341. #define TYPE_MAXIMUM(t) \
  342. ((t) (! TYPE_SIGNED (t) \
  343. ? (t) -1 \
  344. - : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  345. + : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  346. /* The maximum and minimum values for the type of the expression E,
  347. after integer promotion. E should not have side effects. */
  348. @@ -65,29 +68,23 @@
  349. ? _GL_SIGNED_INT_MAXIMUM (e) \
  350. : _GL_INT_NEGATE_CONVERT (e, 1))
  351. #define _GL_SIGNED_INT_MAXIMUM(e) \
  352. - (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
  353. + (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
  354. +
  355. +/* Work around OpenVMS incompatibility with C99. */
  356. +#if !defined LLONG_MAX && defined __INT64_MAX
  357. +# define LLONG_MAX __INT64_MAX
  358. +# define LLONG_MIN __INT64_MIN
  359. +#endif
  360. /* This include file assumes that signed types are two's complement without
  361. padding bits; the above macros have undefined behavior otherwise.
  362. If this is a problem for you, please let us know how to fix it for your host.
  363. - As a sanity check, test the assumption for some signed types that
  364. - <limits.h> bounds. */
  365. -verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
  366. -verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
  367. -verify (TYPE_MINIMUM (short int) == SHRT_MIN);
  368. -verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
  369. -verify (TYPE_MINIMUM (int) == INT_MIN);
  370. -verify (TYPE_MAXIMUM (int) == INT_MAX);
  371. -verify (TYPE_MINIMUM (long int) == LONG_MIN);
  372. -verify (TYPE_MAXIMUM (long int) == LONG_MAX);
  373. -#ifdef LLONG_MAX
  374. -verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
  375. -verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  376. -#endif
  377. + This assumption is tested by the intprops-tests module. */
  378. /* Does the __typeof__ keyword work? This could be done by
  379. 'configure', but for now it's easier to do it by hand. */
  380. -#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
  381. +#if (2 <= __GNUC__ \
  382. + || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  383. || (0x5110 <= __SUNPRO_C && !__STDC__))
  384. # define _GL_HAVE___TYPEOF__ 1
  385. #else
  386. @@ -116,8 +113,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  387. signed, this macro may overestimate the true bound by one byte when
  388. applied to unsigned types of size 2, 4, 16, ... bytes. */
  389. #define INT_STRLEN_BOUND(t) \
  390. - (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
  391. - - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  392. + (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  393. + _GL_SIGNED_TYPE_OR_EXPR (t))
  394. /* Bound on buffer size needed to represent an integer type or expression T,
  395. @@ -183,7 +179,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  396. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  397. See above for restrictions. Avoid && and || as they tickle
  398. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  399. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
  400. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  401. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  402. ((b) < 0 \
  403. ? ((a) < 0 \
  404. @@ -222,20 +218,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  405. ? (a) < (min) >> (b) \
  406. : (max) >> (b) < (a))
  407. -/* True if __builtin_add_overflow (A, B, P) works when P is null. */
  408. -#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__)
  409. +/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
  410. +#if 5 <= __GNUC__ && !defined __ICC
  411. +# define _GL_HAS_BUILTIN_OVERFLOW 1
  412. +#else
  413. +# define _GL_HAS_BUILTIN_OVERFLOW 0
  414. +#endif
  415. +
  416. +/* True if __builtin_add_overflow_p (A, B, C) works. */
  417. +#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  418. /* The _GL*_OVERFLOW macros have the same restrictions as the
  419. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  420. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  421. that the result (e.g., A + B) has that type. */
  422. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  423. -# define _GL_ADD_OVERFLOW(a, b, min, max)
  424. - __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0)
  425. -# define _GL_SUBTRACT_OVERFLOW(a, b, min, max)
  426. - __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0)
  427. -# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)
  428. - __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0)
  429. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  430. +# define _GL_ADD_OVERFLOW(a, b, min, max) \
  431. + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  432. +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  433. + __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  434. +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  435. + __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  436. #else
  437. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  438. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  439. @@ -315,7 +318,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  440. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  441. #define INT_SUBTRACT_OVERFLOW(a, b) \
  442. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  443. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  444. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  445. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  446. #else
  447. # define INT_NEGATE_OVERFLOW(a) \
  448. @@ -349,10 +352,6 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  449. #define INT_MULTIPLY_WRAPV(a, b, r) \
  450. _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
  451. -#ifndef __has_builtin
  452. -# define __has_builtin(x) 0
  453. -#endif
  454. -
  455. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  456. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  457. https://llvm.org/bugs/show_bug.cgi?id=25390
  458. @@ -369,17 +368,17 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  459. the operation. BUILTIN is the builtin operation, and OVERFLOW the
  460. overflow predicate. Return 1 if the result overflows. See above
  461. for restrictions. */
  462. -#if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
  463. +#if _GL_HAS_BUILTIN_OVERFLOW
  464. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
  465. #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  466. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  467. (_Generic \
  468. (*(r), \
  469. signed char: \
  470. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  471. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  472. signed char, SCHAR_MIN, SCHAR_MAX), \
  473. short int: \
  474. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  475. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  476. short int, SHRT_MIN, SHRT_MAX), \
  477. int: \
  478. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  479. @@ -393,10 +392,10 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  480. #else
  481. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  482. (sizeof *(r) == sizeof (signed char) \
  483. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  484. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  485. signed char, SCHAR_MIN, SCHAR_MAX) \
  486. : sizeof *(r) == sizeof (short int) \
  487. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  488. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  489. short int, SHRT_MIN, SHRT_MAX) \
  490. : sizeof *(r) == sizeof (int) \
  491. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  492. @@ -412,15 +411,14 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  493. # else
  494. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  495. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  496. - long int, LONG_MIN, LONG_MAX))
  497. + long int, LONG_MIN, LONG_MAX)
  498. # endif
  499. #endif
  500. /* Store the low-order bits of A <op> B into *R, where the operation
  501. is given by OP. Use the unsigned type UT for calculation to avoid
  502. - overflow problems. *R's type is T, with extremal values TMIN and
  503. - TMAX. T must be a signed integer type. Return 1 if the result
  504. - overflows. */
  505. + overflow problems. *R's type is T, with extrema TMIN and TMAX.
  506. + T must be a signed integer type. Return 1 if the result overflows. */
  507. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  508. (sizeof ((a) op (b)) < sizeof (t) \
  509. ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
  510. @@ -429,17 +427,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  511. ((overflow (a, b) \
  512. || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
  513. || (tmax) < ((a) op (b))) \
  514. - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
  515. - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
  516. -
  517. -/* Return A <op> B, where the operation is given by OP. Use the
  518. - unsigned type UT for calculation to avoid overflow problems.
  519. - Convert the result to type T without overflow by subtracting TMIN
  520. - from large values before converting, and adding it afterwards.
  521. - Compilers can optimize all the operations except OP. */
  522. -#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
  523. - (((ut) (a) op (ut) (b)) <= (tmax) \
  524. - ? (t) ((ut) (a) op (ut) (b)) \
  525. - : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
  526. + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  527. + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  528. +
  529. +/* Return the low-order bits of A <op> B, where the operation is given
  530. + by OP. Use the unsigned type UT for calculation to avoid undefined
  531. + behavior on signed integer overflow, and convert the result to type T.
  532. + UT is at least as wide as T and is no narrower than unsigned int,
  533. + T is two's complement, and there is no padding or trap representations.
  534. + Assume that converting UT to T yields the low-order bits, as is
  535. + done in all known two's-complement C compilers. E.g., see:
  536. + https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  537. +
  538. + According to the C standard, converting UT to T yields an
  539. + implementation-defined result or signal for values outside T's
  540. + range. However, code that works around this theoretical problem
  541. + runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  542. + https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  543. + As the compiler bug is real, don't try to work around the
  544. + theoretical problem. */
  545. +
  546. +#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  547. + ((t) ((ut) (a) op (ut) (b)))
  548. #endif /* _GL_INTPROPS_H */
  549. diff --git a/oathtool/gl/intprops.h b/oathtool/gl/intprops.h
  550. index e1fce5c..af456ff 100644
  551. --- a/oathtool/gl/intprops.h
  552. +++ b/oathtool/gl/intprops.h
  553. @@ -1,19 +1,19 @@
  554. /* intprops.h -- properties of integer types
  555. - Copyright (C) 2001-2016 Free Software Foundation, Inc.
  556. + Copyright (C) 2001-2018 Free Software Foundation, Inc.
  557. This program is free software: you can redistribute it and/or modify it
  558. - under the terms of the GNU General Public License as published
  559. - by the Free Software Foundation; either version 3 of the License, or
  560. + under the terms of the GNU Lesser General Public License as published
  561. + by the Free Software Foundation; either version 2.1 of the License, or
  562. (at your option) any later version.
  563. This program is distributed in the hope that it will be useful,
  564. but WITHOUT ANY WARRANTY; without even the implied warranty of
  565. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  566. - GNU General Public License for more details.
  567. + GNU Lesser General Public License for more details.
  568. - You should have received a copy of the GNU General Public License
  569. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  570. + You should have received a copy of the GNU Lesser General Public License
  571. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  572. /* Written by Paul Eggert. */
  573. @@ -21,13 +21,12 @@
  574. #define _GL_INTPROPS_H
  575. #include <limits.h>
  576. -#include <verify.h>
  577. /* Return a value with the common real type of E and V and the value of V. */
  578. #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
  579. /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
  580. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
  581. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
  582. #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
  583. /* The extra casts in the following macros work around compiler bugs,
  584. @@ -47,12 +46,16 @@
  585. /* Minimum and maximum values for integer types and expressions. */
  586. +/* The width in bits of the integer type or expression T.
  587. + Padding bits are not supported; this is checked at compile-time below. */
  588. +#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  589. +
  590. /* The maximum and minimum values for the integer type T. */
  591. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  592. #define TYPE_MAXIMUM(t) \
  593. ((t) (! TYPE_SIGNED (t) \
  594. ? (t) -1 \
  595. - : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  596. + : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  597. /* The maximum and minimum values for the type of the expression E,
  598. after integer promotion. E should not have side effects. */
  599. @@ -65,29 +68,23 @@
  600. ? _GL_SIGNED_INT_MAXIMUM (e) \
  601. : _GL_INT_NEGATE_CONVERT (e, 1))
  602. #define _GL_SIGNED_INT_MAXIMUM(e) \
  603. - (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
  604. + (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
  605. +
  606. +/* Work around OpenVMS incompatibility with C99. */
  607. +#if !defined LLONG_MAX && defined __INT64_MAX
  608. +# define LLONG_MAX __INT64_MAX
  609. +# define LLONG_MIN __INT64_MIN
  610. +#endif
  611. /* This include file assumes that signed types are two's complement without
  612. padding bits; the above macros have undefined behavior otherwise.
  613. If this is a problem for you, please let us know how to fix it for your host.
  614. - As a sanity check, test the assumption for some signed types that
  615. - <limits.h> bounds. */
  616. -verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
  617. -verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
  618. -verify (TYPE_MINIMUM (short int) == SHRT_MIN);
  619. -verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
  620. -verify (TYPE_MINIMUM (int) == INT_MIN);
  621. -verify (TYPE_MAXIMUM (int) == INT_MAX);
  622. -verify (TYPE_MINIMUM (long int) == LONG_MIN);
  623. -verify (TYPE_MAXIMUM (long int) == LONG_MAX);
  624. -#ifdef LLONG_MAX
  625. -verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
  626. -verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  627. -#endif
  628. + This assumption is tested by the intprops-tests module. */
  629. /* Does the __typeof__ keyword work? This could be done by
  630. 'configure', but for now it's easier to do it by hand. */
  631. -#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
  632. +#if (2 <= __GNUC__ \
  633. + || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  634. || (0x5110 <= __SUNPRO_C && !__STDC__))
  635. # define _GL_HAVE___TYPEOF__ 1
  636. #else
  637. @@ -116,8 +113,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  638. signed, this macro may overestimate the true bound by one byte when
  639. applied to unsigned types of size 2, 4, 16, ... bytes. */
  640. #define INT_STRLEN_BOUND(t) \
  641. - (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
  642. - - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  643. + (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  644. + _GL_SIGNED_TYPE_OR_EXPR (t))
  645. /* Bound on buffer size needed to represent an integer type or expression T,
  646. @@ -183,7 +179,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  647. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  648. See above for restrictions. Avoid && and || as they tickle
  649. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  650. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
  651. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  652. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  653. ((b) < 0 \
  654. ? ((a) < 0 \
  655. @@ -222,20 +218,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  656. ? (a) < (min) >> (b) \
  657. : (max) >> (b) < (a))
  658. -/* True if __builtin_add_overflow (A, B, P) works when P is null. */
  659. -#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__)
  660. +/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
  661. +#if 5 <= __GNUC__ && !defined __ICC
  662. +# define _GL_HAS_BUILTIN_OVERFLOW 1
  663. +#else
  664. +# define _GL_HAS_BUILTIN_OVERFLOW 0
  665. +#endif
  666. +
  667. +/* True if __builtin_add_overflow_p (A, B, C) works. */
  668. +#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  669. /* The _GL*_OVERFLOW macros have the same restrictions as the
  670. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  671. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  672. that the result (e.g., A + B) has that type. */
  673. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  674. -# define _GL_ADD_OVERFLOW(a, b, min, max)
  675. - __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0)
  676. -# define _GL_SUBTRACT_OVERFLOW(a, b, min, max)
  677. - __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0)
  678. -# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)
  679. - __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0)
  680. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  681. +# define _GL_ADD_OVERFLOW(a, b, min, max) \
  682. + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  683. +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  684. + __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  685. +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  686. + __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  687. #else
  688. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  689. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  690. @@ -315,7 +318,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  691. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  692. #define INT_SUBTRACT_OVERFLOW(a, b) \
  693. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  694. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  695. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  696. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  697. #else
  698. # define INT_NEGATE_OVERFLOW(a) \
  699. @@ -349,10 +352,6 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  700. #define INT_MULTIPLY_WRAPV(a, b, r) \
  701. _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
  702. -#ifndef __has_builtin
  703. -# define __has_builtin(x) 0
  704. -#endif
  705. -
  706. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  707. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  708. https://llvm.org/bugs/show_bug.cgi?id=25390
  709. @@ -369,17 +368,17 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  710. the operation. BUILTIN is the builtin operation, and OVERFLOW the
  711. overflow predicate. Return 1 if the result overflows. See above
  712. for restrictions. */
  713. -#if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
  714. +#if _GL_HAS_BUILTIN_OVERFLOW
  715. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
  716. #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  717. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  718. (_Generic \
  719. (*(r), \
  720. signed char: \
  721. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  722. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  723. signed char, SCHAR_MIN, SCHAR_MAX), \
  724. short int: \
  725. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  726. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  727. short int, SHRT_MIN, SHRT_MAX), \
  728. int: \
  729. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  730. @@ -393,10 +392,10 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  731. #else
  732. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  733. (sizeof *(r) == sizeof (signed char) \
  734. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  735. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  736. signed char, SCHAR_MIN, SCHAR_MAX) \
  737. : sizeof *(r) == sizeof (short int) \
  738. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  739. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  740. short int, SHRT_MIN, SHRT_MAX) \
  741. : sizeof *(r) == sizeof (int) \
  742. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  743. @@ -412,15 +411,14 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  744. # else
  745. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  746. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  747. - long int, LONG_MIN, LONG_MAX))
  748. + long int, LONG_MIN, LONG_MAX)
  749. # endif
  750. #endif
  751. /* Store the low-order bits of A <op> B into *R, where the operation
  752. is given by OP. Use the unsigned type UT for calculation to avoid
  753. - overflow problems. *R's type is T, with extremal values TMIN and
  754. - TMAX. T must be a signed integer type. Return 1 if the result
  755. - overflows. */
  756. + overflow problems. *R's type is T, with extrema TMIN and TMAX.
  757. + T must be a signed integer type. Return 1 if the result overflows. */
  758. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  759. (sizeof ((a) op (b)) < sizeof (t) \
  760. ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
  761. @@ -429,17 +427,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  762. ((overflow (a, b) \
  763. || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
  764. || (tmax) < ((a) op (b))) \
  765. - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
  766. - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
  767. -
  768. -/* Return A <op> B, where the operation is given by OP. Use the
  769. - unsigned type UT for calculation to avoid overflow problems.
  770. - Convert the result to type T without overflow by subtracting TMIN
  771. - from large values before converting, and adding it afterwards.
  772. - Compilers can optimize all the operations except OP. */
  773. -#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
  774. - (((ut) (a) op (ut) (b)) <= (tmax) \
  775. - ? (t) ((ut) (a) op (ut) (b)) \
  776. - : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
  777. + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  778. + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  779. +
  780. +/* Return the low-order bits of A <op> B, where the operation is given
  781. + by OP. Use the unsigned type UT for calculation to avoid undefined
  782. + behavior on signed integer overflow, and convert the result to type T.
  783. + UT is at least as wide as T and is no narrower than unsigned int,
  784. + T is two's complement, and there is no padding or trap representations.
  785. + Assume that converting UT to T yields the low-order bits, as is
  786. + done in all known two's-complement C compilers. E.g., see:
  787. + https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  788. +
  789. + According to the C standard, converting UT to T yields an
  790. + implementation-defined result or signal for values outside T's
  791. + range. However, code that works around this theoretical problem
  792. + runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  793. + https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  794. + As the compiler bug is real, don't try to work around the
  795. + theoretical problem. */
  796. +
  797. +#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  798. + ((t) ((ut) (a) op (ut) (b)))
  799. #endif /* _GL_INTPROPS_H */