Skip to content

Commit 3aa1c33

Browse files
skyoxZMihaZupan
andauthored
Ensure the adapter name 100% matching when parsing proc/net/dev (#92187)
* Ensure the adapter name 100% matching when parsing proc/net/dev * Update src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/StringParsingHelpers.Statistics.cs Co-authored-by: Miha Zupan <[email protected]> * Move the stackalloc out of the loop * Update src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/StringParsingHelpers.Statistics.cs Co-authored-by: Miha Zupan <[email protected]> --------- Co-authored-by: Miha Zupan <[email protected]>
1 parent ca23689 commit 3aa1c33

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/StringParsingHelpers.Statistics.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,25 @@ internal static IPInterfaceStatisticsTable ParseInterfaceStatisticsTableFromFile
407407
{
408408
sr.ReadLine();
409409
sr.ReadLine();
410-
int index = 0;
410+
Span<Range> pieces = stackalloc Range[18]; // [0]-[16] used, +1 to ensure any additional segment goes into [17]
411411
while (!sr.EndOfStream)
412412
{
413413
string line = sr.ReadLine()!;
414414
if (line.Contains(name))
415415
{
416-
Span<Range> pieces = stackalloc Range[18]; // [0] skipped, [1]-[16] used, +1 to ensure any additional segment goes into [17]
417416
ReadOnlySpan<char> lineSpan = line;
418-
pieces = pieces.Slice(0, lineSpan.SplitAny(pieces, " :", StringSplitOptions.RemoveEmptyEntries));
417+
int pieceCount = lineSpan.SplitAny(pieces, " :", StringSplitOptions.RemoveEmptyEntries);
418+
419+
if (pieceCount < 17)
420+
{
421+
continue;
422+
}
423+
424+
if (!lineSpan[pieces[0]].SequenceEqual(name))
425+
{
426+
// The adapter name doesn't exactly match.
427+
continue;
428+
}
419429

420430
return new IPInterfaceStatisticsTable()
421431
{
@@ -438,7 +448,6 @@ internal static IPInterfaceStatisticsTable ParseInterfaceStatisticsTableFromFile
438448
CompressedPacketsTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[16]]),
439449
};
440450
}
441-
index += 1;
442451
}
443452

444453
throw ExceptionHelper.CreateForParseFailure();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Inter-| Receive | Transmit
22
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
3+
wlan0a: 20000 394 2 4 6 8 10 12 429496730000 208 1 2 3 4 5 6
34
wlan0: 26622 394 2 4 6 8 10 12 429496730000 208 1 2 3 4 5 6
45
lo: 18446744073709551615 302 0 0 0 0 0 0 30008 302 0 0 0 0 0 0

0 commit comments

Comments
 (0)