Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
44300eb
Throw ArgumentException on unsupported tar entry type
carlossanlop Aug 30, 2022
386ca70
Adjust tests
carlossanlop Aug 30, 2022
4c049be
Also change exception type for internal TarEntry conversion construct…
carlossanlop Aug 30, 2022
712acb5
LinkName setter null check.
carlossanlop Aug 31, 2022
785584d
Internal constructors SeekableSubReadStream and SubReadStream unseeka…
carlossanlop Aug 31, 2022
5931c6b
DataStream setter for regular file should throw ArgumentException if …
carlossanlop Aug 31, 2022
2848e80
TarFile CreateFromDirectory unwritable destination change exception t…
carlossanlop Aug 31, 2022
1647a89
Change to ArgumentException when ExtractToDirectory is an unreadable …
carlossanlop Aug 31, 2022
5216a08
Add some missing exception docs for TarEntry.
carlossanlop Aug 31, 2022
64efec2
Change TarReader constructor exception if unreadable stream. Close te…
carlossanlop Aug 31, 2022
3694af2
Change TarWriter exception for unwritable stream to ArgumentException…
carlossanlop Aug 31, 2022
bc608fc
Add missing documentation for exceptions in constructors.
carlossanlop Aug 31, 2022
b6d620c
Change wording of conversion constructors comment when passing a Pax …
carlossanlop Aug 31, 2022
9c4ccaa
Apply suggestions by Jozkee
carlossanlop Aug 31, 2022
13f52c0
Add exception to LinkName if the entry type is hard/symlink and the u…
carlossanlop Aug 31, 2022
e7d911d
Convert all FormatException to InvalidDataException
carlossanlop Aug 31, 2022
ed4ab9c
Address more suggestions
carlossanlop Aug 31, 2022
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
Prev Previous commit
Next Next commit
Convert all FormatException to InvalidDataException
  • Loading branch information
carlossanlop committed Aug 31, 2022
commit e7d911d659df6f1fe92519fc0016b4c655a797c4
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b
{
if (string.IsNullOrEmpty(LinkName))
{
throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
}

linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, LinkName);
Expand Down Expand Up @@ -516,7 +516,7 @@ private void VerifyPathsForEntryType(string filePath, string? linkTargetPath, bo
}
else
{
throw new FormatException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ internal void ProcessDataBlock(Stream archiveStream, bool copyData)
// No data section
if (_size > 0)
{
throw new FormatException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
}
break;
case TarEntryType.RegularFile:
Expand Down Expand Up @@ -263,7 +263,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca
// No data section
if (_size > 0)
{
throw new FormatException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
throw new InvalidDataException(string.Format(SR.TarSizeFieldTooLargeForEntryType, _typeFlag));
}
break;
case TarEntryType.RegularFile:
Expand Down Expand Up @@ -379,7 +379,7 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca
long size = (int)TarHelpers.ParseOctal<uint>(buffer.Slice(FieldLocations.Size, FieldLengths.Size));
if (size < 0)
{
throw new FormatException(string.Format(SR.TarSizeFieldNegative));
throw new InvalidDataException(string.Format(SR.TarSizeFieldNegative));
}

// Continue with the rest of the fields that require no special checks
Expand Down Expand Up @@ -477,7 +477,7 @@ private void ReadVersionAttribute(Span<byte> buffer)
// Check for gnu version header for mixed case
if (!version.SequenceEqual(GnuVersionBytes))
{
throw new FormatException(string.Format(SR.TarPosixFormatExpected, _name));
throw new InvalidDataException(string.Format(SR.TarPosixFormatExpected, _name));
}

_version = GnuVersion;
Expand All @@ -495,7 +495,7 @@ private void ReadVersionAttribute(Span<byte> buffer)
// Check for ustar or pax version header for mixed case
if (!version.SequenceEqual(UstarVersionBytes))
{
throw new FormatException(string.Format(SR.TarGnuFormatExpected, _name));
throw new InvalidDataException(string.Format(SR.TarGnuFormatExpected, _name));
}

_version = UstarVersion;
Expand Down Expand Up @@ -626,7 +626,7 @@ private void ReadExtendedAttributesFromBuffer(ReadOnlySpan<byte> buffer, string
{
if (!ExtendedAttributes.TryAdd(key, value))
{
throw new FormatException(string.Format(SR.TarDuplicateExtendedAttribute, name));
throw new InvalidDataException(string.Format(SR.TarDuplicateExtendedAttribute, name));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ internal static T ParseOctal<T>(ReadOnlySpan<byte> buffer) where T : struct, INu

[DoesNotReturn]
private static void ThrowInvalidNumber() =>
throw new FormatException(SR.Format(SR.TarInvalidNumber));
throw new InvalidDataException(SR.Format(SR.TarInvalidNumber));

// Returns the string contained in the specified buffer of bytes,
// in the specified encoding, removing the trailing null or space chars.
Expand Down Expand Up @@ -365,7 +365,7 @@ TarEntryType.RegularFile or

case TarEntryFormat.Unknown:
default:
throw new FormatException(string.Format(SR.TarInvalidFormat, archiveFormat));
throw new InvalidDataException(string.Format(SR.TarInvalidFormat, archiveFormat));
}

throw new ArgumentException(string.Format(SR.TarEntryTypeNotSupportedInFormat, entryType, archiveFormat), paramName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async ValueTask DisposeAsync()
/// <para>Set it to <see langword="false"/> if the data should not be copied into a new stream. If the underlying stream is unseekable, the user has the responsibility of reading and processing the <see cref="TarEntry.DataStream"/> immediately after calling this method.</para>
/// <para>The default value is <see langword="false"/>.</para></param>
/// <returns>A <see cref="TarEntry"/> instance if a valid entry was found, or <see langword="null"/> if the end of the archive has been reached.</returns>
/// <exception cref="FormatException"><para>The archive is malformed.</para>
/// <exception cref="InvalidDataException"><para>The entry's data is malformed.</para>
/// <para>-or-</para>
/// <para>The archive contains entries in different formats.</para>
/// <para>-or-</para>
Expand Down Expand Up @@ -154,7 +154,7 @@ public async ValueTask DisposeAsync()
/// <para>The default value is <see langword="false"/>.</para></param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None" />.</param>
/// <returns>A value task containing a <see cref="TarEntry"/> instance if a valid entry was found, or <see langword="null"/> if the end of the archive has been reached.</returns>
/// <exception cref="FormatException"><para>The archive is malformed.</para>
/// <exception cref="InvalidDataException"><para>The archive is malformed.</para>
/// <para>-or-</para>
/// <para>The archive contains entries in different formats.</para>
/// <para>-or-</para>
Expand Down Expand Up @@ -401,7 +401,7 @@ TarEntryType.ExtendedAttributes or
TarEntryType.LongLink or
TarEntryType.LongPath)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes));
}

