Skip to content

Commit 6ab8312

Browse files
committed
Fix storeEnumTagSinglePayload on big-endian systems
The part of the tag stored in the payload can currently be up to 8 bytes in size (though only the 'low' 4 bytes can be non-zero). On little-endian machines this doesn't matter, we can always just store up to 4 bytes and zero the remaining payload bytes. On big- endian systems however we may need to store more than 4 bytes. The store implementation now mirrors the runtime code that fetches the tag on big-endian systems which already treats the payload tag as an 8 byte integer. This is a spot fix but longer term we might want to consider refactoring this code to reduce the number of differences between big- and little-endian implementations. For example, we could centralise some of the copying logic and/or make the payload tag a 4 byte field on all platforms.
1 parent 9b0a94f commit 6ab8312

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

stdlib/public/runtime/EnumImpl.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ inline void storeEnumTagSinglePayloadImpl(
170170

171171
// Store into the value.
172172
#if defined(__BIG_ENDIAN__)
173-
unsigned numPayloadTagBytes = std::min(size_t(4), payloadSize);
174-
if (numPayloadTagBytes)
175-
small_memcpy(valueAddr,
176-
reinterpret_cast<uint8_t *>(&payloadIndex) + 4 -
177-
numPayloadTagBytes,
178-
numPayloadTagBytes, true);
179-
if (payloadSize > 4)
180-
memset(valueAddr + 4, 0, payloadSize - 4);
173+
uint64_t payloadIndexBuf = static_cast<uint64_t>(payloadIndex);
174+
if (payloadSize >= 8) {
175+
memcpy(valueAddr, &payloadIndexBuf, 8);
176+
memset(valueAddr + 8, 0, payloadSize - 8);
177+
} else if (payloadSize > 0) {
178+
payloadIndexBuf <<= (8 - payloadSize) * 8;
179+
memcpy(valueAddr, &payloadIndexBuf, payloadSize);
180+
}
181181
if (numExtraTagBytes)
182182
small_memcpy(extraTagBitAddr,
183183
reinterpret_cast<uint8_t *>(&extraTagIndex) + 4 -

0 commit comments

Comments
 (0)