Skip to content

Handle assignments to global variables from the init function#253

Open
k4n4ry wants to merge 8 commits intouber-go:mainfrom
k4n4ry:handle-init-func
Open

Handle assignments to global variables from the init function#253
k4n4ry wants to merge 8 commits intouber-go:mainfrom
k4n4ry:handle-init-func

Conversation

@k4n4ry
Copy link

@k4n4ry k4n4ry commented Jun 2, 2024

fixes #56

  • Added logic in GlobalAnalyzer's run() to retrieve the init function from the file.
  • Within getGlobalConsumers(), analyzed assignments to global variables from the init function and used it as one of the criteria for determining consumer creation.

Please provide comments if this implementation differs from nilaway's coding practices or design principles. Thank you.

@CLAassistant
Copy link

CLAassistant commented Jun 2, 2024

CLA assistant check
All committers have signed the CLA.

@sonalmahajan15
Copy link
Contributor

@k4n4ry, thank you for submitting the PR! We'll take a look soon and follow up with you.

@codecov
Copy link

codecov bot commented Jun 8, 2024

Codecov Report

❌ Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.26%. Comparing base (99d94ca) to head (7acc4b6).

Files with missing lines Patch % Lines
assertion/global/analyzer.go 92.85% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #253      +/-   ##
==========================================
+ Coverage   87.16%   87.26%   +0.09%     
==========================================
  Files          72       72              
  Lines        8291     8336      +45     
==========================================
+ Hits         7227     7274      +47     
+ Misses        870      869       -1     
+ Partials      194      193       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


// getInitFuncDecl searches for the init function declaration in the given *ast.File.
// It returns the *ast.FuncDecl representing the init function if found, or nil otherwise.
func getInitFuncDecl(file *ast.File) *ast.FuncDecl {
Copy link

Choose a reason for hiding this comment

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

This only returns the first init function. I believe there can be an arbitrary number of them.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you. I have made the changes you suggested and added additional tests.

@SongShawn
Copy link

package sxn01

var (
	b *int
)

func init() {
	init_b_val()
}

func init_b_val() {
	b = new(int)
}

func use_global_var() {
	*b = 20
}

The above code triggers a false positive
image

@k4n4ry
Copy link
Author

k4n4ry commented Jun 21, 2024

@SongShawn
Thanks for the review. I've updated the function to retrieve not only the init functions but also all functions called directly or indirectly from them. dce532a

@k4n4ry
Copy link
Author

k4n4ry commented Aug 11, 2024

@sonalmahajan15
Hello, could you please rerun the CI and review this code? Thank you.

@yuxincs
Copy link
Contributor

yuxincs commented Aug 21, 2024

I'm updating your branch since we recently merged CI fixes, which are required for the CI to pass on forked repositories. I'll review the code this week :)

@github-actions
Copy link

github-actions bot commented Aug 21, 2024

Golden Test

Warning

❌ NilAway errors reported on stdlib are different 📉.

2567 errors on base branch (main, 99d94ca)
2395 errors on test branch (e5c022c)

Diffs
+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/tar/writer.go:543:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- tar/writer.go:304:27: unassigned variable `spb` passed as arg `p` to `Write()`
+ 	- tar/writer.go:540:26: passed as parameter `b` to `regFileWriter.Write()` (implementing `Writer.Write()`)
+ 	- tar/writer.go:543:7: function parameter `b` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/tar/writer.go:583:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- tar/writer.go:304:27: unassigned variable `spb` passed as arg `p` to `Write()`
+ 	- tar/writer.go:580:29: passed as parameter `b` to `sparseFileWriter.Write()` (implementing `Writer.Write()`)
+ 	- tar/writer.go:583:7: function parameter `b` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/archive/zip/zip_test.go:431:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ 	- flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ 	- flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ 	- flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ 	- zip/zip_test.go:421:24: passed as parameter `p` to `suffixSaver.Write()` (implementing `Writer.Write()`)
+ 	- zip/zip_test.go:431:7: function parameter `p` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- decodemeta/decodefile.go:175:11: result 0 of `GetPackagePayload()` lacking guarding; passed as arg `p` to `Write()` via the assignment(s):
+ 		- `r.GetPackagePayload(...)` to `pp` at decodemeta/decodefile.go:172:2
+ 	- io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ 	- io/multi.go:85:20: function parameter `p` passed as arg `p` to `Write()`
+ 	- testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ 	- testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ 	- bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ 	- bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- decodemeta/decodefile.go:175:11: result 0 of `GetPackagePayload()` lacking guarding; passed as arg `p` to `Write()` via the assignment(s):
+ 		- `r.GetPackagePayload(...)` to `pp` at decodemeta/decodefile.go:172:2
+ 	- testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ 	- testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ 	- bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ 	- bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ 	- flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ 	- flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ 	- flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ 	- io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ 	- io/multi.go:85:20: function parameter `p` passed as arg `p` to `Write()`
+ 	- testing/testing.go:1127:24: passed as parameter `p` to `outputWriter.Write()` (implementing `Writer.Write()`)
+ 	- testing/testing.go:1141:28: function parameter `p` passed as arg `s` to `SplitAfter()`
+ 	- bytes/bytes.go:446:18: function parameter `s` passed as arg `s` to `genSplit()`
+ 	- bytes/bytes.go:404:7: function parameter `s` sliced into
+ /opt/hostedtoolcache/go/1.25.6/x64/src/bytes/bytes.go:404:7: Potential nil panic detected. Observed nil flow from source to dereference point: 
+ 	- flate/deflate.go:171:33: unassigned variable `window` passed as arg `input` to `writeBlock()`
+ 	- flate/huffman_bit_writer.go:478:16: function parameter `input` passed as arg `bytes` to `writeBytes()`
+ 	- flate/huffman_bit_writer.go:185:10: function parameter `bytes` passed as arg `b` to `write()`
+ 	- flate/huffman_bit_writer.go:136:28: function parameter `b` passed as arg `p` to `Write()`
+ 	- io/multi.go:83:23: passed as parameter `p` to `multiWriter.Write()` (implementing `Writer.Write()`)
+ 	- io/multi.go:85:20: function parameter `p` passed as arg

 ...(truncated)...

