Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

using System;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -485,7 +486,7 @@ private void ReadPdhMultiString(ref IntPtr strNative, Int32 strSize, ref StringC
{
Debug.Assert(strSize >= 2);
int offset = 0;
string allSubstringsWithNulls = "";
StringBuilder sb = new StringBuilder(strSize);
while (offset <= ((strSize * sizeof(char)) - 4))
{
Int32 next4 = Marshal.ReadInt32(strNative, offset);
Expand All @@ -494,14 +495,19 @@ private void ReadPdhMultiString(ref IntPtr strNative, Int32 strSize, ref StringC
break;
}

allSubstringsWithNulls += (char)next4;
char nextChar = (char)next4;
if (nextChar == '\0')
{
strColl.Add(sb.ToString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to strColl should occur outside the while loop. The updated logic causes the contents of sb to be dropped on the floor by the break statement when next4 == 0.

Essentially, replace the original allSubstringsWithNulls += (char)next4 statement with sb.Append(nextChar) and move the strColl.Add statement to the original location after the while loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but adding to strColl only outside the while loop has some memory implications (has to alloc at least one extra string before Split). Can we just add another strColl.Add(sb.ToString()); outside the while to catch whatever is left in sb?

sb.Clear();
}
else
{
sb.Append(nextChar);
}

offset += 2;
}

allSubstringsWithNulls = allSubstringsWithNulls.TrimEnd('\0');

strColl.AddRange(allSubstringsWithNulls.Split('\0'));
}


Expand Down