Skip to content

Commit 307113d

Browse files
committed
Fixed computation of ip mask
1 parent 4f8fd38 commit 307113d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/http_utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ ip_representation::ip_representation(const std::string& ip)
475475
else
476476
{
477477
CLEAR_BIT(mask, y);
478-
y++;
478+
CLEAR_BIT(mask, y+1);
479+
y+=2;
479480
}
480481
}
481482
}

src/httpserver/http_utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct ip_representation
302302
int weight() const
303303
{
304304
//variable-precision SWAR algorithm
305-
unsigned int x = mask;
305+
unsigned short x = mask;
306306
x = x - ((x >> 1) & 0x55555555);
307307
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
308308
return (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;

test/unit/http_utils_test.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_str_mask)
237237
LT_CHECK_EQ(test_ip.pieces[14], 0);
238238
LT_CHECK_EQ(test_ip.pieces[15], 0);
239239

240-
LT_CHECK_EQ(test_ip.mask, 0xFCFF);
240+
LT_CHECK_EQ(test_ip.mask, 0xF0FF);
241241
LT_END_AUTO_TEST(ip_representation6_str_mask)
242242

243243
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_str_nested)
@@ -366,6 +366,17 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_str_loopback)
366366
LT_CHECK_EQ(test_ip.mask, 0xFFFF);
367367
LT_END_AUTO_TEST(ip_representation6_str_loopback)
368368

369+
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation_weight)
370+
LT_CHECK_EQ(http::ip_representation("::1").weight(), 16);
371+
LT_CHECK_EQ(http::ip_representation("192.168.0.1").weight(), 16);
372+
LT_CHECK_EQ(http::ip_representation("192.168.*.*").weight(), 14);
373+
LT_CHECK_EQ(http::ip_representation("::ffff:192.0.*.*").weight(), 14);
374+
LT_CHECK_EQ(http::ip_representation("2001:db8:8714:3a90:*:*").weight(), 12);
375+
LT_CHECK_EQ(http::ip_representation("2001:db8:8714:3a90:8714:2001:db8:3a90").weight(), 16);
376+
LT_CHECK_EQ(http::ip_representation("2001:db8:8714:3a90:8714:2001:*:*").weight(), 12);
377+
LT_CHECK_EQ(http::ip_representation("*:*:*:*:*:*:*:*").weight(), 0);
378+
LT_END_AUTO_TEST(ip_representation_weight)
379+
369380
LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_str_invalid)
370381
LT_CHECK_THROW(http::ip_representation("2001:db8:8714:3a90::12:4:4:4"));
371382
LT_END_AUTO_TEST(ip_representation6_str_invalid)

0 commit comments

Comments
 (0)