Basic mid now throws an error on out-of-bounds parameters rather than returning

an uninitialised pointer (and crashing).

Fixes: #54
This commit is contained in:
David Given 2018-06-05 09:53:56 +09:00
parent 9dede01efe
commit 3f049a4c29
2 changed files with 11 additions and 3 deletions

9
lang/basic/lib/lib.h Normal file
View file

@ -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

View file

@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "bc_string.h" #include "bc_string.h"
#include "lib.h"
/* $Id$ */ /* $Id$ */
@ -11,8 +12,6 @@
if (X == 0) \ if (X == 0) \
return (0); return (0);
extern char* salloc();
int _length(String* str) int _length(String* str)
{ {
okr(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);*/ /* printf("mid fcn called %d %d %s\n",i1,i2,s->strval);*/
if (i2 < 0 || i1 < -1) if (i2 < 0 || i1 < -1)
return (s2); /* or error? */ error(3);
if (i1 == -1) if (i1 == -1)
i1 = s->strlength; i1 = s->strlength;
s2 = _newstr(s->strval); s2 = _newstr(s->strval);