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 panic caused by nil pointer dereference in "extract" command #211

Merged

Conversation

patgrasso
Copy link
Contributor

Fixes the issue described in #206 (more specifically, this comment).

In short, if an identifier is used inside a composite literal, e.g.

var x = &i18n.LocalizeConfig{
  Funcs: SomeIdentifier,
}

and that identifier (in this case, SomeIdentifier) is defined in another file, the resulting ast.Ident will have a nil Obj field, which is causing the goi18n extract command to panic.

This just adds a simple nil check before accessing the field to prevent the panic from happening.

When walking the AST during extraction, if an identifier in a composite
literal is encountered that does not define in that file, the `Obj`
field of the `ast.Ident` will be nil.

Rather than panicking in this case, have the literal string extraction
function return false (no string found).

Also, add a test case representing this scenario.
@codecov
Copy link

codecov bot commented Feb 28, 2020

Codecov Report

Merging #211 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #211      +/-   ##
==========================================
+ Coverage   82.85%   82.88%   +0.02%     
==========================================
  Files          14       14              
  Lines        1301     1303       +2     
==========================================
+ Hits         1078     1080       +2     
  Misses        159      159              
  Partials       64       64
Impacted Files Coverage Δ
v2/goi18n/extract_command.go 69.66% <100%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 26334ab...f2c42b9. Read the comment docs.

Copy link
Owner

@nicksnyder nicksnyder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nicksnyder nicksnyder merged commit 949485d into nicksnyder:master Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants