diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs
index 898daf1e6c3ed7..946f668dc4a346 100644
--- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs
+++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHelpers.cs
@@ -202,19 +202,18 @@ internal static TarEntryType GetCorrectTypeFlagForFormat(TarEntryFormat format,
/// Parses a byte span that represents an ASCII string containing a number in octal base.
internal static T ParseOctal(ReadOnlySpan buffer) where T : struct, INumber
{
- buffer = TrimEndingNullsAndSpaces(buffer);
-
T octalFactor = T.CreateTruncating(8u);
T value = T.Zero;
- foreach (byte b in buffer)
+
+ // skip leading non-octal bytes
+ int offset = 0;
+ for (; offset < buffer.Length && (buffer[offset] < (byte)'0' || buffer[offset] > (byte)'7'); ++offset);
+
+ foreach (byte b in buffer.Slice(offset))
{
- uint digit = (uint)(b - '0');
- if (digit >= 8)
- {
- ThrowInvalidNumber();
- }
+ if (b < (byte)'0' || b > (byte)'7') break;
- value = checked((value * octalFactor) + T.CreateTruncating(digit));
+ value = checked((value * octalFactor) + T.CreateTruncating((uint)(b - '0')));
}
return value;
diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs
index 5e35c12c3aaac9..75e3e27ef9e0fb 100644
--- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs
+++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.File.Tests.cs
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
@@ -125,28 +125,38 @@ public void Read_Archive_LongFileName_Over100_Under255(TarEntryFormat format, Te
public void Read_Archive_LongPath_Over255(TarEntryFormat format, TestTarFormat testFormat) =>
Read_Archive_LongPath_Over255_Internal(format, testFormat);
- [Fact]
- public void Read_NodeTarArchives_Successfully()
+ public static IEnumerable