Skip to content

Commit ecce5db

Browse files
committed
fix: prevent crashing via too large entropy
1 parent e7df6b3 commit ecce5db

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

templates/shortcodes/palette_calc.html

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<input required
1818
type=number
1919
min=1 max=65536
20-
step=1 value=16
20+
step=1 value=1024
2121
name="palette-entropy"
2222
style="text-align:end;width:9em"
2323
/><br/>
@@ -38,6 +38,14 @@
3838
</select><br/>
3939
<small>Backing Storage Type</small>
4040
</label>
41+
<label>
42+
<input required
43+
type="checkbox"
44+
checked
45+
name="recurring-entropy-skip"
46+
/>
47+
<span>Skip Recurring</span>
48+
</label>
4149
</div>
4250
<blockquote style="display:flex;justify-content:center;align-items:center;padding:0.5rem">
4351
Palette Memory = Palette Entry Size * Entropy<br/>
@@ -80,24 +88,27 @@
8088
}
8189

8290
var calculator = document.getElementById('{{id}}');
83-
var calc_input_es = calculator.querySelector('input[name=palette-entry-size]');
84-
var calc_input_me = calculator.querySelector('input[name=palette-entropy]');
85-
var calc_input_cs = calculator.querySelector('select[name=cell-size]');
86-
var calc_thead = calculator.querySelector('thead');
87-
var calc_tbody = calculator.querySelector('tbody');
91+
var calc_input_es = calculator.querySelector('input[name=palette-entry-size]');
92+
var calc_input_me = calculator.querySelector('input[name=palette-entropy]');
93+
var calc_input_cs = calculator.querySelector('select[name=cell-size]');
94+
var calc_input_rs = calculator.querySelector('input[name=recurring-entropy-skip]');
95+
var calc_thead = calculator.querySelector('thead');
96+
var calc_tbody = calculator.querySelector('tbody');
8897

8998
function update() {
90-
var EntrySize = null, MaxEntropy = null, CellSize = 64;
91-
try {
92-
EntrySize = Number.parseInt(calc_input_es.value || "1");
93-
MaxEntropy = Number.parseInt(calc_input_me.value || "1");
99+
var EntrySize = null, MaxEntropy = null, CellSize = 64, SkipRecurring = true;
100+
try {
101+
EntrySize = Math.abs(Number.parseInt(calc_input_es.value || "1"));
102+
MaxEntropy = Math.abs(Number.parseInt(calc_input_me.value || "1"));
94103
CellSize = calc_input_cs.value;
95-
if (!EntrySize) throw "invalid";
96-
if (!MaxEntropy) throw "invalid";
97-
} catch(e) {
98-
calc_tbody.innerHTML = '';
99-
return;
100-
}
104+
if(MaxEntropy >= 512) calc_input_rs.checked = true;
105+
SkipRecurring = calc_input_rs.checked;
106+
if (!EntrySize) throw "invalid";
107+
if (!MaxEntropy) throw "invalid";
108+
} catch(e) {
109+
calc_tbody.innerHTML = '';
110+
return;
111+
}
101112

102113
var sizes = [4, 8, 16, 32, 64];
103114
var scols = sizes.map((size) => {
@@ -137,9 +148,30 @@
137148
}
138149
calc_thead.innerHTML = thead + "</tr>";
139150

151+
var prev_entropy_bits = -1;
152+
var prev_skipped_count = 0;
140153
var tbody = "";
141154
for(var entropy = 1; entropy <= MaxEntropy; entropy++) {
142155
var entropy_bits = Math.ceil(Math.log2(entropy));
156+
157+
if(SkipRecurring) {
158+
if(prev_entropy_bits == entropy_bits) {
159+
prev_skipped_count++;
160+
continue;
161+
} else if(prev_skipped_count > 0) {
162+
tbody += `<tr>`;
163+
tbody += `<td colspan=${5} style="text-align:center;font-size:0.65em">`;
164+
tbody += `<span style="opacity:0.5">Skipped ${prev_skipped_count} row/s.</span>`;
165+
tbody += `</td>`;
166+
tbody += `<td colspan=${scols.length} style="border-left:1px solid lightgray">`;
167+
tbody += `</td>`;
168+
tbody += `</tr>`;
169+
}
170+
}
171+
172+
prev_entropy_bits = entropy_bits;
173+
prev_skipped_count = 0;
174+
143175
var entries = EntrySize * entropy;
144176
var voxels_per_cell = Math.floor(entropy_bits != 0 ? CellSize / entropy_bits : 0);
145177
var wasted_per_cell = entropy_bits != 0 ? CellSize % entropy_bits : 0;
@@ -190,6 +222,7 @@
190222
calc_input_es.oninput = update;
191223
calc_input_me.oninput = update;
192224
calc_input_cs.onchange = update;
225+
calc_input_rs.onchange = update;
193226
update();
194227
calculator.style = '';
195228
}) ();

0 commit comments

Comments
 (0)