From 534d80c35d19b11e07471ee0a358c9171648546e Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 6 May 2023 14:04:58 +0200 Subject: [PATCH] Reader: Prevent infinite loop during List and Vector creation --- README.org | 4 +--- src/functions.cpp | 1 - src/reader.cpp | 12 ++++++++++-- src/stepA_mal.cpp | 2 -- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/README.org b/README.org index c793f5b..2cfbbf0 100644 --- a/README.org +++ b/README.org @@ -3,12 +3,10 @@ #+LANGUAGE: en #+OPTIONS: toc:nil -This is my implementation of the [[https://github.com/kanaka/mal][Make A Lisp]] project, done in C++20. +This is an implementation of the [[https://github.com/kanaka/mal][Make A Lisp]] project, done in C++20. ** Usage -Change the ~#if 0~ macro at the top of the ~stepX.cpp~ to ~#if 1~. - *** Run the REPL #+BEGIN_SRC shell-script diff --git a/src/functions.cpp b/src/functions.cpp index 875ab98..6e7f209 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -168,7 +168,6 @@ ADD_FUNCTION( return makePtr((result) ? Constant::True : Constant::False); }); -// FIXME: (count {1}) infinite loop ADD_FUNCTION( "count", { diff --git a/src/reader.cpp b/src/reader.cpp index 037bb09..c21c466 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -146,7 +146,11 @@ ValuePtr Reader::readList() auto list = makePtr(); while (!isEOF() && peek().type != Token::Type::ParenClose) { - list->add(readImpl()); + auto node = readImpl(); + if (node == nullptr) { + return nullptr; + } + list->add(node); } if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // ) @@ -163,7 +167,11 @@ ValuePtr Reader::readVector() auto vector = makePtr(); while (!isEOF() && peek().type != Token::Type::BracketClose) { - vector->add(readImpl()); + auto node = readImpl(); + if (node == nullptr) { + return nullptr; + } + vector->add(node); } if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ] diff --git a/src/stepA_mal.cpp b/src/stepA_mal.cpp index 4df9ff2..edd699d 100644 --- a/src/stepA_mal.cpp +++ b/src/stepA_mal.cpp @@ -24,7 +24,6 @@ #include "readline.h" #include "settings.h" -#if 1 namespace blaze { static blaze::Readline s_readline; @@ -178,4 +177,3 @@ auto main(int argc, char* argv[]) -> int return 0; } -#endif