Skip to content

Commit 9e2b9e6

Browse files
loganfindtrugman
authored andcommitted
Implement buddyinfo suggestions
1 parent 2709212 commit 9e2b9e6

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

src/parsers/buddyinfo.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ zone parse_buddyinfo_line(const std::string& line)
6767

6868
zn.name = tokens[ZONE_NAME];
6969

70-
// std::distance can be negative, but the conditional on line 46
71-
// and the nature of the begin() and end() iterators means num_chunks
72-
// should never be less than 0.
73-
size_t num_chunks =
74-
std::distance(tokens.begin() + FIRST_CHUNK, tokens.end());
70+
// This line is protected from underflow thanks to the tokens size
71+
// conditional above.
72+
size_t num_chunks = tokens.size() - FIRST_CHUNK;
7573

7674
zn.chunks.resize(num_chunks);
7775

test/test_buddyinfo.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
99
pfs::zone expected;
1010
std::string line;
1111

12-
SECTION("Sample 1")
12+
SECTION("Default size - Sample 1")
1313
{
1414
line = "Node 0, zone Normal 216 55 189 101 84 38 37 "
1515
" 27 5 3 587";
@@ -19,7 +19,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
1919
expected.chunks = {216, 55, 189, 101, 84, 38, 37, 27, 5, 3, 587};
2020
}
2121

22-
SECTION("Sample 2")
22+
SECTION("Default size - Sample 2")
2323
{
2424
line = "Node 1, zone DMA 1 1 1 0 2 1 1 "
2525
" 0 1 1 3";
@@ -29,8 +29,7 @@ TEST_CASE("Parse buddyinfo", "[procfs][buddyinfo]")
2929
expected.chunks = {1, 1, 1, 0, 2, 1, 1, 0, 1, 1, 3};
3030
}
3131

32-
// 9 chunks instead of the default of 11
33-
SECTION("Sample 3")
32+
SECTION("Custom size - Smaller than default")
3433
{
3534
line = "Node 0, zone Normal 189 101 84 38 37 "
3635
" 27 5 3 587";

0 commit comments

Comments
 (0)