Skip to content

Conversation

@zilllaiss
Copy link
Contributor

simple fix since it only caused by wrong usage of operator and off-by-1 error.

}

var expiration time.Duration
if len(args) == 4 {
Copy link
Owner

@yeqown yeqown Jun 18, 2025

Choose a reason for hiding this comment

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

Please update here condition too, we assume the expiration is at the fourth postional argument, so there's not neccessary len(args) strictly equal to 4

Copy link
Contributor Author

Choose a reason for hiding this comment

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

by looking at the other example (e.g. handleIncr()), I'm assuming you meant something like this?

	if len(args) != 3 && len(args) != 4 {
		return fmt.Errorf("usage: set <key> <value> [expiration]")
	}

	var expiration time.Duration
	if len(args) == 4 {
		if e, err := strconv.ParseUint(args[3], 10, 32); err == nil {
			expiration = time.Duration(e) * time.Second
		}
	}

instead of removing the length check like this...

	if len(args) != 3 {
		return fmt.Errorf("usage: set <key> <value> [expiration]")
	}

	var expiration time.Duration
	if e, err := strconv.ParseUint(args[3], 10, 32); err == nil {
		expiration = time.Duration(e) * time.Second
	}

...since the latter will ignore extra arguments i.e. when len(args) > 4. Is that correct?

Copy link
Owner

Choose a reason for hiding this comment

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

len(args) == 4 to be len(args) >= 4·
keep it's safe to access the expiration index, and work expected event args has more than 4 args

@zilllaiss zilllaiss requested a review from yeqown June 19, 2025 02:24
@yeqown yeqown merged commit 0a39bef into yeqown:main Jun 19, 2025
2 checks passed
@yeqown
Copy link
Owner

yeqown commented Jun 19, 2025

Thanks for your contribution 🎉

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.

2 participants