.go:7230:22: nilable value assigned into global variable `Tptr`
- 	- reflect/all_test.go:7315:31: global variable `Tptr` passed as arg `t` to `SliceOf()`
- 	- reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- reflect/all_test.go:7230:28: nilable value assigned into global variable `Tscalarptr`
- 	- reflect/all_test.go:7338:39: global variable `Tscalarptr` passed as arg `elem` to `ArrayOf()`
- 	- reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- 	- reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- reflect/all_test.go:7230:40: nilable value assigned into global variable `Tptrscalar`
- 	- reflect/all_test.go:7327:46: global variable `Tptrscalar` passed as arg `elem` to `ArrayOf()`
- 	- reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- 	- reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- reflect/all_test.go:7230:52: nilable value assigned into global variable `Tbigptrscalar`
- 	- reflect/all_test.go:7329:31: global variable `Tbigptrscalar` passed as arg `t` to `SliceOf()`
- 	- reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/reflect/type.go:2077:9: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- reflect/all_test.go:7230:5: nilable value assigned into global variable `Tscalar`
- 	- reflect/all_test.go:7344:41: global variable `Tscalar` passed as arg `elem` to `ArrayOf()`
- 	- reflect/type.go:2649:26: function parameter `elem` passed as arg `t` to `SliceOf()`
- 	- reflect/type.go:2077:9: function parameter `t` called `common()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/runtime/unsafepoint_test.go:43:90: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- os/proc.go:16:5: nilable value assigned into global variable `Args`
- 	- runtime/unsafepoint_test.go:43:90: global variable `Args` sliced into
- /opt/hostedtoolcache/go/1.25.6/x64/src/sync/example_pool_test.go:38:2: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- 	- os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- 	- os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- 	- sync/example_pool_test.go:43:6: global variable `Stdout` passed as arg `w` to `Log()`
- 	- sync/example_pool_test.go:38:2: function parameter `w` called `Write()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/syscall/exec_pdeathsig_test.go:40:52: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- os/proc.go:16:5: nilable value assigned into global variable `Args`
- 	- syscall/exec_pdeathsig_test.go:40:52: global variable `Args` sliced into
- 
- (Same nil source could also cause potential nil panic(s) at 5 other place(s): "syscall/exec_pdeathsig_test.go:42:22", "syscall/exec_unix_test.go:338:19", "syscall/exec_unix_test.go:339:15", "syscall/syscall_linux_test.go:197:52", and "syscall/syscall_linux_test.go:199:22".)
- /opt/hostedtoolcache/go/1.25.6/x64/src/text/template/funcs.go:642:2: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- 	- os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- 	- os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- 	- template/example_test.go:104:22: global variable `Stdout` passed as arg `w` to `HTMLEscape()`
- 	- template/escape.go:963:22: function parameter `w` passed as arg `w` to `HTMLEscape()`
- 	- template/funcs.go:642:2: function parameter `w` called `Write()`
- /opt/hostedtoolcache/go/1.25.6/x64/src/text/template/funcs.go:725:2: Potential nil panic detected. Observed nil flow from source to dereference point: 
- 	- os/file_unix.go:90:10: literal `nil` returned from `newFileFromNewFile()` in position 0
- 	- os/file.go:134:9: result 0 of `newFileFromNewFile()` returned from `NewFile()` in position 0
- 	- os/file.go:73:2: result 0 of `NewFile()` assigned into global variable `Stdout`
- 	- template/example_test.go:109:20: global variable `Stdout` passed as arg `w` to `JSEscape()`
- 	- template/escape.go:979:20: function parameter `w` passed as arg `w` to `JSEscape()`
- 	- template/funcs.go:725:2: function parameter `w` called `Write()`

@TvdW
Copy link
Contributor

TvdW commented Dec 23, 2025

Hi, any news on landing this? I've been running this locally and it's very useful 😄

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.

Implement special handling for init

7 participants