Skip to content

Commit 3e81a77

Browse files
authored
fix: Add input validation to BPMFactorBlock setter to prevent audio scheduler crashes (#5740)
1 parent fa74789 commit 3e81a77

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

js/blocks/MeterBlocks.js

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,24 @@ function setupMeterBlocks(activity) {
191191
setter(logo, value, turtle) {
192192
const tur = activity.turtles.ithTurtle(turtle);
193193

194+
// Validate and clamp BPM to safe range [30, 1000]
195+
let bpm = Number(value);
196+
if (isNaN(bpm)) {
197+
bpm = 60; // Default to 60 BPM
198+
activity.errorMsg(_("BPM must be a number, defaulting to 60."));
199+
} else if (bpm < 30) {
200+
bpm = 30;
201+
activity.errorMsg(_("BPM must be at least 30, clamping to 30."));
202+
} else if (bpm > 1000) {
203+
bpm = 1000;
204+
activity.errorMsg(_("BPM must be at most 1000, clamping to 1000."));
205+
}
206+
194207
const len = tur.singer.bpm.length;
195208
if (len > 0) {
196-
tur.singer.bpm[len - 1] = value;
209+
tur.singer.bpm[len - 1] = bpm;
197210
} else {
198-
tur.singer.bpm.push(value);
211+
tur.singer.bpm.push(bpm);
199212
}
200213
}
201214

@@ -931,25 +944,7 @@ function setupMeterBlocks(activity) {
931944
// Set palette, activity, piemenuValuesC1, and beginnerBlock for the block
932945
this.setPalette("meter", activity);
933946
this.piemenuValuesC1 = [
934-
42,
935-
46,
936-
50,
937-
54,
938-
58,
939-
63,
940-
69,
941-
76,
942-
84,
943-
90,
944-
96,
945-
104,
946-
112,
947-
120,
948-
132,
949-
144,
950-
160,
951-
176,
952-
192,
947+
42, 46, 50, 54, 58, 63, 69, 76, 84, 90, 96, 104, 112, 120, 132, 144, 160, 176, 192,
953948
208
954949
];
955950
this.beginnerBlock(true);
@@ -1077,25 +1072,7 @@ function setupMeterBlocks(activity) {
10771072
// Set palette, piemenuValuesC1, beginnerBlock, and activity for the block
10781073
this.setPalette("meter", activity);
10791074
this.piemenuValuesC1 = [
1080-
42,
1081-
46,
1082-
50,
1083-
54,
1084-
58,
1085-
63,
1086-
69,
1087-
76,
1088-
84,
1089-
90,
1090-
96,
1091-
104,
1092-
112,
1093-
120,
1094-
132,
1095-
144,
1096-
160,
1097-
176,
1098-
192,
1075+
42, 46, 50, 54, 58, 63, 69, 76, 84, 90, 96, 104, 112, 120, 132, 144, 160, 176, 192,
10991076
208
11001077
];
11011078
this.beginnerBlock(true);

0 commit comments

Comments
 (0)