mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Null pointer exceptions in parser unary function
This commit is contained in:
@@ -419,7 +419,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
||||
parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal
|
||||
|
||||
//optimisation: check for negative literals
|
||||
if (tmpNode->type == AST_NODE_LITERAL && (IS_INTEGER(tmpNode->atomic.literal) || IS_FLOAT(tmpNode->atomic.literal))) {
|
||||
if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && (IS_INTEGER(tmpNode->atomic.literal) || IS_FLOAT(tmpNode->atomic.literal))) {
|
||||
//negate directly, if int or float
|
||||
Literal lit = tmpNode->atomic.literal;
|
||||
|
||||
@@ -438,7 +438,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
||||
}
|
||||
|
||||
//check for negated boolean errors
|
||||
if (tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
||||
if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
||||
error(parser, parser->previous, "Negative booleans are not allowed");
|
||||
return OP_EOF;
|
||||
}
|
||||
@@ -451,8 +451,8 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
||||
//temp handle to potentially negate values
|
||||
parsePrecedence(parser, &tmpNode, PREC_CALL); //can be a literal, grouping, fn call, etc.
|
||||
|
||||
//check for inverted booleans
|
||||
if (tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
||||
//optimisation: check for inverted booleans
|
||||
if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
||||
//negate directly, if boolean
|
||||
Literal lit = tmpNode->atomic.literal;
|
||||
|
||||
@@ -694,6 +694,13 @@ static Opcode fnCall(Parser* parser, ASTNode** nodeHandle) {
|
||||
|
||||
ASTNode* tmpNode = NULL;
|
||||
parsePrecedence(parser, &tmpNode, PREC_TERNARY);
|
||||
|
||||
//BUGFIX
|
||||
if (!tmpNode) {
|
||||
error(parser, parser->previous, "[internal] No token found in fnCall");
|
||||
return OP_EOF;
|
||||
}
|
||||
|
||||
arguments->fnCollection.nodes[arguments->fnCollection.count++] = *tmpNode;
|
||||
FREE(ASTNode, tmpNode); //simply free the tmpNode, so you don't free the children
|
||||
} while(match(parser, TOKEN_COMMA));
|
||||
@@ -1303,6 +1310,12 @@ static void returnStmt(Parser* parser, ASTNode** nodeHandle) {
|
||||
ASTNode* node = NULL;
|
||||
parsePrecedence(parser, &node, PREC_TERNARY);
|
||||
|
||||
//BUGFIX
|
||||
if (!node) {
|
||||
error(parser, parser->previous, "[internal] No token found in return");
|
||||
return;
|
||||
}
|
||||
|
||||
returnValues->fnCollection.nodes[returnValues->fnCollection.count++] = *node;
|
||||
FREE(ASTNode, node); //free manually
|
||||
} while(match(parser, TOKEN_COMMA));
|
||||
|
||||
1
test/scripts/mustfail/unary-inverted-nothing.toy
Normal file
1
test/scripts/mustfail/unary-inverted-nothing.toy
Normal file
@@ -0,0 +1 @@
|
||||
print !
|
||||
1
test/scripts/mustfail/unary-negative-nothing.toy
Normal file
1
test/scripts/mustfail/unary-negative-nothing.toy
Normal file
@@ -0,0 +1 @@
|
||||
print -
|
||||
@@ -69,7 +69,7 @@ unsigned char* compileString(char* source, size_t* size) {
|
||||
while(node != NULL) {
|
||||
//pack up and leave
|
||||
if (node->type == AST_NODE_ERROR) {
|
||||
printf(ERROR "error node detected\n" RESET);
|
||||
errorsTriggered++;
|
||||
freeASTNode(node);
|
||||
freeCompiler(&compiler);
|
||||
freeParser(&parser);
|
||||
@@ -131,6 +131,8 @@ int main() {
|
||||
"declare-types-dictionary-key.toy",
|
||||
"declare-types-dictionary-value.toy",
|
||||
"index-arrays-non-integer.toy",
|
||||
"unary-inverted-nothing.toy",
|
||||
"unary-negative-nothing.toy",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user