From f07e5150480073010aee8b7434dfab296270ce59 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 28 Oct 2021 13:26:49 -0700 Subject: [PATCH] Fix a new SubStorage bug and add NonCopyable attributes --- src/LibHac/Common/NonCopyableAttribute.cs | 10 ++++++++++ src/LibHac/Common/SharedRef.cs | 4 ++++ src/LibHac/Common/UniqueRef.cs | 1 + src/LibHac/Fs/Common/Path.cs | 1 + src/LibHac/Fs/SubStorage.cs | 2 +- src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs | 4 ++-- src/LibHac/FsSystem/Save/AllocationTable.cs | 6 +++--- 7 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/LibHac/Common/NonCopyableAttribute.cs diff --git a/src/LibHac/Common/NonCopyableAttribute.cs b/src/LibHac/Common/NonCopyableAttribute.cs new file mode 100644 index 00000000..967409ff --- /dev/null +++ b/src/LibHac/Common/NonCopyableAttribute.cs @@ -0,0 +1,10 @@ +using System; + +namespace LibHac.Common +{ + [AttributeUsage(AttributeTargets.Struct)] + public sealed class NonCopyableAttribute : System.Attribute { } + + [AttributeUsage(AttributeTargets.Struct)] + public sealed class NonCopyableDisposableAttribute : System.Attribute { } +} diff --git a/src/LibHac/Common/SharedRef.cs b/src/LibHac/Common/SharedRef.cs index 8755540c..64880794 100644 --- a/src/LibHac/Common/SharedRef.cs +++ b/src/LibHac/Common/SharedRef.cs @@ -4,6 +4,8 @@ using System.Threading; using InlineIL; using LibHac.Diag; +#pragma warning disable LH0001, LH0002, LH0003, LH0004, LH0005 + namespace LibHac.Common { public static class SharedRefExtensions @@ -105,6 +107,7 @@ namespace LibHac.Common } } + [NonCopyableDisposable] public struct SharedRef : IDisposable where T : class, IDisposable { // SharedRef and WeakRef should share a base type, but struct inheritance doesn't exist in C#. @@ -300,6 +303,7 @@ namespace LibHac.Common } } + [NonCopyableDisposable] public struct WeakRef : IDisposable where T : class, IDisposable { private T _value; diff --git a/src/LibHac/Common/UniqueRef.cs b/src/LibHac/Common/UniqueRef.cs index 5ca4f95e..66f8cc8a 100644 --- a/src/LibHac/Common/UniqueRef.cs +++ b/src/LibHac/Common/UniqueRef.cs @@ -15,6 +15,7 @@ namespace LibHac.Common } } + [NonCopyableDisposable] public struct UniqueRef : IDisposable where T : class, IDisposable { private T _value; diff --git a/src/LibHac/Fs/Common/Path.cs b/src/LibHac/Fs/Common/Path.cs index fadd7ba5..d01bb9bd 100644 --- a/src/LibHac/Fs/Common/Path.cs +++ b/src/LibHac/Fs/Common/Path.cs @@ -83,6 +83,7 @@ namespace LibHac.Fs /// is normalized before passing it to . /// Based on FS 12.1.0 (nnSdk 12.3.1) [DebuggerDisplay("{" + nameof(ToString) + "(),nq}")] + [NonCopyableDisposable] public ref struct Path { /// diff --git a/src/LibHac/Fs/SubStorage.cs b/src/LibHac/Fs/SubStorage.cs index d3525c96..96469fe2 100644 --- a/src/LibHac/Fs/SubStorage.cs +++ b/src/LibHac/Fs/SubStorage.cs @@ -107,7 +107,7 @@ namespace LibHac.Fs /// The size of the created SubStorage. public SubStorage(ref SharedRef baseStorage, long offset, long size) { - BaseStorage = _sharedBaseStorage.Get; + BaseStorage = baseStorage.Get; _offset = offset; _size = size; _isResizable = false; diff --git a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs index ca00b740..66759ecb 100644 --- a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs +++ b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs @@ -268,7 +268,7 @@ namespace LibHac.FsSrv.Impl var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, allowAllOperations); using var sharedAdapter = new SharedRef(adapter); - adapter._selfReference = new WeakRef(ref sharedAdapter.Ref()); + adapter._selfReference.Set(ref sharedAdapter.Ref()); return SharedRef.CreateMove(ref sharedAdapter.Ref()); } @@ -279,7 +279,7 @@ namespace LibHac.FsSrv.Impl var adapter = new FileSystemInterfaceAdapter(ref baseFileSystem, flags, allowAllOperations); using var sharedAdapter = new SharedRef(adapter); - adapter._selfReference = new WeakRef(ref sharedAdapter.Ref()); + adapter._selfReference.Set(ref sharedAdapter.Ref()); return SharedRef.CreateMove(ref sharedAdapter.Ref()); } diff --git a/src/LibHac/FsSystem/Save/AllocationTable.cs b/src/LibHac/FsSystem/Save/AllocationTable.cs index cdf21821..79c5de15 100644 --- a/src/LibHac/FsSystem/Save/AllocationTable.cs +++ b/src/LibHac/FsSystem/Save/AllocationTable.cs @@ -455,7 +455,7 @@ namespace LibHac.FsSystem.Save public bool IsListStart() { - return Prev == int.MinValue; + return Prev == unchecked((int)0x80000000); } public bool IsListEnd() @@ -485,12 +485,12 @@ namespace LibHac.FsSystem.Save public void MakeListStart() { - Prev = int.MinValue; + Prev = unchecked((int)0x80000000); } public bool IsRangeEntry() { - return Prev != int.MinValue && Prev < 0; + return Prev != unchecked((int)0x80000000) && Prev < 0; } public void MakeRangeEntry()