Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

fix panic and filter on 'database/sql' package #7

Merged
merged 2 commits into from
May 10, 2022
Merged

Conversation

ldez
Copy link
Contributor

@ldez ldez commented May 10, 2022

Fix #3

@@ -45,28 +57,35 @@ func run(pass *analysis.Pass) (interface{}, error) {
switch arg := n.Args[i].(type) {
Copy link

@favonia favonia May 10, 2022

Choose a reason for hiding this comment

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

Maybe also adding the following checking before var s string?

			if i >= len(n.Args) {
				return
			}

I know this is impossible for the current database/sql (if the program type checks), but I feel we should still check.

Copy link
Owner

Choose a reason for hiding this comment

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

In the golang, the definition of the variable is different between var s string and s := "AAAA"
this code is only checking s := "AAAA" case.

Copy link

@favonia favonia May 10, 2022

Choose a reason for hiding this comment

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

@lufeee No I was not talking about the variable s. Please keep that line as it is. I was proposing the following code, in addition to what @ldez what already did:

			// ...

			if i >= len(n.Args) {
				return
			}

			var s string
			switch arg := n.Args[i].(type) {

			// ...

The reason is that n.Args[i] will panic if i >= len(n.Args[i]). And the reason I propose putting the check before the line var s string is because the variable s is meaningful only later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the check is at the beginning of the function.

Copy link

Choose a reason for hiding this comment

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

Ah, sorry I somehow missed it.

Copy link

@favonia favonia May 10, 2022

Choose a reason for hiding this comment

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

CORRECTION: No, the check is still wrong...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: my login is ldez not idez

I will take a look

Copy link

@favonia favonia May 10, 2022

Choose a reason for hiding this comment

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

@ldez I created the PR #8 with an example.

PS: Sorry I fixed the tagging.

@Hades32
Copy link

Hades32 commented May 10, 2022

@lufeee

@1uf3
Copy link
Owner

1uf3 commented May 10, 2022

@ldez Thanks for fixing this bug.
LGTM

@1uf3 1uf3 merged commit c481895 into 1uf3:main May 10, 2022
@ldez ldez deleted the fix/naive-fix branch May 10, 2022 13:46
@favonia
Copy link

favonia commented May 10, 2022

The code does not fix all the problems because i could be 1 after checking len(n.Args) < 1. However, it is difficult to unmerge a PR, so let me start another PR.

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

Successfully merging this pull request may close these issues.

panic: runtime error: index out of range [0] with length 0
4 participants