Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception type changed for opening FileStream over invalid file name #54337

Closed
danmoseley opened this issue Jun 17, 2021 · 8 comments · Fixed by #54483
Closed

Exception type changed for opening FileStream over invalid file name #54337

danmoseley opened this issue Jun 17, 2021 · 8 comments · Fixed by #54483
Assignees
Milestone

Comments

@danmoseley
Copy link
Member

Attempting to create FileStream over \\.\COM56 threw FileNotFoundException but now throws IOException. We should either reverse this or document a breaking change.

Microsoft.NET.PackTool.targets(70,5): error MSB4018: The \"GenerateToolsSettingsFile\" task failed unexpectedly. [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
C:\h\w\A22A08BF\p\d\sdk\6.0.100-ci\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.PackTool.targets(70,5): error MSB4018: System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. : '\\.\COM56' [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Win32.SafeHandles.SafeFileHandle.NtCreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6000123+0x112 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6000122+0x6 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.Strategies.WindowsFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005e31+0x2b [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005d93+0x28 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) in System.Private.CoreLib.dll:token 0x60059d2+0x0 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.XmlWriterSettings.CreateWriter(String outputFileName) in System.Private.Xml.dll:token 0x60016eb+0x28 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.XmlWriter.Create(String outputFileName, XmlWriterSettings settings) in System.Private.Xml.dll:token 0x6001676+0xa [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.Linq.XDocument.Save(String fileName, SaveOptions options) in System.Private.Xml.Linq.dll:token 0x60001b0+0x3c [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.Linq.XDocument.Save(String fileName) in System.Private.Xml.Linq.dll:token 0x60001ae+0x0 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.NET.Build.Tasks.GenerateToolsSettingsFile.ExecuteCore() in /_/src/Tasks/Microsoft.NET.Build.Tasks/GenerateToolsSettingsFile.cs:line 29 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.NET.Build.Tasks.TaskBase.Execute() in /_/src/Tasks/Common/TaskBase.cs:line 38 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in Microsoft.Build.dll:token 0x60015f9+0x23 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) in Microsoft.Build.dll:token 0x6001478+0x2b5 
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jun 17, 2021
@ghost
Copy link

ghost commented Jun 17, 2021

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Attempting to create FileStream over \\.\COM56 threw FileNotFoundException but now throws IOException. We should either reverse this or document a breaking change.

Microsoft.NET.PackTool.targets(70,5): error MSB4018: The \"GenerateToolsSettingsFile\" task failed unexpectedly. [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
C:\h\w\A22A08BF\p\d\sdk\6.0.100-ci\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.PackTool.targets(70,5): error MSB4018: System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. : '\\.\COM56' [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Win32.SafeHandles.SafeFileHandle.NtCreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6000123+0x112 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6000122+0x6 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.Strategies.WindowsFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005e31+0x2b [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005d93+0x28 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) in System.Private.CoreLib.dll:token 0x60059d2+0x0 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.XmlWriterSettings.CreateWriter(String outputFileName) in System.Private.Xml.dll:token 0x60016eb+0x28 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.XmlWriter.Create(String outputFileName, XmlWriterSettings settings) in System.Private.Xml.dll:token 0x6001676+0xa [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.Linq.XDocument.Save(String fileName, SaveOptions options) in System.Private.Xml.Linq.dll:token 0x60001b0+0x3c [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at System.Xml.Linq.XDocument.Save(String fileName) in System.Private.Xml.Linq.dll:token 0x60001ae+0x0 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.NET.Build.Tasks.GenerateToolsSettingsFile.ExecuteCore() in /_/src/Tasks/Microsoft.NET.Build.Tasks/GenerateToolsSettingsFile.cs:line 29 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.NET.Build.Tasks.TaskBase.Execute() in /_/src/Tasks/Common/TaskBase.cs:line 38 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in Microsoft.Build.dll:token 0x60015f9+0x23 [C:\h\w\A22A08BF\t\dotnetSdkTests\glh5q3pn.sk4\It_collects_E---795E5A8E\HelloWorld.csproj]
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) in Microsoft.Build.dll:token 0x6001478+0x2b5 
Author: danmoseley
Assignees: -
Labels:

area-System.IO, untriaged

Milestone: -

@danmoseley
Copy link
Member Author

dotnet/sdk#18292 (comment)

when fixed, revert dotnet/sdk@1c1a072

@stephentoub
Copy link
Member

cc: @adamsitnik

@stephentoub stephentoub added this to the 6.0.0 milestone Jun 17, 2021
@adamsitnik
Copy link
Member

What we had for invalid path for both FileMode.Open and FileMode.Create:

System.IO.FileNotFoundException: Could not find file '\\.\COM56'.

What we have now:

System.IO.IOException : The filename, directory name, or volume label syntax is incorrect. : '\\.\COM56'

Since IMO getting FileNotFoundException for FileMode.Create is strange (if the file does not exist, it should get created for FileMode.Create) I am going to keep the new behavior and write a breaking change doc.

@stephentoub @danmoseley does that sound good to you?

@adamsitnik adamsitnik self-assigned this Jun 21, 2021
@adamsitnik
Copy link
Member

adamsitnik commented Jun 21, 2021

We have some tests that check the exception type for invalid path:

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsWildCharacterPath_Core()
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
Assert.ThrowsAny<IOException>(() => Create(Path.Combine(testDir.FullName, "dls;d", "442349-0", "v443094(*)(+*$#$*", new string(Path.DirectorySeparatorChar, 3))));
Assert.ThrowsAny<IOException>(() => Create(Path.Combine(testDir.FullName, "*")));
Assert.ThrowsAny<IOException>(() => Create(Path.Combine(testDir.FullName, "Test*t")));
Assert.ThrowsAny<IOException>(() => Create(Path.Combine(testDir.FullName, "*Tes*t")));
}

Would it be more natural to throw ArgumentException for invalid paths?

Edit: STATUS_OBJECT_NAME_INVALID is returned by NtCreatFile for all kind of invalid file names: the ones that use restricted names like in this example and names that are too long. So IOException seems to be a better choice than ArgumentException

@danmoseley
Copy link
Member Author

@marklio any thoughts?

@adamsitnik what are the cases that changed - any invalid oath e.g. invalid characters?

@stephentoub
Copy link
Member

Is this fallout from the switch to use NtCreateFile?

I'm not seeing a strong rationale here for making a breaking change. FileNotFoundException seems better to me than IOException, and I would not be at all surprised if this actually breaks real code.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 22, 2021
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Jun 22, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 23, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants