Skip to content

Commit 412dd78

Browse files
committed
Refrain from setting array indices outside the array length
The code `array[i] = foo` where `i >= array.length` does, for obvious reasons, not work inside unchecked contexts. Code like this was the cause of crashes when compiling with `--uncheckedBehavior always`. In my opinion, this practice is sloppy, although my workarounds can be considered equally sloppy.
1 parent 7d60311 commit 412dd78

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/builtins.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10329,17 +10329,17 @@ export function compileVisitMembers(compiler: Compiler): void {
1032910329
let instanceId = _keys[i];
1033010330
assert(instanceId == nextId++);
1033110331
let instance = assert(managedClasses.get(instanceId));
10332-
names[i] = instance.internalName;
10332+
names.push(instance.internalName);
1033310333
if (instance.isPointerfree) {
10334-
cases[i] = module.return();
10334+
cases.push(module.return());
1033510335
} else {
10336-
cases[i] = module.block(null, [
10336+
cases.push(module.block(null, [
1033710337
module.call(`${instance.internalName}~visit`, [
1033810338
module.local_get(0, sizeTypeRef), // this
1033910339
module.local_get(1, TypeRef.I32) // cookie
1034010340
], TypeRef.None),
1034110341
module.return()
10342-
], TypeRef.None);
10342+
], TypeRef.None));
1034310343
ensureVisitMembersOf(compiler, instance);
1034410344
}
1034510345
}

src/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6373,7 +6373,7 @@ export class Compiler extends DiagnosticEmitter {
63736373
}
63746374
let numOptional = assert(maxOperands - minOperands);
63756375

6376-
let forwardedOperands = new Array<ExpressionRef>(minOperands);
6376+
let forwardedOperands = new Array<ExpressionRef>(maxOperands);
63776377
let operandIndex = 0;
63786378
let stmts = new Array<ExpressionRef>();
63796379

@@ -6597,7 +6597,7 @@ export class Compiler extends DiagnosticEmitter {
65976597
let body: ExpressionRef;
65986598
let instanceClass = instance.getBoundClassOrInterface();
65996599
if (!instance.is(CommonFlags.Abstract) && !(instanceClass && instanceClass.kind == ElementKind.Interface)) {
6600-
let paramExprs = new Array<ExpressionRef>(numParameters);
6600+
let paramExprs = new Array<ExpressionRef>(numParameters + 1);
66016601
paramExprs[0] = module.local_get(0, sizeTypeRef); // this
66026602
for (let i = 0, k = parameterTypes.length; i < k; ++i) {
66036603
paramExprs[1 + i] = module.local_get(1 + i, parameterTypes[i].toRef());

src/passes/shadowstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ export class ShadowStackPass extends Pass {
467467
let results = _BinaryenFunctionGetResults(funcRef);
468468
let body = assert(_BinaryenFunctionGetBody(funcRef));
469469
let numVars = _BinaryenFunctionGetNumVars(funcRef);
470-
let vars = new Array<TypeRef>();
470+
let vars = new Array<TypeRef>(numVars);
471471
for (let i: Index = 0; i < numVars; ++i) {
472472
vars[i] = _BinaryenFunctionGetVar(funcRef, i);
473473
}

0 commit comments

Comments
 (0)