From ac25c4ac3a5099b46bc9f90be5192a35a841e131 Mon Sep 17 00:00:00 2001 From: "david.zagury" Date: Mon, 14 Nov 2022 11:15:19 +0200 Subject: [PATCH 1/2] Fix minor issues found during static code scan Fix using uninitialized value when calling Compare on unit tests. Fix Overrunning array of 16 bytes at byte offset 16 by dereferencing pointer ipa.ip_addr.ipv6_addr + mid + 1 on this case we call memset with a pointer to outside the block. This shouldn't cause any issues due to the fact that we call it with size to be written 0, but still should not be done. --- common/ipprefix.h | 3 ++- tests/stringutility_ut.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/common/ipprefix.h b/common/ipprefix.h index 6a348eb41..3bc964e32 100644 --- a/common/ipprefix.h +++ b/common/ipprefix.h @@ -76,7 +76,8 @@ class IpPrefix { assert(mid >= 0 && mid < 16); ipa.ip_addr.ipv6_addr[mid] = (uint8_t)(0xFF << (8 - bits)); - memset(ipa.ip_addr.ipv6_addr + mid + 1, 0, 16 - mid - 1); + if (mid < 15) + memset(ipa.ip_addr.ipv6_addr + mid + 1, 0, 16 - mid - 1); } return IpAddress(ipa); } diff --git a/tests/stringutility_ut.cpp b/tests/stringutility_ut.cpp index 209392592..0ffff30aa 100644 --- a/tests/stringutility_ut.cpp +++ b/tests/stringutility_ut.cpp @@ -8,7 +8,7 @@ TEST(STRINGUTILITY, cast_int) { - int i; + int i = 0; EXPECT_NO_THROW(swss::lexical_convert("123", i)); EXPECT_EQ(i, 123); @@ -39,7 +39,7 @@ TEST(STRINGUTILITY, cast_alpha_bool) TEST(STRINGUTILITY, cast_mix) { - int i; + int i = 0; swss::AlphaBoolean b; std::string s; From f6848cfe915fdf67e4fe188960b91a098c015b0d Mon Sep 17 00:00:00 2001 From: davidza Date: Tue, 18 Jul 2023 09:31:30 +0300 Subject: [PATCH 2/2] Remove fix for ipprefix issue --- common/ipprefix.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/ipprefix.h b/common/ipprefix.h index 3bc964e32..6a348eb41 100644 --- a/common/ipprefix.h +++ b/common/ipprefix.h @@ -76,8 +76,7 @@ class IpPrefix { assert(mid >= 0 && mid < 16); ipa.ip_addr.ipv6_addr[mid] = (uint8_t)(0xFF << (8 - bits)); - if (mid < 15) - memset(ipa.ip_addr.ipv6_addr + mid + 1, 0, 16 - mid - 1); + memset(ipa.ip_addr.ipv6_addr + mid + 1, 0, 16 - mid - 1); } return IpAddress(ipa); }