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

Explicitly validate the len of params to ensure we don't get index ou… #126

Merged
merged 3 commits into from
Jun 12, 2024

Conversation

AndrewRPorter
Copy link
Contributor

@AndrewRPorter AndrewRPorter commented Jun 6, 2024

Please make sure to open your PR against the develop branch.

v4 commits do not exist in develop branch, so PR is created against main.

Pull Request Changes

  • Explicitly validate the len of params to ensure we don't get index out of bounds errors. This fixes code scanning alerts like CodeQL.

Closes

#125

Copy link
Owner

@petergtz petergtz left a comment

Choose a reason for hiding this comment

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

I think this looks good. Can you change the PR from draft to "proper" PR? I hope this will trigger the CI.

@petergtz petergtz marked this pull request as ready for review June 7, 2024 11:59
@petergtz
Copy link
Owner

petergtz commented Jun 7, 2024

Ah, never mind my last comment, I was also able to remove the draft status.

@petergtz
Copy link
Owner

petergtz commented Jun 7, 2024

Okay, tests show this doesn't work for variadic arguments. I recommend to stick with changing from comparison > 0 to != 0, assuming it will satisfy CodeQL. This will not change the logic and therefore not break the tests.

@AndrewRPorter
Copy link
Contributor Author

AndrewRPorter commented Jun 10, 2024

Sadly it doesn't look like changing the check to != 0 fixes the CodeQL warnings.

Instead, I just added a check for non variadic arguments.

This change generates verification methods like:

func (c *MockDisplay_ChanParams_OngoingVerification) GetAllCapturedArguments() (_param0 []<-chan string, _param1 []chan<- error) {
	_params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
	if len(_params) > 0 {
		if 0 < len(_params) {
			_param0 = make([]<-chan string, len(c.methodInvocations))
			for u, param := range _params[0] {
				_param0[u] = param.(<-chan string)
			}
		}
		if 1 < len(_params) {
			_param1 = make([]chan<- error, len(c.methodInvocations))
			for u, param := range _params[1] {
				_param1[u] = param.(chan<- error)
			}
		}
	}
	return
}

Copy link
Owner

@petergtz petergtz left a comment

Choose a reason for hiding this comment

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

Okay, looks good and tests are green. Just one minor nit :-), then it's ready for merge.

mockgen/mockgen.go Outdated Show resolved Hide resolved
@petergtz petergtz merged commit 692162c into petergtz:main Jun 12, 2024
3 checks passed
@petergtz
Copy link
Owner

Merged and cut a new release 4.1.0 with your changes. Thank you very much for your contribution @AndrewRPorter.

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.

None yet

2 participants