Skip to content

Commit

Permalink
Fixes crash on encode_buffer_append with wrong args. (#6365)
Browse files Browse the repository at this point in the history
## Description
When encode_buffer_append was called with a wrong number of args an
array OOB panic was thrown.

With this fix we throw a CompileError::IntrinsicIncorrectNumArgs in case
the intrinsic encode_buffer_append is called with more or less than the
expected 2 arguments.

Fixes #6337

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: João Matos <joao@tritao.eu>
  • Loading branch information
esdrubal and tritao authored Aug 7, 2024
1 parent a906afa commit cd0213a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ fn type_check_encode_append(
_type_arguments: &[TypeArgument],
span: Span,
) -> Result<(ty::TyIntrinsicFunctionKind, TypeId), ErrorEmitted> {
if arguments.len() != 2 {
return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs {
name: kind.to_string(),
expected: 2,
span,
}));
}

let type_engine = ctx.engines.te();
let engines = ctx.engines();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "encode_append_wrong_args"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "encode_append_wrong_args"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library;

pub struct Buffer {
buffer: u64
}

pub trait T {
fn ar(buffer: Buffer) -> Buffer;
}

impl T for str[10] {
fn ar(buffer: Buffer) -> Buffer {
Buffer {
buffer: __encode_buffer_append(buffer.buffer)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
category = "fail"

# check: $()warning
# check: $()buffer: __encode_buffer_append(buffer.buffer)

# check: $()buffer: __encode_buffer_append(buffer.buffer)
# nextln: $()Call to "encode_buffer_append" expects 2 arguments

0 comments on commit cd0213a

Please sign in to comment.