Commit 40da1ed5 authored by brian-peloton's avatar brian-peloton Committed by Feng Xiao

Removing undefined behavior and compiler warnings (#1315)

* Comment out unused arguments.

These last few are all that's needed to compile with -Wunused-arguments.

* Fix missing struct field initializer.

With this fix, everything compiles with -Wmissing-field-initializers.

* Add support for disabling unaligned memory accesses on x86 too.

ubsan doesn't like these because they are technically undefined
behavior, so -DGOOGLE_PROTOBUF_DONT_USE_UNALIGNED will disable them easily.

* Avoid undefined integer overflow.

ubsan catches all of these.
parent ba987a7e
...@@ -829,13 +829,13 @@ class LIBPROTOBUF_EXPORT Arena { ...@@ -829,13 +829,13 @@ class LIBPROTOBUF_EXPORT Arena {
} }
template <typename T> template <typename T>
static void CreateInArenaStorageInternal( static void CreateInArenaStorageInternal(
T* ptr, Arena* arena, google::protobuf::internal::false_type) { T* ptr, Arena* /* arena */, google::protobuf::internal::false_type) {
new (ptr) T(); new (ptr) T();
} }
template <typename T> template <typename T>
static void RegisterDestructorInternal( static void RegisterDestructorInternal(
T* ptr, Arena* arena, google::protobuf::internal::true_type) {} T* /* ptr */, Arena* /* arena */, google::protobuf::internal::true_type) {}
template <typename T> template <typename T>
static void RegisterDestructorInternal( static void RegisterDestructorInternal(
T* ptr, Arena* arena, google::protobuf::internal::false_type) { T* ptr, Arena* arena, google::protobuf::internal::false_type) {
...@@ -870,7 +870,7 @@ class LIBPROTOBUF_EXPORT Arena { ...@@ -870,7 +870,7 @@ class LIBPROTOBUF_EXPORT Arena {
} }
template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
static ::google::protobuf::Arena* GetArenaInternal(const T* value, ...) { static ::google::protobuf::Arena* GetArenaInternal(const T* /* value */, ...) {
return NULL; return NULL;
} }
......
...@@ -1373,7 +1373,7 @@ bool Parser::ParseOption(Message* options, ...@@ -1373,7 +1373,7 @@ bool Parser::ParseOption(Message* options,
value_location.AddPath( value_location.AddPath(
UninterpretedOption::kNegativeIntValueFieldNumber); UninterpretedOption::kNegativeIntValueFieldNumber);
uninterpreted_option->set_negative_int_value( uninterpreted_option->set_negative_int_value(
-static_cast<int64>(value)); static_cast<int64>(-value));
} else { } else {
value_location.AddPath( value_location.AddPath(
UninterpretedOption::kPositiveIntValueFieldNumber); UninterpretedOption::kPositiveIntValueFieldNumber);
......
...@@ -341,7 +341,7 @@ inline std::ostream& operator<<(std::ostream& out, ...@@ -341,7 +341,7 @@ inline std::ostream& operator<<(std::ostream& out,
MultiTokenCase kMultiTokenCases[] = { MultiTokenCase kMultiTokenCases[] = {
// Test empty input. // Test empty input.
{ "", { { "", {
{ Tokenizer::TYPE_END , "" , 0, 0 }, { Tokenizer::TYPE_END , "" , 0, 0, 0 },
}}, }},
// Test all token types at the same time. // Test all token types at the same time.
......
...@@ -753,7 +753,7 @@ class LIBPROTOBUF_EXPORT Reflection { ...@@ -753,7 +753,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// TODO(tmarek): Make virtual after all subclasses have been // TODO(tmarek): Make virtual after all subclasses have been
// updated. // updated.
virtual void AddAllocatedMessage(Message* /* message */, virtual void AddAllocatedMessage(Message* /* message */,
const FieldDescriptor* /*field */, const FieldDescriptor* /* field */,
Message* /* new_entry */) const {} Message* /* new_entry */) const {}
...@@ -961,7 +961,7 @@ class LIBPROTOBUF_EXPORT Reflection { ...@@ -961,7 +961,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// TODO(jieluo) - make the map APIs pure virtual after updating // TODO(jieluo) - make the map APIs pure virtual after updating
// all the subclasses. // all the subclasses.
// Returns true if key is in map. Returns false if key is not in map field. // Returns true if key is in map. Returns false if key is not in map field.
virtual bool ContainsMapKey(const Message& /* message*/, virtual bool ContainsMapKey(const Message& /* message */,
const FieldDescriptor* /* field */, const FieldDescriptor* /* field */,
const MapKey& /* key */) const { const MapKey& /* key */) const {
return false; return false;
...@@ -979,7 +979,7 @@ class LIBPROTOBUF_EXPORT Reflection { ...@@ -979,7 +979,7 @@ class LIBPROTOBUF_EXPORT Reflection {
// Delete and returns true if key is in the map field. Returns false // Delete and returns true if key is in the map field. Returns false
// otherwise. // otherwise.
virtual bool DeleteMapValue(Message* /* mesage */, virtual bool DeleteMapValue(Message* /* message */,
const FieldDescriptor* /* field */, const FieldDescriptor* /* field */,
const MapKey& /* key */) const { const MapKey& /* key */) const {
return false; return false;
......
...@@ -252,9 +252,15 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF); ...@@ -252,9 +252,15 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
#define GOOGLE_GUARDED_BY(x) #define GOOGLE_GUARDED_BY(x)
#define GOOGLE_ATTRIBUTE_COLD #define GOOGLE_ATTRIBUTE_COLD
#ifdef GOOGLE_PROTOBUF_DONT_USE_UNALIGNED
# define GOOGLE_PROTOBUF_USE_UNALIGNED 0
#else
// x86 and x86-64 can perform unaligned loads/stores directly. // x86 and x86-64 can perform unaligned loads/stores directly.
#if defined(_M_X64) || defined(__x86_64__) || \ # define GOOGLE_PROTOBUF_USE_UNALIGNED defined(_M_X64) || \
defined(_M_IX86) || defined(__i386__) defined(__x86_64__) || defined(_M_IX86) || defined(__i386__)
#endif
#if GOOGLE_PROTOBUF_USE_UNALIGNED
#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p)) #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p)) #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
......
...@@ -241,18 +241,18 @@ class LIBPROTOBUF_EXPORT MessageDifferencer { ...@@ -241,18 +241,18 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
// mutually exclusive. If a field has been both moved and modified, then // mutually exclusive. If a field has been both moved and modified, then
// only ReportModified will be called. // only ReportModified will be called.
virtual void ReportMoved( virtual void ReportMoved(
const Message& message1, const Message& /* message1 */,
const Message& message2, const Message& /* message2 */,
const std::vector<SpecificField>& field_path) { } const std::vector<SpecificField>& /* field_path */) { }
// Reports that two fields match. Useful for doing side-by-side diffs. // Reports that two fields match. Useful for doing side-by-side diffs.
// This function is mutually exclusive with ReportModified and ReportMoved. // This function is mutually exclusive with ReportModified and ReportMoved.
// Note that you must call set_report_matches(true) before calling Compare // Note that you must call set_report_matches(true) before calling Compare
// to make use of this function. // to make use of this function.
virtual void ReportMatched( virtual void ReportMatched(
const Message& message1, const Message& /* message1 */,
const Message& message2, const Message& /* message2 */,
const std::vector<SpecificField>& field_path) { } const std::vector<SpecificField>& /* field_path */) { }
// Reports that two fields would have been compared, but the // Reports that two fields would have been compared, but the
// comparison has been skipped because the field was marked as // comparison has been skipped because the field was marked as
...@@ -274,16 +274,16 @@ class LIBPROTOBUF_EXPORT MessageDifferencer { ...@@ -274,16 +274,16 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
// the fields are equal or not (perhaps with a second call to // the fields are equal or not (perhaps with a second call to
// Compare()), if it cares. // Compare()), if it cares.
virtual void ReportIgnored( virtual void ReportIgnored(
const Message& message1, const Message& /* message1 */,
const Message& message2, const Message& /* message2 */,
const std::vector<SpecificField>& field_path) { } const std::vector<SpecificField>& /* field_path */) { }
// Report that an unknown field is ignored. (see comment above). // Report that an unknown field is ignored. (see comment above).
// Note this is a different function since the last SpecificField in field // Note this is a different function since the last SpecificField in field
// path has a null field. This could break existing Reporter. // path has a null field. This could break existing Reporter.
virtual void ReportUnknownFieldIgnored( virtual void ReportUnknownFieldIgnored(
const Message& message1, const Message& message2, const Message& /* message1 */, const Message& /* message2 */,
const std::vector<SpecificField>& field_path) {} const std::vector<SpecificField>& /* field_path */) {}
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reporter); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reporter);
...@@ -297,9 +297,9 @@ class LIBPROTOBUF_EXPORT MessageDifferencer { ...@@ -297,9 +297,9 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
virtual ~MapKeyComparator(); virtual ~MapKeyComparator();
virtual bool IsMatch( virtual bool IsMatch(
const Message& message1, const Message& /* message1 */,
const Message& message2, const Message& /* message2 */,
const std::vector<SpecificField>& parent_fields) const { const std::vector<SpecificField>& /* parent_fields */) const {
GOOGLE_CHECK(false) << "IsMatch() is not implemented."; GOOGLE_CHECK(false) << "IsMatch() is not implemented.";
return false; return false;
} }
...@@ -321,18 +321,18 @@ class LIBPROTOBUF_EXPORT MessageDifferencer { ...@@ -321,18 +321,18 @@ class LIBPROTOBUF_EXPORT MessageDifferencer {
// Returns true if the field should be ignored. // Returns true if the field should be ignored.
virtual bool IsIgnored( virtual bool IsIgnored(
const Message& message1, const Message& /* message1 */,
const Message& message2, const Message& /* message2 */,
const FieldDescriptor* field, const FieldDescriptor* /* field */,
const std::vector<SpecificField>& parent_fields) = 0; const std::vector<SpecificField>& /* parent_fields */) = 0;
// Returns true if the unknown field should be ignored. // Returns true if the unknown field should be ignored.
// Note: This will be called for unknown fields as well in which case // Note: This will be called for unknown fields as well in which case
// field.field will be null. // field.field will be null.
virtual bool IsUnknownFieldIgnored( virtual bool IsUnknownFieldIgnored(
const Message& message1, const Message& message2, const Message& /* message1 */, const Message& /* message2 */,
const SpecificField& field, const SpecificField& /* field */,
const std::vector<SpecificField>& parent_fields) { const std::vector<SpecificField>& /* parent_fields */) {
return false; return false;
} }
}; };
......
...@@ -197,7 +197,7 @@ class LIBPROTOBUF_EXPORT WireFormatLite { ...@@ -197,7 +197,7 @@ class LIBPROTOBUF_EXPORT WireFormatLite {
// type-safe, though, so prefer it if possible. // type-safe, though, so prefer it if possible.
#define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \ #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \
static_cast<uint32>( \ static_cast<uint32>( \
((FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \ (static_cast<uint32>(FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \
| (TYPE)) | (TYPE))
// These are the tags for the old MessageSet format, which was defined as: // These are the tags for the old MessageSet format, which was defined as:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment