aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-19 23:09:19 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-19 23:09:19 +0000
commitc07391bf3eabdca20faf64d71d63046889c887ba (patch)
tree1e464c080aaf7c79d1eae8124663e09f41335894
parent30afb86166e599b3cf6ca58444884a813094e51d (diff)
parentf87bd1dc071f3d79c96f3341a419e674dbd6c85f (diff)
downloadprotobuf-android14-qpr1-release.tar.gz
Change-Id: Ic89f789a5fcffe5a54e44f688960bf2af5a84662
-rw-r--r--src/google/protobuf/parse_context.h14
-rw-r--r--src/google/protobuf/wire_format_lite.h12
2 files changed, 25 insertions, 1 deletions
diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h
index f674933ba..fc4db9a6f 100644
--- a/src/google/protobuf/parse_context.h
+++ b/src/google/protobuf/parse_context.h
@@ -194,7 +194,13 @@ class PROTOBUF_EXPORT EpsCopyInputStream {
}
bool EndedAtLimit() const { return last_tag_minus_1_ == 0; }
bool EndedAtEndOfStream() const { return last_tag_minus_1_ == 1; }
- void SetLastTag(uint32_t tag) { last_tag_minus_1_ = tag - 1; }
+ void SetLastTag(uint32_t tag) {
+#if __has_builtin(__builtin_sub_overflow)
+ __builtin_sub_overflow(tag, 1, &last_tag_minus_1_);
+#else
+ last_tag_minus_1_ = tag - 1;
+#endif
+ }
void SetEndOfStream() { last_tag_minus_1_ = 1; }
bool IsExceedingLimit(const char* ptr) {
return ptr > limit_end_ &&
@@ -566,7 +572,13 @@ inline const char* ReadTag(const char* p, uint32_t* out,
return p + 1;
}
uint32_t second = static_cast<uint8_t>(p[1]);
+#if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow)
+ uint32_t second_minus_1;
+ __builtin_sub_overflow(second, 1, &second_minus_1);
+ __builtin_add_overflow(res, second_minus_1 << 7, &res);
+#else
res += (second - 1) << 7;
+#endif
if (second < 128) {
*out = res;
return p + 2;
diff --git a/src/google/protobuf/wire_format_lite.h b/src/google/protobuf/wire_format_lite.h
index 80d396156..e4b94d7bc 100644
--- a/src/google/protobuf/wire_format_lite.h
+++ b/src/google/protobuf/wire_format_lite.h
@@ -864,8 +864,14 @@ inline uint32_t WireFormatLite::ZigZagEncode32(int32_t n) {
}
inline int32_t WireFormatLite::ZigZagDecode32(uint32_t n) {
+#if __has_builtin(__builtin_add_overflow)
+ uint32_t xor_mask;
+ __builtin_add_overflow(~(n & 1), 1, &xor_mask);
+ return static_cast<int32_t>((n >> 1) ^ xor_mask);
+#else
// Note: Using unsigned types prevent undefined behavior
return static_cast<int32_t>((n >> 1) ^ (~(n & 1) + 1));
+#endif
}
inline uint64_t WireFormatLite::ZigZagEncode64(int64_t n) {
@@ -875,8 +881,14 @@ inline uint64_t WireFormatLite::ZigZagEncode64(int64_t n) {
}
inline int64_t WireFormatLite::ZigZagDecode64(uint64_t n) {
+#if __has_builtin(__builtin_add_overflow)
+ uint64_t xor_mask;
+ __builtin_add_overflow(~(n & 1), 1, &xor_mask);
+ return static_cast<int64_t>((n >> 1) ^ xor_mask);
+#else
// Note: Using unsigned types prevent undefined behavior
return static_cast<int64_t>((n >> 1) ^ (~(n & 1) + 1));
+#endif
}
// String is for UTF-8 text only, but, even so, ReadString() can simply