From 13a3c05e826069f1bf3899369083565781a23f3b Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Thu, 21 Nov 2024 20:10:12 +1300 Subject: [PATCH] Throw ArgumentNullException from public methods --- OpenMcdf/CfbStream.cs | 5 +++++ OpenMcdf/RootStorage.cs | 24 ++++++++++++++++++++++++ OpenMcdf/Storage.cs | 30 +++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/OpenMcdf/CfbStream.cs b/OpenMcdf/CfbStream.cs index 0b2e9d7..b405e34 100644 --- a/OpenMcdf/CfbStream.cs +++ b/OpenMcdf/CfbStream.cs @@ -62,6 +62,8 @@ public override void Flush() public override int Read(byte[] buffer, int offset, int count) { + ThrowHelper.ThrowIfStreamArgumentsAreInvalid(buffer, offset, count); + this.ThrowIfDisposed(isDisposed); return stream.Read(buffer, offset, count); @@ -83,6 +85,9 @@ private void EnsureLengthToWrite(int count) public override void SetLength(long value) { + if (value < 0) + throw new ArgumentOutOfRangeException(nameof(value)); + this.ThrowIfDisposed(isDisposed); this.ThrowIfNotWritable(); diff --git a/OpenMcdf/RootStorage.cs b/OpenMcdf/RootStorage.cs index 0887fdc..60970af 100644 --- a/OpenMcdf/RootStorage.cs +++ b/OpenMcdf/RootStorage.cs @@ -54,6 +54,9 @@ private static IOContextFlags ToIOContextFlags(StorageModeFlags flags) public static RootStorage Create(string fileName, Version version = Version.V3, StorageModeFlags flags = StorageModeFlags.None) { + if (fileName is null) + throw new ArgumentNullException(nameof(fileName)); + ThrowIfLeaveOpen(flags); FileStream stream = File.Create(fileName); @@ -62,6 +65,9 @@ public static RootStorage Create(string fileName, Version version = Version.V3, public static RootStorage Create(Stream stream, Version version = Version.V3, StorageModeFlags flags = StorageModeFlags.None) { + if (stream is null) + throw new ArgumentNullException(nameof(stream)); + stream.ThrowIfNotSeekable(); stream.SetLength(0); stream.Position = 0; @@ -76,6 +82,9 @@ public static RootStorage Create(Stream stream, Version version = Version.V3, St public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags flags = StorageModeFlags.None) { + if (fileName is null) + throw new ArgumentNullException(nameof(fileName)); + ThrowIfInvalid(mode); ThrowIfLeaveOpen(flags); @@ -85,6 +94,9 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags public static RootStorage Open(string fileName, FileMode mode, FileAccess access, StorageModeFlags flags = StorageModeFlags.None) { + if (fileName is null) + throw new ArgumentNullException(nameof(fileName)); + ThrowIfInvalid(mode); ThrowIfInvalid(access); ThrowIfLeaveOpen(flags); @@ -95,6 +107,9 @@ public static RootStorage Open(string fileName, FileMode mode, FileAccess access public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageModeFlags.None) { + if (stream is null) + throw new ArgumentNullException(nameof(stream)); + stream.ThrowIfNotSeekable(); stream.Position = 0; @@ -106,6 +121,9 @@ public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageMo public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None) { + if (fileName is null) + throw new ArgumentNullException(nameof(fileName)); + ThrowIfLeaveOpen(flags); FileStream stream = File.OpenRead(fileName); @@ -203,11 +221,17 @@ private void SwitchToCore(Stream stream, bool allowLeaveOpen) public void SwitchTo(Stream stream) { + if (stream is null) + throw new ArgumentNullException(nameof(stream)); + SwitchToCore(stream, true); } public void SwitchTo(string fileName) { + if (fileName is null) + throw new ArgumentNullException(nameof(fileName)); + FileStream stream = File.Create(fileName); SwitchToCore(stream, false); } diff --git a/OpenMcdf/Storage.cs b/OpenMcdf/Storage.cs index 16337bc..8b0a5da 100644 --- a/OpenMcdf/Storage.cs +++ b/OpenMcdf/Storage.cs @@ -96,6 +96,9 @@ IEnumerable EnumerateDirectoryEntries() public bool ContainsEntry(string name) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed); @@ -105,6 +108,9 @@ public bool ContainsEntry(string name) public bool TryGetEntryInfo(string name, out EntryInfo entryInfo) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed); @@ -130,6 +136,9 @@ DirectoryEntry AddDirectoryEntry(StorageType storageType, string name) public Storage CreateStorage(string name) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed); @@ -141,6 +150,9 @@ public Storage CreateStorage(string name) public CfbStream CreateStream(string name) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed); @@ -157,6 +169,9 @@ public Storage OpenStorage(string name) public bool TryOpenStorage(string name, [MaybeNullWhen(false)] out Storage storage) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed); @@ -179,10 +194,14 @@ public CfbStream OpenStream(string name) public bool TryOpenStream(string name, [MaybeNullWhen(false)] out CfbStream stream) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); + this.ThrowIfDisposed(Context.IsDisposed); - directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry); + directoryTree.TryGetDirectoryEntry(name, out DirectoryEntry? entry); if (entry is null || entry.Type is not StorageType.Stream) { stream = null; @@ -195,6 +214,12 @@ public bool TryOpenStream(string name, [MaybeNullWhen(false)] out CfbStream stre public void CopyTo(Storage destination) { + if (destination is null) + throw new ArgumentNullException(nameof(destination)); + + if (destination == this) + throw new ArgumentException("A storage cannot be copied to itself.", nameof(destination)); + foreach (DirectoryEntry entry in EnumerateDirectoryEntries()) { if (entry.Type is StorageType.Storage) @@ -214,6 +239,9 @@ public void CopyTo(Storage destination) public void Delete(string name) { + if (name is null) + throw new ArgumentNullException(nameof(name)); + ThrowHelper.ThrowIfNameIsInvalid(name); this.ThrowIfDisposed(Context.IsDisposed);