Skip to content

Commit eff9f43

Browse files
matthiasgoergensCAM-Gerlacherlend-aaslandAA-Turnerhauntsaninja
authoredMar 4, 2023
pythongh-96821: Add config option --with-strict-overflow (python#96823)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Shantanu <hauntsaninja@gmail.com>
1 parent e4609cb commit eff9f43

File tree

4 files changed

+122
-18
lines changed

4 files changed

+122
-18
lines changed
 

‎Doc/using/configure.rst

+5
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ also be used to improve performance.
326326

327327
Enable C-level code profiling with ``gprof`` (disabled by default).
328328

329+
.. cmdoption:: --with-strict-overflow
330+
331+
Add ``-fstrict-overflow`` to the C compiler flags (by default we add
332+
``-fno-strict-overflow`` instead).
333+
329334

330335
.. _debug-build:
331336

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Explicitly mark C extension modules that need defined signed integer overflow,
2+
and add a configure option :option:`--with-strict-overflow`.
3+
Patch by Matthias Görgens and Shantanu Jain.

‎configure

+69-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac

+45-9
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,45 @@ case $CC in
20732073
fi
20742074
esac
20752075

2076+
dnl Historically, some of our code assumed that signed integer overflow
2077+
dnl is defined behaviour via twos-complement.
2078+
dnl Set STRICT_OVERFLOW_CFLAGS and NO_STRICT_OVERFLOW_CFLAGS depending on compiler support.
2079+
dnl Pass the latter to modules that depend on such behaviour.
2080+
_SAVE_VAR([CFLAGS])
2081+
CFLAGS="-fstrict-overflow -fno-strict-overflow"
2082+
AC_CACHE_CHECK([if $CC supports -fstrict-overflow and -fno-strict-overflow],
2083+
[ac_cv_cc_supports_fstrict_overflow],
2084+
AC_COMPILE_IFELSE(
2085+
[AC_LANG_PROGRAM([[]], [[]])],
2086+
[ac_cv_cc_supports_fstrict_overflow=yes],
2087+
[ac_cv_cc_supports_fstrict_overflow=no]
2088+
)
2089+
)
2090+
_RESTORE_VAR([CFLAGS])
2091+
2092+
AS_VAR_IF([ac_cv_cc_supports_fstrict_overflow], [yes],
2093+
[STRICT_OVERFLOW_CFLAGS="-fstrict-overflow"
2094+
NO_STRICT_OVERFLOW_CFLAGS="-fno-strict-overflow"],
2095+
[STRICT_OVERFLOW_CFLAGS=""
2096+
NO_STRICT_OVERFLOW_CFLAGS=""])
2097+
2098+
AC_MSG_CHECKING([for --with-strict-overflow])
2099+
AC_ARG_WITH([strict-overflow],
2100+
AS_HELP_STRING(
2101+
[--with-strict-overflow],
2102+
[if 'yes', add -fstrict-overflow to CFLAGS, else add -fno-strict-overflow (default is no)]
2103+
),
2104+
[
2105+
AS_VAR_IF(
2106+
[ac_cv_cc_supports_fstrict_overflow], [no],
2107+
[AC_MSG_WARN([--with-strict-overflow=yes requires a compiler that supports -fstrict-overflow])],
2108+
[]
2109+
)
2110+
],
2111+
[with_strict_overflow=no]
2112+
)
2113+
AC_MSG_RESULT([$with_strict_overflow])
2114+
20762115
# Check if CC supports -Og optimization level
20772116
_SAVE_VAR([CFLAGS])
20782117
CFLAGS="-Og"
@@ -2103,15 +2142,8 @@ if test "${OPT-unset}" = "unset"
21032142
then
21042143
case $GCC in
21052144
yes)
2106-
# For gcc 4.x we need to use -fwrapv so lets check if its supported
2107-
if "$CC" -v --help 2>/dev/null |grep -- -fwrapv > /dev/null; then
2108-
WRAP="-fwrapv"
2109-
fi
2110-
21112145
if test -n "${cc_is_clang}"
21122146
then
2113-
# Clang also needs -fwrapv
2114-
WRAP="-fwrapv"
21152147
# bpo-30104: disable strict aliasing to compile correctly dtoa.c,
21162148
# see Makefile.pre.in for more information
21172149
CFLAGS_ALIASING="-fno-strict-aliasing"
@@ -2122,7 +2154,7 @@ then
21222154
if test "$Py_DEBUG" = 'true' ; then
21232155
OPT="-g $PYDEBUG_CFLAGS -Wall"
21242156
else
2125-
OPT="-g $WRAP -O3 -Wall"
2157+
OPT="-g -O3 -Wall"
21262158
fi
21272159
;;
21282160
*)
@@ -2237,6 +2269,10 @@ AC_DEFUN([PY_CHECK_CC_WARNING], [
22372269
])
22382270

22392271
# tweak BASECFLAGS based on compiler and platform
2272+
AS_VAR_IF([with_strict_overflow], [yes],
2273+
[BASECFLAGS="$BASECFLAGS $STRICT_OVERFLOW_CFLAGS"],
2274+
[BASECFLAGS="$BASECFLAGS $NO_STRICT_OVERFLOW_CFLAGS"])
2275+
22402276
case $GCC in
22412277
yes)
22422278
CFLAGS_NODIST="$CFLAGS_NODIST -std=c11"
@@ -7213,7 +7249,7 @@ PY_STDLIB_MOD([_crypt],
72137249
[$LIBCRYPT_CFLAGS], [$LIBCRYPT_LIBS])
72147250
PY_STDLIB_MOD([_ctypes],
72157251
[], [test "$have_libffi" = yes],
7216-
[$LIBFFI_CFLAGS], [$LIBFFI_LIBS])
7252+
[$NO_STRICT_OVERFLOW_CFLAGS $LIBFFI_CFLAGS], [$LIBFFI_LIBS])
72177253
PY_STDLIB_MOD([_curses],
72187254
[], [test "$have_curses" != "no"],
72197255
[$CURSES_CFLAGS], [$CURSES_LIBS]

0 commit comments

Comments
 (0)
Please sign in to comment.