Basic mid now throws an error on out-of-bounds parameters rather than returning
authorDavid Given <dg@cowlark.com>
Tue, 5 Jun 2018 00:53:56 +0000 (09:53 +0900)
committerDavid Given <dg@cowlark.com>
Tue, 5 Jun 2018 00:53:56 +0000 (09:53 +0900)
an uninitialised pointer (and crashing).

Fixes: #54

lang/basic/lib/lib.h [new file with mode: 0644]
lang/basic/lib/string.c

diff --git a/lang/basic/lib/lib.h b/lang/basic/lib/lib.h
new file mode 100644 (file)
index 0000000..8dc371c
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef LIB_H
+#define LIB_H
+
+extern void error(int index);
+extern char* salloc(unsigned length);
+extern void sfree(char* c);
+
+#endif
+
index 4c3d7a3..c4cdbe1 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "bc_string.h"
+#include "lib.h"
 
 /* $Id$ */
 
@@ -11,8 +12,6 @@
        if (X == 0) \
                return (0);
 
-extern char* salloc();
-
 int _length(String* str)
 {
        okr(str);
@@ -158,7 +157,7 @@ String* _mid(int i1, int i2, String* s)
 
        /*      printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/
        if (i2 < 0 || i1 < -1)
-               return (s2); /* or error? */
+               error(3);
        if (i1 == -1)
                i1 = s->strlength;
        s2 = _newstr(s->strval);