Repository navigation
fix: prevent nil pointer dereference in getFuncDoc when parsing depen… - #2044
Merged
Merged
Conversation
…dencies Add comprehensive nil checks in getFuncDoc function to handle incomplete AST nodes from dependency packages. This resolves segmentation faults that occur when parsing standard library types like atomic.Int32, json.scanner, and ecdh.PrivateKey with ParseDependency enabled. - Check for empty astDecl.Specs before accessing astDecl.Specs[0] - Check for empty astDecl.Values before accessing astDecl.Values[0] - Add nil checks for value.Obj and value.Obj.Decl chain Fixes critical crash when parsing projects with dependencies.
GordonFremen
approved these changes
Jul 17, 2025
Contributor
Author
|
Just trying to run the install command locally and make sure this works before I take out of draft. |
Contributor
Author
|
local testing outcomes: my swagger:
swag init -o ./swagger --parseDependencyLevel=3 --parseInternalthis continues to raise the segmentation problem my updated swagger:
go run -mod=mod github.com/swaggo/swag/cmd/swag init -o ./swagger --parseDependencyLevel=3 --parseInternalthis works now! I think this is good. But I'm a Go noob so please review and test yourselves. Also my team at work has looked at it. |
gatorjuice
marked this pull request as ready for review
July 17, 2025 15:41
sdghchj
approved these changes
Jul 18, 2025
Member
|
Thanks |
Contributor
Author
My pleasure, we enjoy this project very much @sdghchj and look forward to OpenAPI 3.0 support! |
pborgen
pushed a commit
to pborgen/swag
that referenced
this pull request
Jan 8, 2026
…dencies (swaggo#2044) Add comprehensive nil checks in getFuncDoc function to handle incomplete AST nodes from dependency packages. This resolves segmentation faults that occur when parsing standard library types like atomic.Int32, json.scanner, and ecdh.PrivateKey with ParseDependency enabled. - Check for empty astDecl.Specs before accessing astDecl.Specs[0] - Check for empty astDecl.Values before accessing astDecl.Values[0] - Add nil checks for value.Obj and value.Obj.Decl chain Fixes critical crash when parsing projects with dependencies. Co-authored-by: James Gates <jgates@corebt.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#2042
Fix nil pointer dereference in getFuncDoc function when parsing dependencies
Problem
When parsing dependencies with
ParseDependencyenabled, swag encounters a segmentation fault due to nil pointer dereferences in thegetFuncDocfunction. This occurs when the parser encounters AST nodes from standard library types (likeatomic.Int32,json.scanner,ecdh.PrivateKey, etc.) that don't have all their fields properly initialized.Error Stack Trace:
Root Cause
The
getFuncDocfunction was accessing AST node fields without proper nil checks:*ast.GenDeclcase, it accessedastDecl.Specs[0]without checking ifastDecl.Specswas empty*ast.ValueSpeccase, it accessedastDecl.Values[0]without checking ifastDecl.Valueswas emptyvalue.Obj.Declwithout checking ifvalue.Objorvalue.Obj.Declwere nilWhen parsing dependencies, especially standard library packages, these AST nodes may not have all fields initialized, leading to nil pointer dereferences.
Solution
Added comprehensive nil checks in the
getFuncDocfunction:if len(astDecl.Specs) == 0before accessingastDecl.Specs[0]if len(astDecl.Values) == 0before accessingastDecl.Values[0]value.Obj == nil || value.Obj.Decl == nilChanges Made
parser.goat lines 1052, 1062, and 1066Testing
This fix resolves the segmentation fault when running swag with dependency parsing enabled. The parser now gracefully skips over problematic AST nodes instead of crashing.
Impact
This is a critical stability fix that allows swag to successfully parse projects with dependencies without crashing.