-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Permissions: Protect GetIdsFromPathReversed against invalid program exception
#20930
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
base: main
Are you sure you want to change the base?
Permissions: Protect GetIdsFromPathReversed against invalid program exception
#20930
Conversation
GetIdsFromPathReversed against invalid program exception.GetIdsFromPathReversed against invalid program exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR addresses an InvalidProgramException reported on Umbraco Cloud after an upgrade, caused by boxing of a ref struct enumerator. The fix refactors GetIdsFromPathReversed to use an explicit enumerator pattern instead of foreach to prevent potential boxing issues across different compilers/JITs. The PR also adds test coverage for null and empty string inputs and improves the XML documentation.
Key Changes:
- Refactored
GetIdsFromPathReversedfrom foreach loop to explicit while/MoveNext pattern to avoid ref struct boxing - Added test cases for null and empty string inputs
- Enhanced XML documentation with parameter and return value descriptions
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/Umbraco.Core/Extensions/StringExtensions.cs |
Refactored GetIdsFromPathReversed to use explicit enumerator pattern instead of foreach, added defensive comment explaining the change, improved XML documentation |
tests/Umbraco.Tests.UnitTests/Umbraco.Core/ShortStringHelper/StringExtensionsTests.cs |
Added null and empty string test cases, renamed test method for clarity |
| /// <param name="path">The path string expected as a comma delimited collection if integers.</param> | ||
| /// <returns>An array of integers matching the provided path.</returns> | ||
| public static int[] GetIdsFromPathReversed(this string path) | ||
| { |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling AsSpan() on a null string will throw a NullReferenceException. The test cases at lines 392-393 test null and empty strings, so this method needs to handle null input. Consider adding a null check at the beginning: if (path is null or "") return Array.Empty<int>(); or similar.
| { | |
| { | |
| if (string.IsNullOrEmpty(path)) | |
| { | |
| return Array.Empty<int>(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This this is OK as is. We accept string, not string? as input to this function. I added a null input for the test just to verify that we don't get a failure if somehow a null was coerced in.
Co-authored-by: Copilot <[email protected]>
Description
This came from a report on an upgraded site on Umbraco Cloud:
Stack trace:
I couldn't replicate this via unit tests, but from some AI help got the advice to change as indicated by the comment I've added to the code.
Why couldn't a unit test be created to replicate this?
So why this happened on this Cloud site I'm not clear on, but nonetheless this update continues to pass the existing unit tests, plus some additions, so I think it's a good, defensive update to apply.