// Replace all the attributes representing standard fields with the extended ones, if any
Expand Down Expand Up @@ -433,13 +433,13 @@ TarEntryType.ExtendedAttributes or
TarEntryType.LongLink or
TarEntryType.LongPath)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, actualHeader._typeFlag, TarEntryType.ExtendedAttributes));
}

// Can't have two extended attribute metadata entries in a row
if (actualHeader._typeFlag is TarEntryType.ExtendedAttributes)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, TarEntryType.ExtendedAttributes, TarEntryType.ExtendedAttributes));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, TarEntryType.ExtendedAttributes, TarEntryType.ExtendedAttributes));
}

// Replace all the attributes representing standard fields with the extended ones, if any
Expand Down Expand Up @@ -468,7 +468,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta
// Can't have two identical metadata entries in a row
if (secondHeader._typeFlag == header._typeFlag)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag));
}

// It's possible to have the two different metadata entries in a row
Expand All @@ -486,7 +486,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta
// Can't have three GNU metadata entries in a row
if (thirdHeader._typeFlag is TarEntryType.LongLink or TarEntryType.LongPath)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag));
}

if (header._typeFlag is TarEntryType.LongLink)
Expand Down Expand Up @@ -543,7 +543,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta
// Can't have two identical metadata entries in a row
if (secondHeader._typeFlag == header._typeFlag)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, secondHeader._typeFlag, header._typeFlag));
}

TarHeader finalHeader;
Expand All @@ -562,7 +562,7 @@ private bool TryProcessGnuMetadataHeader(TarHeader header, bool copyData, out Ta
// Can't have three GNU metadata entries in a row
if (thirdHeader._typeFlag is TarEntryType.LongLink or TarEntryType.LongPath)
{
throw new FormatException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag));
throw new InvalidDataException(string.Format(SR.TarUnexpectedMetadataEntry, thirdHeader._typeFlag, secondHeader._typeFlag));
}

if (header._typeFlag is TarEntryType.LongLink)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private TarEntry ConstructEntryForWriting(string fullPath, string entryName, Fil
TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName),
TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName),
TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName),
_ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)),
_ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)),
};

if (entryType is TarEntryType.BlockDevice or TarEntryType.CharacterDevice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private TarEntry ConstructEntryForWriting(string fullPath, string entryName, Fil
TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName),
TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName),
TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName),
_ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)),
_ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)),
};

FileSystemInfo info = (attributes & FileAttributes.Directory) != 0 ? new DirectoryInfo(fullPath) : new FileInfo(fullPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void WriteEntryInternal(TarEntry entry)

default:
Debug.Assert(entry.Format == TarEntryFormat.Unknown, "Missing format handler");
throw new FormatException(string.Format(SR.TarInvalidFormat, Format));
throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format));
}

