From 7e094f890c8e3cd623ada6596e63b8e3c0fd8b1c Mon Sep 17 00:00:00 2001 From: slava Date: Mon, 30 Oct 2006 02:34:10 +0000 Subject: [PATCH] string>float primitive should not throw an error on invalid input (non-ASCII strings) --- vm/math.c | 17 ++++++++++++----- vm/types.c | 11 +++++++---- vm/types.h | 2 ++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/vm/math.c b/vm/math.c index c3789f1892..ceacbb8bbd 100644 --- a/vm/math.c +++ b/vm/math.c @@ -533,13 +533,20 @@ void primitive_str_to_float(void) maybe_gc(sizeof(F_FLOAT)); str = untag_string(dpeek()); - c_str = to_char_string(str,true); - end = c_str; - f = strtod(c_str,&end); - if(end != c_str + string_capacity(str)) + + /* if the string has nulls or chars > 255, its definitely not a float */ + if(!check_string(str,sizeof(char))) drepl(F); else - drepl(tag_float(f)); + { + c_str = to_char_string(str,false); + end = c_str; + f = strtod(c_str,&end); + if(end != c_str + string_capacity(str)) + drepl(F); + else + drepl(tag_float(f)); + } } void primitive_float_to_str(void) diff --git a/vm/types.c b/vm/types.c index b45ba532ea..bcf68ef109 100644 --- a/vm/types.c +++ b/vm/types.c @@ -304,7 +304,7 @@ void primitive_resize_string(void) MEMORY_TO_STRING(char,u8) MEMORY_TO_STRING(u16,u16) -void check_string(F_STRING *s, CELL max) +bool check_string(F_STRING *s, CELL max) { CELL capacity = string_capacity(s); CELL i; @@ -312,8 +312,9 @@ void check_string(F_STRING *s, CELL max) { u16 ch = string_nth(s,i); if(ch == '\0' || ch >= (1 << (max * 8))) - general_error(ERROR_C_STRING,tag_object(s),F,true); + return false; } + return true; } F_ARRAY *allot_c_string(CELL capacity, CELL size) @@ -339,7 +340,8 @@ F_ARRAY *allot_c_string(CELL capacity, CELL size) { \ CELL capacity = string_capacity(s); \ F_ARRAY *_c_str; \ - if(check) check_string(s,sizeof(type)); \ + if(check && !check_string(s,sizeof(type))) \ + general_error(ERROR_C_STRING,tag_object(s),F,true); \ _c_str = allot_c_string(capacity,sizeof(type)); \ type *c_str = (type*)(_c_str + 1); \ type##_string_to_memory(s,c_str); \ @@ -350,7 +352,8 @@ F_ARRAY *allot_c_string(CELL capacity, CELL size) { \ if(sizeof(type) == sizeof(u16)) \ { \ - if(check) check_string(s,sizeof(type)); \ + if(check && !check_string(s,sizeof(type))) \ + general_error(ERROR_C_STRING,tag_object(s),F,true); \ return (type*)(s + 1); \ } \ else \ diff --git a/vm/types.h b/vm/types.h index 2c555fab7a..ad927c4fb9 100644 --- a/vm/types.h +++ b/vm/types.h @@ -94,6 +94,8 @@ void primitive_string(void); F_STRING *resize_string(F_STRING *string, F_FIXNUM capacity, u16 fill); void primitive_resize_string(void); +bool check_string(F_STRING *s, CELL max); + F_STRING *memory_to_char_string(const char *string, CELL length); void primitive_memory_to_char_string(void); F_STRING *from_char_string(const char *c_string);