Null pointer exceptions in parser unary function

This commit is contained in:
2023-01-15 18:15:59 +00:00
parent 51740e2b9e
commit aeecfabbbc
4 changed files with 22 additions and 5 deletions

View File

@@ -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));

View File

@@ -0,0 +1 @@
print !

View File

@@ -0,0 +1 @@
print -

View File

@@ -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
};