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

inclusion: RoundUpByMultipleOf and all other modulo denominator must be checked for zero before usage #45

Open
odeke-em opened this issue Mar 1, 2024 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@odeke-em
Copy link
Contributor

odeke-em commented Mar 1, 2024

If we examine

func RoundUpByMultipleOf(cursor, v int) int {
if cursor%v == 0 {
return cursor
}
we see that an outside modulus is being used. Let's bullet proof by firstly checking if the denominator is zero and about a runtime panic of divide by zero

@rootulp
Copy link
Collaborator

rootulp commented Mar 4, 2024

Agreed. I think you're proposing something like:

// RoundUpByMultipleOf rounds cursor up to the next multiple of v. If cursor is divisible
// by v, then it returns cursor.
func RoundUpByMultipleOf(cursor, v int) (int, error) {
	if v == 0 {
		return 0, fmt.Errorf("v cannot be 0")
	}
	if cursor%v == 0 {
		return cursor, nil
	}
	return ((cursor / v) + 1) * v, nil
}

which is API breaking but IMO seems reasonable.

@rootulp rootulp added enhancement New feature or request good first issue Good for newcomers and removed needs:triage external labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants