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

adapter: Convert system config to unsigned ints #14502

Merged
merged 2 commits into from
Aug 29, 2022

Conversation

jkosh44
Copy link
Contributor

@jkosh44 jkosh44 commented Aug 29, 2022

The system configuration variables were always strictly positive
numbers, but we were using signed integers to represent them. This
commit converts them to use unsigned integers.

Motivation

This PR refactors existing code.

Checklist

The system configuration variables were always strictly positive
numbers, but we were using signed integers to represent them. This
commit converts them to use unsigned integers.
Comment on lines 677 to 681
if current_amount > usize::cast_from(u32::MAX)
|| (new_instances > 0
&& u32::try_from(current_amount).expect("guaranteed to fit") + new_instances
> limit)
{
Copy link
Member

Choose a reason for hiding this comment

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

Not that it really matters, but you might be able to avoid the expect with something like this?

let exceeds_limit = match u32::try_from(current_amount) {
    Ok(current_amount) => new_instances > 0 && current_amount + new_instances > limit,
    Err(_) => true,
}

Also, is this robust against overflows? It looks to me like if current_amount is u32::MAX and limit is u32::MAX the code (both yours and mine) will wrap around and erroneously allow the creation of the resource. Unlikely to matter in practice, because we likely won't have limits that large, but now that I've seen it I can't unsee it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're right. I should be using checked_add here. I'll fix that.

@jkosh44 jkosh44 enabled auto-merge (squash) August 29, 2022 22:31
@jkosh44 jkosh44 merged commit c20ab22 into MaterializeInc:main Aug 29, 2022
@jkosh44 jkosh44 deleted the unsigned-vars branch August 29, 2022 22:51
umanwizard pushed a commit to umanwizard/materialize-1 that referenced this pull request Aug 30, 2022
The system configuration variables were always strictly positive
numbers, but we were using signed integers to represent them. This
commit converts them to use unsigned integers.
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