Skip to content

Commit 8100624

Browse files
committed
add validation and use ArenaAllocator
1 parent 4d61d45 commit 8100624

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

bindings/napi/proposer_index.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const ComputeIndexUtils = state_transition.committee_indices.ComputeIndexUtils(u
77
const ByteCount = state_transition.committee_indices.ByteCount;
88
const preset = state_transition.preset;
99

10-
const allocator = std.heap.page_allocator;
11-
1210
pub fn ProposerIndex_computeProposerIndex(env: napi.Env, cb: napi.CallbackInfo(4)) !napi.Value {
1311
// arg 0: fork (string)
1412
var fork_name_buf: [16]u8 = undefined;
@@ -35,12 +33,23 @@ pub fn ProposerIndex_computeProposerIndex(env: napi.Env, cb: napi.CallbackInfo(4
3533
return error.InvalidSeedLength;
3634
}
3735

36+
// Validate indices don't exceed effective_balance_increments bounds
37+
for (indices) |idx| {
38+
if (idx >= effective_balance_increments.len) {
39+
return error.IndexOutOfBounds;
40+
}
41+
}
42+
3843
// Derive fork-dependent parameters
3944
const rand_byte_count: ByteCount = if (fork.gte(.electra)) .Two else .One;
4045
const max_effective_balance: u64 = if (fork.gte(.electra)) preset.MAX_EFFECTIVE_BALANCE_ELECTRA else preset.MAX_EFFECTIVE_BALANCE;
4146

47+
// Use arena allocator scoped to this function call
48+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
49+
defer arena.deinit();
50+
4251
const proposer_index = try ComputeIndexUtils.computeProposerIndex(
43-
allocator,
52+
arena.allocator(),
4453
seed_info.data,
4554
indices,
4655
effective_balance_increments,

0 commit comments

Comments
 (0)