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

Integer literal overflow/underflow doesn't always error #5372

Closed
asterite opened this issue Jul 1, 2024 · 0 comments · Fixed by #5375
Closed

Integer literal overflow/underflow doesn't always error #5372

asterite opened this issue Jul 1, 2024 · 0 comments · Fixed by #5375
Assignees
Labels
bug Something isn't working

Comments

@asterite
Copy link
Collaborator

asterite commented Jul 1, 2024

Aim

The compiler should error when integer literals don't fit the target integer type. This is already done in some cases, but not in all of them and not always correctly. Some examples:

let x: u8 = -1; // Should error, minimum is 0

let x: i8 = 128; // Should error, maximum is 127
let x: i8 = -129; // Should error, minimum is -128

let x: i8 = 256; // Correctly errors, because it checks against 255
let x: u8 = 256; // Correctly errors, because it checks against 255

(and similarly for other integer types)

Expected Behavior

This program should fail to compile:

fn main() {
    let x: i8 = 128;
    println(x);
}

Bug

The program successfully compiles and executing it prints "-128".

To Reproduce

No response

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

Yes

Support Needs

No response

@asterite asterite added the bug Something isn't working label Jul 1, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 1, 2024
@asterite asterite self-assigned this Jul 1, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 3, 2024
…5375)

# Description

## Problem

Resolves #5372

## Summary

Correctly detects overflows/underflows for basic integer types. The sign
of a field element value is now also propagated to the ssa phase. But
previously, the field value was applied some casting before reaching the
ssa phase so checking that value like that was a bit strange. In this PR
that conversion is delayed until the ssa phase, and the value can be
checked prior to that conversion.

## Additional Context

None.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 3, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 5, 2024
…5416)

# Description

## Problem

Resolves #5419

## Summary

This is similar to #5372, except that here the compiler does an early
check with explicitly annotated types, and in the related issue the
check is done later on, once unknown types are resolved.

Also in this PR:
- a test was moved from `test_programs` to `noirc_frontend/src/tests.rs`
because the error produced here can be caught there
- the error message produced in both places is now exactly the same
(showing the possible range of values, which helps to know why you
exceeded the min/max)

## Additional Context

None

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant