Commit 953adb16 authored by Thomas Van Lenten's avatar Thomas Van Lenten

Add casts to removed undefined behaviors around shifts.

Fixes #4246
Fixes #4247
parent b7185515
...@@ -110,7 +110,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) { ...@@ -110,7 +110,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
int64_t result = 0; int64_t result = 0;
while (shift < 64) { while (shift < 64) {
int8_t b = ReadRawByte(state); int8_t b = ReadRawByte(state);
result |= (int64_t)(b & 0x7F) << shift; result |= (int64_t)((uint64_t)(b & 0x7F) << shift);
if ((b & 0x80) == 0) { if ((b & 0x80) == 0) {
return result; return result;
} }
......
...@@ -291,7 +291,7 @@ BOOL GPBGetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber) { ...@@ -291,7 +291,7 @@ BOOL GPBGetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber) {
} else { } else {
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit."); NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t byteIndex = idx / 32; uint32_t byteIndex = idx / 32;
uint32_t bitMask = (1 << (idx % 32)); uint32_t bitMask = (1U << (idx % 32));
BOOL hasIvar = BOOL hasIvar =
(self->messageStorage_->_has_storage_[byteIndex] & bitMask) ? YES : NO; (self->messageStorage_->_has_storage_[byteIndex] & bitMask) ? YES : NO;
return hasIvar; return hasIvar;
...@@ -315,7 +315,7 @@ void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber, ...@@ -315,7 +315,7 @@ void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit."); NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t *has_storage = self->messageStorage_->_has_storage_; uint32_t *has_storage = self->messageStorage_->_has_storage_;
uint32_t byte = idx / 32; uint32_t byte = idx / 32;
uint32_t bitMask = (1 << (idx % 32)); uint32_t bitMask = (1U << (idx % 32));
if (value) { if (value) {
has_storage[byte] |= bitMask; has_storage[byte] |= bitMask;
} else { } else {
......
...@@ -124,7 +124,7 @@ GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) { ...@@ -124,7 +124,7 @@ GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
// thus always taking 10 bytes on the wire.) // thus always taking 10 bytes on the wire.)
GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) { GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
// Note: the right-shift must be arithmetic // Note: the right-shift must be arithmetic
return (uint32_t)((n << 1) ^ (n >> 31)); return ((uint32_t)n << 1) ^ (uint32_t)(n >> 31);
} }
// Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
...@@ -133,7 +133,7 @@ GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) { ...@@ -133,7 +133,7 @@ GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
// thus always taking 10 bytes on the wire.) // thus always taking 10 bytes on the wire.)
GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) { GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
// Note: the right-shift must be arithmetic // Note: the right-shift must be arithmetic
return (uint64_t)((n << 1) ^ (n >> 63)); return ((uint64_t)n << 1) ^ (uint64_t)(n >> 63);
} }
#pragma clang diagnostic push #pragma clang diagnostic push
......
...@@ -220,7 +220,7 @@ ...@@ -220,7 +220,7 @@
0xa6, 0x01) 0xa6, 0x01)
value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
(0x3bLL << 28) | (0x56LL << 35) | (0x00LL << 42) | (0x3bLL << 28) | (0x56LL << 35) | (0x00LL << 42) |
(0x05LL << 49) | (0x26LL << 56) | (0x01LL << 63)]; (0x05LL << 49) | (0x26LL << 56) | (0x01ULL << 63)];
// Failures // Failures
[self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, [self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
......
...@@ -266,7 +266,7 @@ ...@@ -266,7 +266,7 @@
value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) |
(0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) | (0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) |
(0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) | (0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) |
(0x01LL << 63)]; (0x01ULL << 63)];
} }
- (void)testWriteLittleEndian { - (void)testWriteLittleEndian {
......
...@@ -52,12 +52,12 @@ ...@@ -52,12 +52,12 @@
- (void)testRightShiftFunctions { - (void)testRightShiftFunctions {
XCTAssertEqual((1UL << 31) >> 31, 1UL); XCTAssertEqual((1UL << 31) >> 31, 1UL);
XCTAssertEqual((1 << 31) >> 31, -1); XCTAssertEqual((int32_t)(1U << 31) >> 31, -1);
XCTAssertEqual((1ULL << 63) >> 63, 1ULL); XCTAssertEqual((1ULL << 63) >> 63, 1ULL);
XCTAssertEqual((1LL << 63) >> 63, -1LL); XCTAssertEqual((int64_t)(1ULL << 63) >> 63, -1LL);
XCTAssertEqual(GPBLogicalRightShift32((1 << 31), 31), 1); XCTAssertEqual(GPBLogicalRightShift32((1U << 31), 31), 1);
XCTAssertEqual(GPBLogicalRightShift64((1LL << 63), 63), 1LL); XCTAssertEqual(GPBLogicalRightShift64((1ULL << 63), 63), 1LL);
} }
- (void)testGPBDecodeTextFormatName { - (void)testGPBDecodeTextFormatName {
......
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