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

gocritic->filepathJoin - Detects problems in filepath.Join() function calls. Should we enable it? #13193

Closed
zak-pawel opened this issue May 1, 2023 · 4 comments · Fixed by #13758
Assignees
Labels

Comments

@zak-pawel
Copy link
Collaborator

Description

This issue starts a discussion about enabling:

  • linter: gocritic - Highly extensible Go source code linter providing checks currently missing from other linters.
  • group: diagnostic - Diagnostics try to find programming errors in the code. They also detect code that may be correct, but looks suspicious.
  • checker: filepathJoin - Detects problems in filepath.Join() function calls.

Example

Before:

filepath.Join("dir/", filename)

After:

filepath.Join("dir", filename)

Expected output

Decision about enabling or not enabling this checker.

Findings

For this checker, the following findings were found in the current codebase:

config/config_test.go:1078:49                                gocritic  filepathJoin: "./testdata/processor_order" contains a path separator
internal/globpath/globpath_test.go:45:37                     gocritic  filepathJoin: "log\\[!*" contains a path separator
internal/globpath/globpath_test.go:47:37                     gocritic  filepathJoin: "log\\[^*" contains a path separator
plugins/inputs/diskio/diskio_linux.go:118:54                 gocritic  filepathJoin: "/dev" contains a path separator
plugins/inputs/file/file_test.go:30:37                       gocritic  filepathJoin: "dev/testfiles/**.log" contains a path separator
plugins/inputs/file/file_test.go:45:39                       gocritic  filepathJoin: "dev/testfiles/json_a.log" contains a path separator
plugins/inputs/file/file_test.go:70:37                       gocritic  filepathJoin: "dev/testfiles/json_a.log" contains a path separator
plugins/inputs/file/file_test.go:89:37                       gocritic  filepathJoin: "dev/testfiles/grok_a.log" contains a path separator
plugins/inputs/powerdns_recursor/powerdns_recursor.go:47:31  gocritic  filepathJoin: "/" contains a path separator
plugins/inputs/prometheus/kubernetes.go:66:42                gocritic  filepathJoin: ".kube/config" contains a path separator
plugins/inputs/system/ps.go:175:27                           gocritic  filepathJoin: "/" contains a path separator
testutil/socket.go:22:25                                     gocritic  filepathJoin: "/tmp" contains a path separator

Additional configuration

For this checker, no additional configuration can be provided.

@zak-pawel
Copy link
Collaborator Author

I would review current findings and enable this checker.

@Hipska
Copy link
Contributor

Hipska commented May 2, 2023

The findings seem correct uses to me?

@powersj
Copy link
Contributor

powersj commented May 5, 2023

-1 the findings all seem to line up with valid use cases

@srebhan
Copy link
Member

srebhan commented May 9, 2023

I think that

  • testutil/socket.go:22:25 should use os.MkdirTemp
  • plugins/inputs/system/ps.go:175:27 should maybe use os.PathSeparator as it might fail on Windows otherwise
  • plugins/inputs/prometheus/kubernetes.go:66:42 is a clear true-positive
  • plugins/inputs/powerdns_recursor/powerdns_recursor.go:47:31 should maybe just have a fixed /var/run string
  • plugins/inputs/diskio/diskio_linux.go:118:54 can just do string concatenation as it's only built for Linux

Also for the test-findings, it might not hurt to construct the paths using filepath.Join instead of doing this. I even don't know why this works on Windows machines...

With all this being said, I think there is a point in enabling this linter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants