From f4528e64b95f55c3cff0b94ef42ecfe88e4874ff Mon Sep 17 00:00:00 2001 From: Vera Lewis Date: Thu, 25 Dec 2025 20:25:58 -0600 Subject: [PATCH] added guard for empty expressions --- src/ee.c | 20 +++++++++++++++----- src/ee.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ee.c b/src/ee.c index 16229d1..ddcd50d 100644 --- a/src/ee.c +++ b/src/ee.c @@ -11,8 +11,13 @@ main (const int argc, const char* argv[]) const char** endPtr = argv+argc, ** arg = argv+1 ; + /* Guard for empty expressions */ + if (arg>=endPtr) { + error |= ERR_NO_EXPR; + } + char* fmt = "%g\n"; - if (strcmp(*arg,"-f") == 0) { + if (!error && strcmp(*arg,"-f") == 0) { fmt ="%f\n"; arg+= 1; } @@ -35,11 +40,14 @@ main (const int argc, const char* argv[]) } - if (expr == NULL) - error |= ERR_NS_OPERANDS; + /* Detect malformed expressions */ + if (!error) { + if (expr == NULL) + error |= ERR_NS_OPERANDS; - else if ((*expr).tail != NULL) - error |= ERR_NS_OPERATORS; + else if ((*expr).tail != NULL) + error |= ERR_NS_OPERATORS; + } report(&expr, fmt); @@ -202,6 +210,8 @@ report(STACK* expr, char* fmt) else if (error & ERR_DIV_BY_ZERO) printf("Undefined\n"); + else if (error & ERR_NO_EXPR) + printf("No expression provided.\n"); else printf(fmt, peek(expr)); } diff --git a/src/ee.h b/src/ee.h index 77faa9b..09f9c8f 100644 --- a/src/ee.h +++ b/src/ee.h @@ -11,6 +11,7 @@ const int ERR_NS_OPERATORS = 2; const int ERR_INVALID_INPUT = 4; const int ERR_INVALID_FACTORIAL = 8; const int ERR_DIV_BY_ZERO = 16; +const int ERR_NO_EXPR = 32; struct EE_STACK_NODE {