From 4cda75df11da12ef01eeffb5a62dfefe92ad2569 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 15 Aug 2022 02:53:30 +0100 Subject: [PATCH] Added a limit to string and identifier lengths --- scripts/small.toy | 3 +-- source/parser.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/scripts/small.toy b/scripts/small.toy index 139597f..1c8a0e7 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,2 +1 @@ - - +; \ No newline at end of file diff --git a/source/parser.c b/source/parser.c index 26e6a7b..7e8ef66 100644 --- a/source/parser.c +++ b/source/parser.c @@ -6,6 +6,8 @@ #include "literal.h" #include "opcodes.h" +#include "console_colors.h" + #include //utility functions @@ -235,9 +237,18 @@ static Opcode compound(Parser* parser, Node** nodeHandle, bool canBeAssigned) { static Opcode string(Parser* parser, Node** nodeHandle, bool canBeAssigned) { //handle strings switch(parser->previous.type) { - case TOKEN_LITERAL_STRING: - emitNodeLiteral(nodeHandle, TO_STRING_LITERAL(copyString(parser->previous.lexeme, parser->previous.length))); + case TOKEN_LITERAL_STRING: { + int length = parser->previous.length; + + //for safety + if (length > 4096) { + length = 4096; + error(parser, parser->previous, "Strings can only be a maximum of 4096 characters long"); + } + + emitNodeLiteral(nodeHandle, TO_STRING_LITERAL(copyString(parser->previous.lexeme, length))); return OP_EOF; + } //TODO: interpolated strings @@ -396,7 +407,16 @@ static Opcode atomic(Parser* parser, Node** nodeHandle, bool canBeAssigned) { static Opcode identifier(Parser* parser, Node** nodeHandle, bool canBeAssigned) { //make a copy of the string Token identifierToken = parser->previous; - char* cpy = copyString(identifierToken.lexeme, identifierToken.length); + + int length = identifierToken.length; + + //for safety + if (length > 256) { + length = 256; + error(parser, parser->previous, "Identifiers can only be a maximum of 256 characters long"); + } + + char* cpy = copyString(identifierToken.lexeme, length); Literal identifier = _toIdentifierLiteral(cpy, strlen(cpy)); //BUGFIX: use this instead of the macro emitNodeLiteral(nodeHandle, identifier); @@ -732,6 +752,13 @@ static void assertStmt(Parser* parser, Node** nodeHandle) { //precedence functions static void expressionStmt(Parser* parser, Node** nodeHandle) { + //BUGFIX: check for empty statements + if (match(parser, TOKEN_SEMICOLON)) { + (*nodeHandle)->type = NODE_LITERAL; + (*nodeHandle)->atomic.literal = TO_NULL_LITERAL; + return; + } + //BUGFIX: statements assume the node exists, expressions assume it doens't Node* ptr = NULL; expression(parser, &ptr);