Skip to content

FileSystemAclExtensions.Create broken in multiple ways #57768

@stephentoub

Description

@stephentoub

On .NET Framework, this:

using System;
using System.IO;
using System.Security.AccessControl;

internal class Program
{
    static void Main()
    {
        string path = @"tmp.txt";
        using (FileStream fs = new FileStream(path, FileMode.Create, FileSystemRights.AppendData, FileShare.None, 1, FileOptions.None, null))
        {
            for (int i = 0; i < 26; i++)
            {
                fs.WriteByte((byte)('a' + i));
                fs.Position = 0;
            }
        }

        const string Expected = "abcdefghijklmnopqrstuvwxyz";
        string text = File.ReadAllText(path);
        Console.WriteLine(text == Expected ?
            "Success" :
            $"Expected {Expected}, got {text}");
    }
}

produces:

Success

On .NET 5/6, you should be able to use FileStreamAclExtensions.Create in the exact same way (its whole purpose for existing is to provide the exact same functionality on core):

using System;
using System.IO;
using System.Security.AccessControl;

internal class Program
{
    static void Main()
    {
        string path = @"tmp.txt";
        using (FileStream fs = FileSystemAclExtensions.Create(new FileInfo(path), FileMode.Create, FileSystemRights.AppendData, FileShare.None, 1, FileOptions.None, null))
        {
            for (int i = 0; i < 26; i++)
            {
                fs.WriteByte((byte)('a' + i));
                fs.Position = 0;
            }
        }

        const string Expected = "abcdefghijklmnopqrstuvwxyz";
        string text = File.ReadAllText(path);
        Console.WriteLine(text == Expected ?
            "Success" :
            $"Expected {Expected}, got {text}");
    }
}

but that blows up with an ArgumentNullException:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'fileSecurity')
   at System.IO.FileSystemAclExtensions.Create(FileInfo fileInfo, FileMode mode, FileSystemRights rights, FileShare share, Int32 bufferSize, FileOptions options, FileSecurity fileSecurity)
   at Program.Main()

If you then fix that to allow null, it then fails deeper with how it's mapping to FileStream validation of FileAccess:

Unhandled exception. System.ArgumentOutOfRangeException: Enum value was out of legal range. (Parameter 'access')
   at System.IO.FileStream.ValidateHandle(SafeFileHandle handle, FileAccess access, Int32 bufferSize) in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\IO\FileStream.cs:line 75
   at System.IO.FileStream.ValidateHandle(SafeFileHandle handle, FileAccess access, Int32 bufferSize, Boolean isAsync) in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\IO\FileStream.cs:line 89
   at System.IO.FileStream..ctor(SafeFileHandle handle, FileAccess access, Int32 bufferSize, Boolean isAsync) in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\IO\FileStream.cs:line 115
   at System.IO.FileSystemAclExtensions.Create(FileInfo fileInfo, FileMode mode, FileSystemRights rights, FileShare share, Int32 bufferSize, FileOptions options, FileSecurity fileSecurity) in D:\repos\runtime\src\libraries\System.IO.FileSystem.AccessControl\src\System\IO\FileSystemAclExtensions.cs:line 204
   at Program.Main()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions