zf8848 / protobuf

Automatically exported from code.google.com/p/protobuf
Other
0 stars 0 forks source link

Undefined behavior while running unit tests #663

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

When compiling protobuf 2.6.0-rc1 with gcc 4.9's undefined behavior detector, I 
get runtime errors from protoc:

./configure CXX="g++-4.9" CXXFLAGS="-O0 -ggdb -fsanitize=undefined 
-fsanitize=signed-integer-overflow" && make
[...]
oldpwd=`pwd` && ( cd . && $oldpwd/protoc -I. --cpp_out=$oldpwd 
google/protobuf/unittest.proto google/protobuf/unittest_empty.proto 
google/protobuf/unittest_import.proto 
google/protobuf/unittest_import_public.proto 
google/protobuf/unittest_mset.proto google/protobuf/unittest_optimize_for.proto 
google/protobuf/unittest_embed_optimize_for.proto 
google/protobuf/unittest_custom_options.proto 
google/protobuf/unittest_lite.proto google/protobuf/unittest_import_lite.proto 
google/protobuf/unittest_import_public_lite.proto 
google/protobuf/unittest_lite_imports_nonlite.proto 
google/protobuf/unittest_no_generic_services.proto 
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto )
google/protobuf/stubs/strutil.cc:908:17: runtime error: signed integer 
overflow: 42 * -100000000 cannot be represented in type 'int'
google/protobuf/compiler/parser.cc:1103:41: runtime error: negation of 
-9223372036854775808 cannot be represented in type 'long int'; cast to an 
unsigned type to negate this value to itself
[...]

Note that -9223372036854775808 is -0x8000000000000000.

Original issue reported on code.google.com by robert.e...@gmail.com on 21 Aug 2014 at 6:32

GoogleCodeExporter commented 9 years ago
Yes our implementation used this undefined behavior.

Line 1103 -static_cast<int64>(value);  tries to cast an uint64 to int64. If the 
uint64 is 0x8000000000000000, we will have a minimum int64. Negation of minimum 
negative value is an undefined behavior and compiler will "cast to an unsigned 
type to negate this value to itself"

It reports undefined behavior because you used the detector. We prefer not fix 
it unless there are some compilers which do not keep the value as it is when 
"negation of the minimum negative" 

Thanks,
Jie

Original comment by jie...@google.com on 21 Aug 2014 at 11:28

GoogleCodeExporter commented 9 years ago

Original comment by jie...@google.com on 26 Aug 2014 at 3:28