Skip to content

[dotnet-sdk-8.0.100-alpha.1.22619.1] System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' exception thrown when OpenFileDialog.Multiselect=true #8414

Closed

Description

@Junjun-zhao

.NET version

.NET SDK:
Version:   8.0.100-alpha.1.22619.1
Commit:    8750a90545
Runtime Environment:
OS Name:     Windows
OS Version:  10.0.19045
OS Platform: Windows
RID:         win10-x64
Base Path:   C:\Program Files\dotnet\sdk\8.0.100-alpha.1.22619.1\

Host:
 Version:      8.0.0-alpha.1.22605.1
 Architecture: x64
 Commit:       1a37caf773

.NET SDKs installed:
  8.0.100-alpha.1.22619.1 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.0-alpha.1.22615.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.0-alpha.1.22605.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 8.0.0-alpha.1.22614.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Verify Scenarios:
1). Windows10 21H2 x64+dotnet-sdk-8.0.100-alpha.1.22621.5: Fail
2). Windows10 21H2 x64+dotnet-sdk-8.0.100-alpha.1.22619.1: Fail
3). Windows10 21H2 x64+dotnet-sdk-8.0.100-alpha.1.22616.7: Fail
4). Windows10 21H2 x64+dotnet-sdk-7.0.101: Pass

Issue description

When testing the 3rd Party Apps with .NET 8 runtime, System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' exception thrown when OpenFileDialog.Multiselect=true

Application Name: paint.netcore, XamlViewer
OS: Windows 10 21H2
CPU: X64
.NET Build Number: dotnet-sdk-8.0.100-alpha.1.22619.1

App or App Source checking at : https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1709145

Steps to reproduce

Repro steps:
The machine only has 8.0 installed. And DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=2 is set.

  1. Copy paint.net app to local machine.
  2. Launch paintdotnet.exe.
  3. Click File > Open.
  4. Select one .png file, click Open button

Expected Result:
The png file will be opened in paint.net app successfully.

Actual Result:
Open failed and app crashed with following error (The crash log file pdncrash.1.log ):
image

Minimal Repro sample:
Minimal repro sample attached.
OpenFileDemo.zip

  1. Create a .NET 7.0 WinForm app.
  2. Add following code in Form1.cs file:
 public Form1()
        {
            InitializeComponent();
            openFileDialog1 = new OpenFileDialog();

            selectButton = new Button()
            {
                Size = new Size(100, 20),
                Location = new Point(15, 15),
                Text = "Select file"
            };
            selectButton.Click += new EventHandler(selectButton_Click);
            Controls.Add(selectButton);
        }

        private Button selectButton;
        private OpenFileDialog openFileDialog1;


        private void selectButton_Click(object sender, EventArgs e)
        {

            openFileDialog1.Multiselect = true; //This line of code will cause the exception

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {

                    var filePath = openFileDialog1.FileName;

                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                    $"Details:\n\n{ex.StackTrace}");
                }
            }
        }
  1. Build the project.
  2. Change the runtimeconfig to let the app run against with .NET 8.0.100-alpha.1.22619.1:
{
  "runtimeOptions": {
    "tfm": "net7.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "8.0.0-alpha.1.22605.1"
      },
      {
        "name": "Microsoft.WindowsDesktop.App",
        "version": "8.0.0-alpha.1.22614.1"
      }
    ]
  }
}
  1. Press F5 to launch the sample app.
  2. Click Select button and select a file to open it.

Excepted Result:
No exception thrown.

Actual Result:
This exception is thrown in code:
image

Findings:
This issue only occurs when Multiselect=true for OpenFileDialog. If we remove Multiselect=true from source code, this issue will go.

@dotnet-actwx-bot @dotnet/compat

Activity

  1. added
    untriaged
    on Dec 22, 2022
  2. added
    💥 regression-preview
    on Dec 28, 2022
  3. elachlan commented on Jan 5, 2023

    @elachlan
    Contributor

    There is a few issues here:

    The FileName property getter doesn't check if there are items in the _fileNames array.

    /// <summary>
    /// Gets or sets a string containing the file name selected in the file dialog box.
    /// </summary>
    [SRCategory(nameof(SR.CatData))]
    [DefaultValue("")]
    [SRDescription(nameof(SR.FDfileNameDescr))]
    [AllowNull]
    public string FileName
    {
    get => _fileNames is null ? string.Empty : _fileNames[0];
    set => _fileNames = value is not null ? new string[] { value } : null;
    }

    Fix:

    get => _fileNames is null || !_fileNames.Any() ? string.Empty : _fileNames[0];

    The other issue is that _fileNames should have an item in it, but it doesn't. It is set in DoFileOk. Which conditionally set the _fileNames based on the OFN_ALLOWMULTISELECT flag.

    _fileNames = _fileNameFlags.HasFlag(OFN_ALLOWMULTISELECT)
    ? GetMultiselectFiles(new((char*)lpOFN->lpstrFile, (int)lpOFN->nMaxFile))
    : new string[] { lpOFN->lpstrFile.ToString() };

    I am unsure how we can interact with a filedialog in an integration test so we can avoid regressions. Would manual testing be ok?

  4. dreddy-work commented on Jan 6, 2023

    @dreddy-work
    Member

    The FileName property getter doesn't check if there are items in the _fileNames array.

    Do you know which commit caused this regression? Is this from CSWin32 changes?

  5. elachlan commented on Jan 6, 2023

    @elachlan
    Contributor

    I haven't bisected it. But I suspect its caused by #7933

    That refactored GetMultiselectFiles and DoFileOk. I am having issues with debug not working in VS preview, otherwise I would have tracked it down.

  6. dreddy-work commented on Jan 6, 2023

    @dreddy-work
    Member

    Okay. Thank you.

    I am having issues with debug not working in VS preview

    FYI - Latest SDK install should unblock you from debugging.

  7. added this to the 7.0.3 milestone on Jan 10, 2023
  8. merriemcgaw commented on Jan 10, 2023

    @merriemcgaw
    Contributor

    @JeremyKuhne could you take a look at this one when you have a chance? I'd like to get it serviced sooner rather than later.

  9. elachlan commented on Jan 10, 2023

    @elachlan
    Contributor

    @merriemcgaw I don't think its affecting 7.x just 8.x.

  10. ghost added
    🚧 work in progress
    on Jan 13, 2023
  11. JeremyKuhne commented on Jan 13, 2023

    @JeremyKuhne
    Member

    @elachlan I fixed the issue and found a way to automate the regression test.

  12. ghost removed
    🚧 work in progress
    on Jan 13, 2023
  13. ghost removed this from the 8.0 Preview1 milestone on Jan 13, 2023
  14. Nora-Zhou01 commented on Jan 20, 2023

    @Nora-Zhou01
    Contributor

    Verified with .NET SDK 8.0.100-alpha.1.23068.16 build from main branch, this issue is fixed that the png file will be opened in paint.net app successfully.
    open

  15. Junjun-zhao commented on Jan 30, 2023

    @Junjun-zhao
    MemberAuthor

    Verified with dotnet-sdk-8.0.100-alpha.1.23077.6, this issue has been fixed.

  16. added this to the 8.0 Preview1 milestone on Feb 4, 2023
  17. John-Qiao commented on Feb 13, 2023

    @John-Qiao
    Contributor

    Verified with .NET 8 Preview 1 TP build: .NET 8.0.100-preview.1.23108.10, issue was fixed as same as above test result.

  18. ghost locked as resolved and limited conversation to collaborators on Mar 15, 2023
  19. removed
    untriaged
    on May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

💥 regression-preview

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions