string>float primitive should not throw an error on invalid input (non-ASCII strings)

slava 2006-10-30 02:34:10 +00:00
parent 5bb98d28d9
commit 7e094f890c
3 changed files with 21 additions and 9 deletions

View File

@ -533,13 +533,20 @@ void primitive_str_to_float(void)
maybe_gc(sizeof(F_FLOAT)); maybe_gc(sizeof(F_FLOAT));
str = untag_string(dpeek()); str = untag_string(dpeek());
c_str = to_char_string(str,true);
end = c_str; /* if the string has nulls or chars > 255, its definitely not a float */
f = strtod(c_str,&end); if(!check_string(str,sizeof(char)))
if(end != c_str + string_capacity(str))
drepl(F); drepl(F);
else 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) void primitive_float_to_str(void)

View File

@ -304,7 +304,7 @@ void primitive_resize_string(void)
MEMORY_TO_STRING(char,u8) MEMORY_TO_STRING(char,u8)
MEMORY_TO_STRING(u16,u16) 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 capacity = string_capacity(s);
CELL i; CELL i;
@ -312,8 +312,9 @@ void check_string(F_STRING *s, CELL max)
{ {
u16 ch = string_nth(s,i); u16 ch = string_nth(s,i);
if(ch == '\0' || ch >= (1 << (max * 8))) 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) 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); \ CELL capacity = string_capacity(s); \
F_ARRAY *_c_str; \ 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)); \ _c_str = allot_c_string(capacity,sizeof(type)); \
type *c_str = (type*)(_c_str + 1); \ type *c_str = (type*)(_c_str + 1); \
type##_string_to_memory(s,c_str); \ 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(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); \ return (type*)(s + 1); \
} \ } \
else \ else \

View File

@ -94,6 +94,8 @@ void primitive_string(void);
F_STRING *resize_string(F_STRING *string, F_FIXNUM capacity, u16 fill); F_STRING *resize_string(F_STRING *string, F_FIXNUM capacity, u16 fill);
void primitive_resize_string(void); void primitive_resize_string(void);
bool check_string(F_STRING *s, CELL max);
F_STRING *memory_to_char_string(const char *string, CELL length); F_STRING *memory_to_char_string(const char *string, CELL length);
void primitive_memory_to_char_string(void); void primitive_memory_to_char_string(void);
F_STRING *from_char_string(const char *c_string); F_STRING *from_char_string(const char *c_string);