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

Dividing variable by itself in loop_handler.go #2026

Open
1 task done
McLaynV opened this issue Nov 10, 2024 · 4 comments
Open
1 task done

Dividing variable by itself in loop_handler.go #2026

McLaynV opened this issue Nov 10, 2024 · 4 comments
Assignees

Comments

@McLaynV
Copy link
Contributor

McLaynV commented Nov 10, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Version of Corteza

2024.9.x branch

Current Behavior

In server/automation/automation/loop_handler.go there is:

if args.First*(args.Step/args.Step) >= args.Last*(args.Step/args.Step)

if args.First*(args.Step/args.Step) >= args.Last*(args.Step/args.Step) {

Expected Behavior

I believe it is not intended to compute args.Step/args.Step.
If it is intended, there should be a comment explaining what magic is going on.

Steps To Reproduce

No response

Environment and versions

No response

Anything else?

@darh

Copy link

Stale issue message

@McLaynV
Copy link
Contributor Author

McLaynV commented Jan 18, 2025

@darh or @tjerman could you have a look at it?

loopSequenceArgs.Step is of type int64. That gives us 2 possible scenarios:

  • if Step == 0, then Step/Step causes runtime panic. Returning an error would be better.
  • if Step != 0, then Step/Step is 1 and the x*(args.Step/args.Step) reduces to x*1 which is plain x.

So to keep the current functionality, just replace

	if args.First*(args.Step/args.Step) >= args.Last*(args.Step/args.Step) {
		return nil, fmt.Errorf("failed to initialize counter iterator with first step greater than last")
	}

with

	if args.Step == 0 {
		return nil, fmt.Errorf("failed to initialize counter iterator with zero step")
	}
	if args.First >= args.Last {
		return nil, fmt.Errorf("failed to initialize counter iterator with first step greater than last")
	}

I can imagine it was meant to be a sign check to allow negative steps. If so, it could be like this:

func signOf(n int64) int {
    if n > 0 {
        return 1
    } else if n < 0 {
        return -1
    }
    return 0
}

and replace

	if args.First*(args.Step/args.Step) >= args.Last*(args.Step/args.Step) {
		return nil, fmt.Errorf("failed to initialize counter iterator with first step greater than last")
	}

with

	if args.Step == 0 && args.First != args.Last {
		return nil, fmt.Errorf("failed to initialize counter iterator with zero step")
	}
	sign := signOf(args.Step)
	if args.First*sign > args.Last*sign {
		return nil, fmt.Errorf("failed to initialize counter iterator with first value greater than last")
	}

or if zero steps are not allowed, then replace the block with

	sign := signOf(args.Step)
	if args.First*sign >= args.Last*sign {
		return nil, fmt.Errorf("failed to initialize counter iterator with first value greater than last")
	}

@tjerman
Copy link
Member

tjerman commented Jan 31, 2025

It should be

        if step == 0 {error...}

        sign := signOf(args.Step)
	if args.First*sign >= args.Last*sign {
		return nil, fmt.Errorf("failed to initialize counter iterator with first value greater than last")
	}

If you want, do a pull request so I don't steal your work

@McLaynV
Copy link
Contributor Author

McLaynV commented Jan 31, 2025

No problem @tjerman I grant you permission to steal this piece of code from me :D

I don't have a setup to build go or to run the tests. So I would only feel bad that I can't check all the checkboxes in the PR template.

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

No branches or pull requests

2 participants