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

Fix counting of nested named fragments #3094

Merged
merged 2 commits into from
Apr 22, 2022

Conversation

cotillion
Copy link
Contributor

@cotillion cotillion commented Apr 21, 2022

Fixes #3093

It's possible there is a better way to do this but it seems to work. There might be some performance impact..

@Shane32
Copy link
Member

Shane32 commented Apr 21, 2022

My only comment is that with the revised code, a recursive query will cause a stack overflow exception. Stack overflow exceptions cannot be caught by any .NET code and will cause an immediate restart of the application without any logging. However, recursive queries are not allowed according to spec, and are typically caught during the validation phase.

Should we add any code to prevent recursively referenced fragments from crashing an app, or leave it as-is?

If so, perhaps something as simple as if (count > 500) throw new InvalidOperationException("Too many nested fragments"); would suffice.

As for performance, so long as there are no fragment references, there is no performance impact. And if there is, it is important that the fragments be counted properly. So the added code seems fine to me. The only alternative is to use a List<ROM> instead, which would require 2 allocations per execution, rather than a rented array. Probably stick with the code as proposed here.

@sungam3r Would you like to comment also?

@Shane32 Shane32 added the bugfix Pull request that fixes a bug label Apr 21, 2022
@sungam3r
Copy link
Member

with the revised code, a recursive query will cause a stack overflow exception.

NoFragmentCycles validation rule handles this case. Of course, one can turn it off 😄 .

As for performance, so long as there are no fragment references, there is no performance impact. And if there is, it is important that the fragments be counted properly.

I agree. One more thing I want to say - it is strange that such an obvious error remained unnoticed. In other places recursion is used when dealing with fragments, and here we just forgot.

if (count > 500) throw new InvalidOperationException("Too many nested fragments");

I would not. As I said, in other places recursion is also used. Validation rules are intended to catch broken requests. Adding such checks here and there in code base will only complicate and pollute the code.

@sungam3r
Copy link
Member

@Shane32 merge if OK.

@Shane32 Shane32 merged commit 23361a7 into graphql-dotnet:master Apr 22, 2022
@Shane32
Copy link
Member

Shane32 commented Apr 22, 2022

Thanks for the contribution @cotillion !!

@cotillion cotillion deleted the spread-exception branch April 22, 2022 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Pull request that fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IndexOutOfRangeException with more than 16 named fragment spread
3 participants