_wroteEntries = true;
Expand All @@ -324,7 +324,7 @@ private async Task WriteEntryAsyncInternal(TarEntry entry, CancellationToken can
TarEntryFormat.Pax when entry._header._typeFlag is TarEntryType.GlobalExtendedAttributes => entry._header.WriteAsPaxGlobalExtendedAttributesAsync(_archiveStream, buffer, _nextGlobalExtendedAttributesEntryNumber++, cancellationToken),
TarEntryFormat.Pax => entry._header.WriteAsPaxAsync(_archiveStream, buffer, cancellationToken),
TarEntryFormat.Gnu => entry._header.WriteAsGnuAsync(_archiveStream, buffer, cancellationToken),
_ => throw new FormatException(string.Format(SR.TarInvalidFormat, Format)),
_ => throw new InvalidDataException(string.Format(SR.TarInvalidFormat, Format)),
};
await task.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected TarEntry InvokeTarEntryConversionConstructor(TarEntryFormat targetForm
TarEntryFormat.Ustar => new UstarTarEntry(other),
TarEntryFormat.Pax => new PaxTarEntry(other),
TarEntryFormat.Gnu => new GnuTarEntry(other),
_ => throw new FormatException($"Unexpected format: {targetFormat}")
_ => throw new InvalidDataException($"Unexpected format: {targetFormat}")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public async Task Throw_FifoContainsNonZeroDataSectionAsync()
Assert.NotNull(await reader.GetNextEntryAsync());
Assert.NotNull(await reader.GetNextEntryAsync());
Assert.NotNull(await reader.GetNextEntryAsync());
await Assert.ThrowsAsync<FormatException>(async () => await reader.GetNextEntryAsync());
await Assert.ThrowsAsync<InvalidDataException>(async () => await reader.GetNextEntryAsync());
}

[Fact]
Expand Down Expand Up @@ -268,7 +268,7 @@ public async Task Throw_ArchivesWithRandomCharsAsync(string testCaseName)
{
await using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", testCaseName);
await using TarReader reader = new TarReader(archiveStream);
await Assert.ThrowsAsync<FormatException>(async () => await reader.GetNextEntryAsync());
await Assert.ThrowsAsync<InvalidDataException>(async () => await reader.GetNextEntryAsync());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void Throw_FifoContainsNonZeroDataSection()
Assert.NotNull(reader.GetNextEntry());
Assert.NotNull(reader.GetNextEntry());
Assert.NotNull(reader.GetNextEntry());
Assert.Throws<FormatException>(() => reader.GetNextEntry());
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
}

[Fact]
Expand Down Expand Up @@ -268,7 +268,7 @@ public void Throw_ArchivesWithRandomChars(string testCaseName)
{
using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "golang_tar", testCaseName);
using TarReader reader = new TarReader(archiveStream);
Assert.Throws<FormatException>(() => reader.GetNextEntry());
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void MalformedArchive_HeaderSize()
malformed.Seek(0, SeekOrigin.Begin);

using TarReader reader = new TarReader(malformed);
Assert.Throws<FormatException>(() => reader.GetNextEntry());
Assert.Throws<InvalidDataException>(() => reader.GetNextEntry());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task MalformedArchive_HeaderSize_Async()

await using (TarReader reader = new TarReader(malformed))
{
await Assert.ThrowsAsync<FormatException>(async () => await reader.GetNextEntryAsync());
await Assert.ThrowsAsync<InvalidDataException>(async () => await reader.GetNextEntryAsync());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Formats.Tar/tests/TarTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected Type GetTypeForFormat(TarEntryFormat expectedFormat)
TarEntryFormat.Ustar => typeof(UstarTarEntry),
TarEntryFormat.Pax => typeof(PaxTarEntry),
TarEntryFormat.Gnu => typeof(GnuTarEntry),
_ => throw new FormatException($"Unrecognized format: {expectedFormat}"),
_ => throw new InvalidDataException($"Unrecognized format: {expectedFormat}"),
};
}

Expand Down Expand Up @@ -483,7 +483,7 @@ protected TarEntry InvokeTarEntryCreationConstructor(TarEntryFormat targetFormat
TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName),
TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName),
TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName),
_ => throw new FormatException($"Unexpected format: {targetFormat}")
_ => throw new InvalidDataException($"Unexpected format: {targetFormat}")
};

public static IEnumerable<object[]> GetFormatsAndLinks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void Write_RegularFileEntry_In_V7Writer(TarEntryFormat entryFormat)
TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, InitialEntryName),
TarEntryFormat.Pax => new PaxTarEntry(TarEntryType.RegularFile, InitialEntryName),
TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, InitialEntryName),
_ => throw new FormatException($"Unexpected format: {entryFormat}")
_ => throw new InvalidDataException($"Unexpected format: {entryFormat}")
};

// Should be written in the format of the entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task Write_RegularFileEntry_In_V7Writer_Async(TarEntryFormat entryF
TarEntryFormat.Ustar => new UstarTarEntry(TarEntryType.RegularFile, InitialEntryName),
TarEntryFormat.Pax => new PaxTarEntry(TarEntryType.RegularFile, InitialEntryName),
TarEntryFormat.Gnu => new GnuTarEntry(TarEntryType.RegularFile, InitialEntryName),
_ => throw new FormatException($"Unexpected format: {entryFormat}")
_ => throw new InvalidDataException($"Unexpected format: {entryFormat}")
};

// Should be written in the format of the entry
Expand Down