Improve compliance with C standards regarding signed integer shifts.
authorMark H Weaver <mhw@netris.org>
Fri, 7 Mar 2014 09:21:46 +0000 (04:21 -0500)
committerMark H Weaver <mhw@netris.org>
Fri, 7 Mar 2014 09:34:27 +0000 (04:34 -0500)
* configure.ac: Add -fwrapv when using GCC (or compatible), if
  supported.

* libguile/numbers.h (SCM_I_MAKINUM): Cast to scm_t_bits (unsigned)
  before shifting, to avoid undefined behavior.

configure.ac
libguile/numbers.h

index e99b272..a5cb4b8 100644 (file)
@@ -1516,7 +1516,8 @@ AC_SUBST(HOST_CC)
 
 GUILE_CHECK_GUILE_FOR_BUILD
                        
-## If we're using GCC, ask for aggressive warnings.
+## If we're using GCC, add flags to reduce strictness of undefined
+## behavior, and ask for aggressive warnings.
 GCC_CFLAGS=""
 case "$GCC" in
   yes )
@@ -1526,13 +1527,13 @@ case "$GCC" in
     ## -Wundef was removed because Gnulib prevented it (see
     ## <http://thread.gmane.org/gmane.lisp.guile.bugs/5329>.)
 
-    ## Build with `-fno-strict-aliasing' to prevent miscompilation on
-    ## some platforms.  See
+    ## Build with `-fno-strict-aliasing' and `-fwrapv' to prevent
+    ## miscompilation on some platforms.  See
     ## <http://lists.gnu.org/archive/html/guile-devel/2012-01/msg00487.html>.
 
     POTENTIAL_GCC_CFLAGS="-Wall -Wmissing-prototypes \
       -Wdeclaration-after-statement -Wpointer-arith \
-      -Wswitch-enum -fno-strict-aliasing"
+      -Wswitch-enum -fno-strict-aliasing -fwrapv"
     # Do this here so we don't screw up any of the tests above that might
     # not be "warning free"
     if test "${GUILE_ERROR_ON_WARNING}" = yes
index 4d977dc..b4202f2 100644 (file)
@@ -4,7 +4,7 @@
 #define SCM_NUMBERS_H
 
 /* Copyright (C) 1995,1996,1998,2000,2001,2002,2003,2004,2005, 2006,
- *   2008, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
+ *   2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -60,7 +60,7 @@ typedef scm_t_int32 scm_t_wchar;
 #define SCM_I_INUMP(x) (2 & SCM_UNPACK (x))
 #define SCM_I_NINUMP(x) (!SCM_I_INUMP (x))
 #define SCM_I_MAKINUM(x) \
-  (SCM_PACK ((((scm_t_signed_bits) (x)) << 2) + scm_tc2_int))
+  (SCM_PACK ((((scm_t_bits) (x)) << 2) + scm_tc2_int))
 #define SCM_I_INUM(x)   (SCM_SRS ((scm_t_signed_bits) SCM_UNPACK (x), 2))
 
 /* SCM_FIXABLE is true if its long argument can be encoded in an SCM_INUM. */