Fix ubsan warning in ParseError testcase

The parse error testcase creates a random byte string
and tries to parse it as a delta attribute expecting it to fail.

Ubsan detected that there was "unsafe" static_cast<>, where
a value from network is static_casted:ed into a enum.
That enum was then *checked* for validity, so I think it was
same before aswell.

This fix changes to do the check/convering as one step.

Bug: webrtc:15392
Change-Id: Ie2534deef8988bc3c3179e194155cfd48b0ee6e5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/343980
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41942}
This commit is contained in:
Jonas Oreland 2024-03-21 14:04:45 +01:00 committed by WebRTC LUCI CQ
parent 709374b333
commit 41e236aa59

View File

@ -18,6 +18,31 @@
namespace cricket {
namespace {
StunAttributeValueType GetStunAttributeValueType(int value_type) {
switch (value_type) {
case STUN_VALUE_ADDRESS:
return STUN_VALUE_ADDRESS;
case STUN_VALUE_XOR_ADDRESS:
return STUN_VALUE_XOR_ADDRESS;
case STUN_VALUE_UINT32:
return STUN_VALUE_UINT32;
case STUN_VALUE_UINT64:
return STUN_VALUE_UINT64;
case STUN_VALUE_BYTE_STRING:
return STUN_VALUE_BYTE_STRING;
case STUN_VALUE_ERROR_CODE:
return STUN_VALUE_ERROR_CODE;
case STUN_VALUE_UINT16_LIST:
return STUN_VALUE_UINT16_LIST;
default:
return STUN_VALUE_UNKNOWN;
}
}
} // namespace
const StunAddressAttribute* StunDictionaryView::GetAddress(int key) const {
const StunAttribute* attr = GetOrNull(key, STUN_VALUE_ADDRESS);
if (attr == nullptr) {
@ -120,7 +145,7 @@ StunDictionaryView::ParseDelta(const StunByteStringAttribute& delta) {
}
StunAttributeValueType value_type_enum =
static_cast<StunAttributeValueType>(value_type);
GetStunAttributeValueType(value_type);
std::unique_ptr<StunAttribute> attr(
StunAttribute::Create(value_type_enum, key, length, nullptr));
if (!attr) {