[Carpet] hdf5toascii_slicer: integer constant overflows (prevents compiling)

Jonathan Thornburg jthorn at aei.mpg.de
Sun Aug 20 22:24:07 CEST 2006


The current-darcs version of
  Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc
gives fatal errors when compiling with gcc 3.3.6 the compiler flags
  -pipe -ffast-math -Wall -W -Wformat -Wno-unused -Wshadow -Winline \
  -Wpointer-arith -Wcast-align -Wcast-qual -Woverloaded-virtual -Wreorder \
  -ffor-scope -Wreorder
The interesting part of the compile transcript is this:
/usr/local/bin/g++ /home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc -DCCODE  -g -pipe -ffast-math -Wall -W -Wformat -Wno-unused -Wshadow -Winline -Wpointer-arith -Wcast-align -Wcast-qual -Wmissing-declarations -Wbad-function-cast -Wstrict-prototypes -Wmissing-prototypes -I/home/jonathan/cactus/Cactus/configs/test-moving-excision/config-data -I/home/jonathan/cactus/Cactus/configs/test-moving-excision/bindings/include -I/home/jonathan/cactus/Cactus/src/include -I/home/jonathan/cactus/Cactus/arrangements  -c -o /home/jonathan/cactus/Cactus/configs/test-moving-excision/build/CarpetIOHDF5/hdf5toascii_slicer.o
cc1plus: warning: "-Wmissing-declarations" is valid for C/ObjC but not for C++
cc1plus: warning: "-Wbad-function-cast" is valid for C/ObjC but not for C++
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:69: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:69: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:69: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc: In
   function `int main(int, char* const*)':
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:188: warning: declaration
   of `i' shadows a previous local
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:109: warning: shadowed
   declaration is here
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:192: warning: declaration
   of `i' shadows a previous local
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:109: warning: shadowed
   declaration is here
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:192: warning: comparison
   between signed and unsigned integer expressions
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:201: warning: declaration
   of `i' shadows a previous local
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:109: warning: shadowed
   declaration is here
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:201: warning: comparison
   between signed and unsigned integer expressions
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc: In
   function `herr_t FindPatches(int, const char*, void*)':
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:286: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc: In
   function `void ReadPatch(const patch_t&, int)':
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:381: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:419: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:421: error: integer
   constant is too large for "long" type
/home/jonathan/cactus/Cactus/arrangements/Carpet/CarpetIOHDF5/src/util/hdf5toascii_slicer.cc:423: error: integer
   constant is too large for "long" type
gmake[1]: *** [/home/jonathan/cactus/Cactus/configs/test-moving-excision/build/CarpetIOHDF5/hdf5toascii_slicer.o] Error 1
gmake[1]: Leaving directory `/home/jonathan/cactus/Cactus'
gmake: *** [test-moving-excision-utils] Error 2

The offending code is this:
  #define SLICECOORD_UNSET        -424242424242
  static double slice_coord[3] = {SLICECOORD_UNSET, SLICECOORD_UNSET, SLICECOORD_UNSET};
Since there's no explicit type suffix on that literal constant, C/C++
first tries to implicitly type it as 'int', but (on a 32-bit system)
it's too big for that, so it gets an implicit type of 'long'.  But it's
still too big for that (it's about 2**38.6 in magnitude, and so would
need 40-bit ints to represent in 2's complement).  So it overflows. :(

The fix is easy: just change the literal to floating-point (since that's
how it's used anyway):
  #define SLICECOORD_UNSET        -424242424242.0 // this is about 2**39 in
                                                  // magnitude, small enough to
                                                  // be exactly represented in
                                                  // IEEE 'double' floating point
                                                  // (this is important because
                                                  // later code tests if values
                                                  // are == or != this #define)
  static double slice_coord[3] = {SLICECOORD_UNSET, SLICECOORD_UNSET, SLICECOORD_UNSET};

A patch is attached.  Thomas, could you push this into darcs?

ciao,

-- 
-- Jonathan Thornburg <jthorn at aei.mpg.de>      
   Max-Planck-Institut fuer Gravitationsphysik (Albert-Einstein-Institut),
   Golm, Germany, "Old Europe"     http://www.aei.mpg.de/~jthorn/home.html      
   "Washing one's hands of the conflict between the powerful and the
    powerless means to side with the powerful, not to be neutral."
                                      -- quote by Freire / poster by Oxfam
-------------- next part --------------
--- hdf5toascii_slicer.cc.orig	Fri Aug 18 18:22:28 2006
+++ hdf5toascii_slicer.cc	Sun Aug 20 22:18:16 2006
@@ -65,7 +65,13 @@
  *****************************************************************************/
 
 // the slice coordinate as selected by the user
-#define SLICECOORD_UNSET	-424242424242
+#define SLICECOORD_UNSET	-424242424242.0	// this is about 2**39 in
+						// magnitude, small enough to
+						// be exactly represented in
+						// IEEE 'double' floating point
+						// (this is important because
+						// later code tests if values
+						// are == or != this #define)
 static double slice_coord[3] = {SLICECOORD_UNSET, SLICECOORD_UNSET, SLICECOORD_UNSET};
 
 // the list of all patches


More information about the developers mailing list