Skip to content

Commit 9663eaf

Browse files
authored
RP2xxx: Fix overflow when PIO programs contain 32 instructions (#599)
1 parent 4d40fc9 commit 9663eaf

File tree

1 file changed

+8
-5
lines changed
  • port/raspberrypi/rp2xxx/src/hal/pio/assembler

1 file changed

+8
-5
lines changed

port/raspberrypi/rp2xxx/src/hal/pio/assembler/encoder.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,30 +486,33 @@ pub fn Encoder(comptime chip: Chip, comptime options: Options) type {
486486

487487
fn encode_instruction_body(self: *Self, program: *BoundedProgram, diags: *?Diagnostics) !void {
488488
// first scan through body for labels
489-
var instr_index: u5 = program.origin orelse 0;
489+
var instr_index: ?u5 = program.origin orelse 0;
490490
for (self.tokens[self.index..]) |token| {
491491
switch (token.data) {
492492
.label => |label| try program.labels.append(.{
493493
.name = label.name,
494494
.public = label.public,
495-
.index = instr_index,
495+
.index = instr_index.?,
496496
}),
497-
.instruction, .word => instr_index += 1,
497+
.instruction, .word => {
498+
const result, const ov = @addWithOverflow(instr_index.?, 1);
499+
instr_index = if (ov != 0) null else result;
500+
},
498501
.wrap_target => {
499502
if (program.wrap_target != null) {
500503
diags.* = Diagnostics.init(token.index, "wrap_target already set for this program", .{});
501504
return error.WrapTargetAlreadySet;
502505
}
503506

504-
program.wrap_target = instr_index;
507+
program.wrap_target = instr_index.?;
505508
},
506509
.wrap => {
507510
if (program.wrap != null) {
508511
diags.* = Diagnostics.init(token.index, "wrap already set for this program", .{});
509512
return error.WrapAlreadySet;
510513
}
511514

512-
program.wrap = instr_index - 1;
515+
program.wrap = if (instr_index) |idx| idx - 1 else 31;
513516
},
514517
.program => break,
515518
else => unreachable, // invalid

0 commit comments

Comments
 (0)