From a05ee35f68c29992e753e0bc30bd539de1b420f2 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 19 Jun 2021 17:54:07 +0300 Subject: [PATCH] UI: Net/Ip4Range: Fix PVS-Studio warning. V610 Undefined behavior. Check the shift operator '<<'. The right operand ('(32 - nbits)' = [1..32]) is greater than or equal to the length in bits of the promoted left operand. ip4range.cpp 152 --- src/tests/UtilTest/tst_netutil.h | 3 +++ src/ui/util/net/ip4range.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tests/UtilTest/tst_netutil.h b/src/tests/UtilTest/tst_netutil.h index 551d9ce5..a8c9089a 100644 --- a/src/tests/UtilTest/tst_netutil.h +++ b/src/tests/UtilTest/tst_netutil.h @@ -41,6 +41,9 @@ TEST_F(NetUtilTest, ip4Ranges) ASSERT_TRUE(ip4Range.fromText("172.16.0.1/32")); ASSERT_EQ(ip4Range.toText(), QString("172.16.0.1\n")); + ASSERT_TRUE(ip4Range.fromText("172.16.0.1/0")); + ASSERT_EQ(ip4Range.toText(), QString("172.16.0.1-255.255.255.255\n")); + // Simple range { ASSERT_TRUE(ip4Range.fromText("127.0.0.1\n" diff --git a/src/ui/util/net/ip4range.cpp b/src/ui/util/net/ip4range.cpp index 373e21c1..918ddc79 100644 --- a/src/ui/util/net/ip4range.cpp +++ b/src/ui/util/net/ip4range.cpp @@ -149,7 +149,7 @@ bool Ip4Range::parseAddressMask(const StringView line, quint32 &from, quint32 &t return false; } - to = from | (nbits == 32 ? 0 : ((1 << (32 - nbits)) - 1)); + to = nbits == 0 ? quint32(-1) : (from | (nbits == 32 ? 0 : ((1 << (32 - nbits)) - 1))); } return true;