Fixed an obscure memory leak, potential issue with lib_runner on linux

This commit is contained in:
2023-02-04 15:30:46 +11:00
parent 2f1613e306
commit 57c16d2ede
3 changed files with 23 additions and 19 deletions

View File

@@ -224,19 +224,28 @@ static void consumeShort(Toy_Interpreter* interpreter, unsigned short bytes, uns
static bool execAssert(Toy_Interpreter* interpreter) { static bool execAssert(Toy_Interpreter* interpreter) {
Toy_Literal rhs = Toy_popLiteralArray(&interpreter->stack); Toy_Literal rhs = Toy_popLiteralArray(&interpreter->stack);
Toy_Literal lhs = Toy_popLiteralArray(&interpreter->stack); Toy_Literal lhs = Toy_popLiteralArray(&interpreter->stack);
Toy_parseIdentifierToValue(interpreter, &lhs);
Toy_Literal lhsIdn = lhs;
if (TOY_IS_IDENTIFIER(lhs) && Toy_parseIdentifierToValue(interpreter, &lhs)) {
Toy_freeLiteral(lhsIdn);
}
if (!TOY_IS_STRING(rhs)) { if (!TOY_IS_STRING(rhs)) {
interpreter->errorOutput("The assert keyword needs a string as the second argument, received: "); interpreter->errorOutput("The assert keyword needs a string as the second argument, received: ");
Toy_printLiteralCustom(rhs, interpreter->errorOutput); Toy_printLiteralCustom(rhs, interpreter->errorOutput);
interpreter->errorOutput("\n"); interpreter->errorOutput("\n");
Toy_freeLiteral(rhs);
Toy_freeLiteral(lhs);
return false; return false;
} }
if (TOY_IS_NULL(lhs) || !TOY_IS_TRUTHY(lhs)) { if (TOY_IS_NULL(lhs) || !TOY_IS_TRUTHY(lhs)) {
(*interpreter->assertOutput)(Toy_toCString(TOY_AS_STRING(rhs))); (*interpreter->assertOutput)(Toy_toCString(TOY_AS_STRING(rhs)));
Toy_freeLiteral(rhs);
interpreter->panic = true; interpreter->panic = true;
Toy_freeLiteral(rhs);
Toy_freeLiteral(lhs);
return false; return false;
} }
@@ -1086,16 +1095,14 @@ static bool execFalseJump(Toy_Interpreter* interpreter) {
//actually jump //actually jump
Toy_Literal lit = Toy_popLiteralArray(&interpreter->stack); Toy_Literal lit = Toy_popLiteralArray(&interpreter->stack);
bool freeLit = false; Toy_Literal litIdn = lit;
if (TOY_IS_IDENTIFIER(lit)) { if (TOY_IS_IDENTIFIER(lit) && Toy_parseIdentifierToValue(interpreter, &lit)) {
Toy_Literal idn = lit; Toy_freeLiteral(litIdn);
Toy_parseIdentifierToValue(interpreter, &lit);
Toy_freeLiteral(idn);
freeLit = true;
} }
if (TOY_IS_NULL(lit)) { if (TOY_IS_NULL(lit)) {
interpreter->errorOutput("Null detected in comparison\n"); interpreter->errorOutput("Null detected in comparison\n");
Toy_freeLiteral(lit);
return false; return false;
} }
@@ -1103,9 +1110,7 @@ static bool execFalseJump(Toy_Interpreter* interpreter) {
interpreter->count = target + interpreter->codeStart; interpreter->count = target + interpreter->codeStart;
} }
if (freeLit) { Toy_freeLiteral(lit);
Toy_freeLiteral(lit);
}
return true; return true;
} }
@@ -1995,7 +2000,6 @@ static void execInterpreter(Toy_Interpreter* interpreter) {
} }
break; break;
case TOY_OP_FN_RETURN: case TOY_OP_FN_RETURN:
if (!execFnReturn(interpreter)) { if (!execFnReturn(interpreter)) {
return; return;

View File

@@ -23,13 +23,13 @@
//test ambiguous syntax //test ambiguous syntax
{ {
var a = 1; var aa = 1;
var b = 2; var bbb = 2;
var c = 3; var cccc = 3;
var d = 4; var ddddd = 4;
var e = 5; var eeeeee = 5;
assert a ? b ? c : d : e == 3, "Ambiguous syntax failed"; assert aa ? bbb ? cccc : ddddd : eeeeee, "Ambiguous syntax failed";
} }