ref: 12a2928c2a6ecbac382c5fe20c8fe28dec47d8c4
parent: ea8a8d710c4f29b31a9222f64b75184122ed335d
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Nov 11 15:19:42 EST 2024
import from Julia: "fix a couple small flisp memory bugs", by Jeff Bezanson This is d5d80317f68d9e81e940b900e7feb9030eaace21.
--- a/read.c
+++ b/read.c
@@ -92,7 +92,7 @@
#define RS value2c(ios_t*, FL(readstate)->source)
-int
+bool
isnumtok_base(const char *tok, value_t *pval, int base)
{
char *end;
@@ -101,13 +101,13 @@
double d;
mpint *mp = nil;
if(*tok == '\0')
- return 0;
+ return false;
if(!((tok[0] == '0' && tok[1] == 'x') || (base >= 15)) && strpbrk(tok, ".eEpP")){
d = strtod(tok, &end);
if(*end == '\0'){
if(pval)
*pval = mk_double(d);
- return 1;
+ return true;
}
// floats can end in f or f0
if(end > tok && end[0] == 'f' &&
@@ -115,7 +115,7 @@
(end[1] == '0' && end[2] == '\0'))){
if(pval)
*pval = mk_float((float)d);
- return 1;
+ return true;
}
}
@@ -123,42 +123,44 @@
if(!strcmp(tok, "+NaN") || !strcasecmp(tok, "+nan.0")){
if(pval)
*pval = mk_double(D_PNAN);
- return 1;
+ return true;
}
if(!strcmp(tok, "+Inf") || !strcasecmp(tok, "+inf.0")){
if(pval)
*pval = mk_double(D_PINF);
- return 1;
+ return true;
}
}else if(tok[0] == '-'){
if(!strcmp(tok, "-NaN") || !strcasecmp(tok, "-nan.0")){
if(pval)
*pval = mk_double(D_NNAN);
- return 1;
+ return true;
}
if(!strcmp(tok, "-Inf") || !strcasecmp(tok, "-inf.0")){
if(pval)
*pval = mk_double(D_NINF);
- return 1;
+ return true;
}
i64 = strtoll_mp(tok, &end, base, &mp);
+ bool ok = *end == '\0';
if(pval)
*pval = mp == nil ? return_from_int64(i64) : mk_mpint(mp);
- return *end == '\0';
+ return ok;
}
ui64 = strtoull_mp(tok, &end, base, &mp);
+ bool ok = *end == '\0';
if(pval)
*pval = mp == nil ? return_from_uint64(ui64) : mk_mpint(mp);
- return *end == '\0';
+ return ok;
}
-int
+bool
isnumtok(const char *tok, value_t *pval)
{
return isnumtok_base(tok, pval, 0);
}
-static int
+static bool
read_numtok(const char *tok, value_t *pval, int base)
{
return isnumtok_base(tok, pval, base);
@@ -481,6 +483,7 @@
while(peek(ctx) != closer){
if(ios_eof(RS))
parse_error("unexpected end of input");
+ v = FL(stack)[FL(sp)-1]; // reload after possible alloc in peek()
if(i >= vector_size(v)){
v = FL(stack)[FL(sp)-1] = vector_grow(v, label != UNBOUND);
if(label != UNBOUND)
--- a/read.h
+++ b/read.h
@@ -1,8 +1,8 @@
#pragma once
value_t fl_read_sexpr(value_t f);
-int isnumtok_base(const char *tok, value_t *pval, int base);
-int isnumtok(const char *tok, value_t *pval);
+bool isnumtok_base(const char *tok, value_t *pval, int base);
+bool isnumtok(const char *tok, value_t *pval);
// defines which characters are ordinary symbol characters.
// exceptions are '.', which is an ordinary symbol character
--- a/string.c
+++ b/string.c
@@ -201,6 +201,7 @@
if(endbytes == startbytes)
return symbol_value(FL(emptystringsym));
value_t ns = cvalue_string(endbytes-startbytes);
+ s = cvalue_data(args[0]); // reload after alloc
memmove(cv_data(ptr(ns)), s+startbytes, endbytes-startbytes);
return ns;
}