Skip to content

Commit 0310c9d

Browse files
committed
Have encoding_size take self.is_packed() into account
1 parent f38aed9 commit 0310c9d

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/codegen/encoding/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ use self::buffer_validator::BufferValidator;
3232

3333
/// Insert encoding instructions into the `cfg` for any `Expression` in `args`.
3434
/// Returns a pointer to the encoded data and the size as a 32bit integer.
35+
//
36+
// smoelius: If you see a message like the following, it may mean your trying to write into an
37+
// improperly sized vector:
38+
//
39+
// ```
40+
// WARN [...] program failure err="execution reverted" msg="call failed
41+
//
42+
// Caused by:
43+
// 0: hard user error
44+
// 1: RuntimeError: out of bounds memory access
45+
// ...
46+
// ```
3547
pub(super) fn abi_encode(
3648
loc: &Loc,
3749
args: Vec<Expression>,
@@ -327,7 +339,7 @@ pub(crate) trait AbiEncoding {
327339
cfg: &mut ControlFlowGraph,
328340
width: u16,
329341
) -> Expression {
330-
let encoding_size = encoding_size(ns, width);
342+
let encoding_size = self.encoding_size(ns, width);
331343
let expr = if encoding_size != width {
332344
if expr.ty().is_signed_int(ns) {
333345
Expression::SignExt {
@@ -742,7 +754,7 @@ pub(crate) trait AbiEncoding {
742754
) -> (Expression, Expression) {
743755
match ty {
744756
Type::Uint(width) | Type::Int(width) => {
745-
let encoding_size = encoding_size(ns, *width);
757+
let encoding_size = self.encoding_size(ns, *width);
746758

747759
let size = Expression::NumberLiteral {
748760
loc: Codegen,
@@ -1417,7 +1429,7 @@ pub(crate) trait AbiEncoding {
14171429
Type::Uint(n) | Type::Int(n) => Expression::NumberLiteral {
14181430
loc: Codegen,
14191431
ty: Uint(32),
1420-
value: BigInt::from(encoding_size(ns, *n) / 8),
1432+
value: BigInt::from(self.encoding_size(ns, *n) / 8),
14211433
},
14221434
Type::Enum(_) | Type::Contract(_) | Type::Bool | Type::Address(_) | Type::Bytes(_) => {
14231435
Expression::NumberLiteral {
@@ -1780,13 +1792,13 @@ pub(crate) trait AbiEncoding {
17801792
fn const_encode(&self, _args: &[Expression]) -> Option<Vec<u8>> {
17811793
None
17821794
}
1783-
}
17841795

1785-
fn encoding_size(ns: &Namespace, n: u16) -> u16 {
1786-
if ns.target == Target::Stylus {
1787-
256
1788-
} else {
1789-
n.next_power_of_two()
1796+
fn encoding_size(&self, ns: &Namespace, n: u16) -> u16 {
1797+
if !self.is_packed() && ns.target == Target::Stylus {
1798+
256
1799+
} else {
1800+
n.next_power_of_two()
1801+
}
17901802
}
17911803
}
17921804

0 commit comments

Comments
 (0)