From 41e236aa5995297d6f47f5e3e11897a45cd71e72 Mon Sep 17 00:00:00 2001 From: Jonas Oreland Date: Thu, 21 Mar 2024 14:04:45 +0100 Subject: [PATCH] Fix ubsan warning in ParseError testcase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Björn Terelius Commit-Queue: Jonas Oreland Cr-Commit-Position: refs/heads/main@{#41942} --- p2p/base/stun_dictionary.cc | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/p2p/base/stun_dictionary.cc b/p2p/base/stun_dictionary.cc index aabb8c394f..4ace8265af 100644 --- a/p2p/base/stun_dictionary.cc +++ b/p2p/base/stun_dictionary.cc @@ -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(value_type); + GetStunAttributeValueType(value_type); std::unique_ptr attr( StunAttribute::Create(value_type_enum, key, length, nullptr)); if (!attr) {