From 0f8e6d1402fc021e02b9def83df9a9683ba6ea76 Mon Sep 17 00:00:00 2001 From: Harvey Tuch Date: Fri, 15 Mar 2019 17:20:56 -0400 Subject: [PATCH] Fix some UBSAN warnings. * external/com_google_protobuf/src/google/protobuf/stubs/strutil.cc:1122:9: runtime error: negation of -9223372036854775808 cannot be represented in type 'google::protobuf::int64' (aka 'long'); cast to an unsigned type to negate this value to itself * Bad external/com_google_protobuf/src/google/protobuf/text_format.cc:1320:14: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/string.h:62:62: note: nonnull attribute specified here Signed-off-by: Harvey Tuch --- src/google/protobuf/stubs/strutil.cc | 6 ++++-- src/google/protobuf/text_format.cc | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc index 1d34870de..3844fa6b8 100644 --- a/src/google/protobuf/stubs/strutil.cc +++ b/src/google/protobuf/stubs/strutil.cc @@ -1116,10 +1116,12 @@ char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) { } char* FastInt64ToBufferLeft(int64 i, char* buffer) { - uint64 u = i; + uint64 u = 0; if (i < 0) { *buffer++ = '-'; - u = -i; + u -= i; + } else { + u = i; } return FastUInt64ToBufferLeft(u, buffer); } diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc index ba0c3028e..801a8e378 100644 --- a/src/google/protobuf/text_format.cc +++ b/src/google/protobuf/text_format.cc @@ -1315,7 +1315,9 @@ class TextFormat::Printer::TextGenerator while (size > buffer_size_) { // Data exceeds space in the buffer. Write what we can and request a new // buffer. - memset(buffer_, ' ', buffer_size_); + if (buffer_size_ > 0) { + memset(buffer_, ' ', buffer_size_); + } size -= buffer_size_; void* void_buffer; failed_ = !output_->Next(&void_buffer, &buffer_size_);