Add casts to removed undefined behaviors around shifts.

Fixes #4246
Fixes #4247
This commit is contained in:
Thomas Van Lenten 2018-01-31 11:59:57 -05:00
parent b718551571
commit 953adb16ff
6 changed files with 11 additions and 11 deletions

View File

@ -110,7 +110,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
int64_t result = 0;
while (shift < 64) {
int8_t b = ReadRawByte(state);
result |= (int64_t)(b & 0x7F) << shift;
result |= (int64_t)((uint64_t)(b & 0x7F) << shift);
if ((b & 0x80) == 0) {
return result;
}

View File

@ -291,7 +291,7 @@ BOOL GPBGetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber) {
} else {
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t byteIndex = idx / 32;
uint32_t bitMask = (1 << (idx % 32));
uint32_t bitMask = (1U << (idx % 32));
BOOL hasIvar =
(self->messageStorage_->_has_storage_[byteIndex] & bitMask) ? YES : NO;
return hasIvar;
@ -315,7 +315,7 @@ void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t *has_storage = self->messageStorage_->_has_storage_;
uint32_t byte = idx / 32;
uint32_t bitMask = (1 << (idx % 32));
uint32_t bitMask = (1U << (idx % 32));
if (value) {
has_storage[byte] |= bitMask;
} else {

View File

@ -124,7 +124,7 @@ GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
// thus always taking 10 bytes on the wire.)
GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
// 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
@ -133,7 +133,7 @@ GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
// thus always taking 10 bytes on the wire.)
GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
// 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

View File

@ -220,7 +220,7 @@
0xa6, 0x01)
value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
(0x3bLL << 28) | (0x56LL << 35) | (0x00LL << 42) |
(0x05LL << 49) | (0x26LL << 56) | (0x01LL << 63)];
(0x05LL << 49) | (0x26LL << 56) | (0x01ULL << 63)];
// Failures
[self assertReadVarintFailure:bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,

View File

@ -266,7 +266,7 @@
value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) |
(0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) |
(0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) |
(0x01LL << 63)];
(0x01ULL << 63)];
}
- (void)testWriteLittleEndian {

View File

@ -52,12 +52,12 @@
- (void)testRightShiftFunctions {
XCTAssertEqual((1UL << 31) >> 31, 1UL);
XCTAssertEqual((1 << 31) >> 31, -1);
XCTAssertEqual((int32_t)(1U << 31) >> 31, -1);
XCTAssertEqual((1ULL << 63) >> 63, 1ULL);
XCTAssertEqual((1LL << 63) >> 63, -1LL);
XCTAssertEqual((int64_t)(1ULL << 63) >> 63, -1LL);
XCTAssertEqual(GPBLogicalRightShift32((1 << 31), 31), 1);
XCTAssertEqual(GPBLogicalRightShift64((1LL << 63), 63), 1LL);
XCTAssertEqual(GPBLogicalRightShift32((1U << 31), 31), 1);
XCTAssertEqual(GPBLogicalRightShift64((1ULL << 63), 63), 1LL);
}
- (void)testGPBDecodeTextFormatName {