From 2d8f27d6ad475911c580c49200ec422b4e751546 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 10 May 2023 12:03:42 +0300 Subject: [PATCH] UI: IpRange: Simpllify parseIp*AddressMaskPrefix() --- src/ui/util/conf/confutil.cpp | 2 +- src/ui/util/net/iprange.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ui/util/conf/confutil.cpp b/src/ui/util/conf/confutil.cpp index 1564fc51..2e944cc8 100644 --- a/src/ui/util/conf/confutil.cpp +++ b/src/ui/util/conf/confutil.cpp @@ -25,7 +25,7 @@ using namespace Qt::StringLiterals; namespace { -bool checkIpRangeSize(const IpRange &range) +inline bool checkIpRangeSize(const IpRange &range) { return (range.ip4Size() + range.pair4Size()) < FORT_CONF_IP_MAX && (range.ip6Size() + range.pair6Size()) < FORT_CONF_IP_MAX; diff --git a/src/ui/util/net/iprange.cpp b/src/ui/util/net/iprange.cpp index 15a742e6..d4ee1f28 100644 --- a/src/ui/util/net/iprange.cpp +++ b/src/ui/util/net/iprange.cpp @@ -9,6 +9,16 @@ namespace { +inline bool checkIp4MaskBitsCount(const int nbits) +{ + return (nbits >= 0 && nbits <= 32); +} + +inline bool checkIp6MaskBitsCount(const int nbits) +{ + return (nbits >= 0 && nbits <= 128); +} + bool compareLessIp6(const ip6_addr_t &l, const ip6_addr_t &r) { return memcmp(&l, &r, sizeof(ip6_addr_t)) < 0; @@ -256,7 +266,7 @@ IpRange::ParseError IpRange::parseIp4AddressMaskPrefix( bool ok = true; const int nbits = mask.isEmpty() ? emptyNetMask : mask.toInt(&ok); - if (!ok || nbits < 0 || nbits > 32) { + if (!ok || !checkIp4MaskBitsCount(nbits)) { setErrorMessage(tr("Bad mask")); setErrorDetails(QString("IPv4 mask='%1' nbits='%2'").arg(mask, QString::number(nbits))); return ErrorBadMask; @@ -331,7 +341,7 @@ IpRange::ParseError IpRange::parseIp6AddressMaskPrefix( bool ok; const int nbits = mask.toInt(&ok); - if (!ok || nbits < 0 || nbits > 128) { + if (!ok || !checkIp6MaskBitsCount(nbits)) { setErrorMessage(tr("Bad mask")); setErrorDetails(QString("IPv6 mask='%1' nbits='%2'").arg(mask, QString::number(nbits))); return ErrorBadMask;