]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Added very minimal stack limit checking after execution of each word to *hopefully...
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 12 Apr 2014 14:23:00 +0000 (10:23 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 12 Apr 2014 14:23:00 +0000 (10:23 -0400)
source/slvm/main.c

index 03c5669ae0678be98bd997cffeedef34a77a2c69..6cd15811fbb9d42bafcb856133a24dd79bdc8d01 100644 (file)
@@ -48,6 +48,7 @@ static long* CodePtr;
 /** The argument stack */
 static long ArgStack[32];
 
+/** A state variable used to flag when the interpreter reads a line of input */
 static long Line_Read;
 
 /**
@@ -64,6 +65,21 @@ static void docolon(long* code) {
     CodePtr = prev_code;
 }
 
+static void check_stack(void)
+{
+    if(ArgStackPtr < (ArgStack-1))
+    {
+        puts("Stack Underflow!");
+        exit(1);
+    }
+
+    if(ArgStackPtr > (ArgStack+30))
+    {
+        puts("Stack Overflow!");
+        exit(1);
+    }
+}
+
 /**
  * Define a built-in word that executes native code */
 #define defcode(name_str,c_name,flags,prev)      \
@@ -275,7 +291,6 @@ defcode("execw", exec_word, 0, &semicolon){
 defcode("parsenum", parse_num, 0, &exec_word){
     char* end;
     long num = strtol((const char *)*(ArgStackPtr), &end, 10);
-    //*(ArgStackPtr) = strtol((const char *)*(ArgStackPtr), &end, 10);
     if(end != (char *)*(ArgStackPtr))
     {
         *(ArgStackPtr) = num;
@@ -336,9 +351,9 @@ defcode("quit", quit, 0, &interpret){
     while(1)
     {
         EXEC(interpret);
-
         if(Line_Read)
         {
+            check_stack();
             long stacksz = ArgStackPtr - ArgStack + 1;
             if (stacksz > 5)
                 printf("( ... ");