Ensure nul-terminated arg to strtod.
Inside Str_To_F64 we use strtod, which requires a nul-terminated C
string argument. Now that Clownfish Strings consistently lack
nul-termination, we need to copy content into a nul-terminated buffer
and pass that to strtod.
diff --git a/CHANGES b/CHANGES
index d295a0b..ffa2e67 100644
--- a/CHANGES
+++ b/CHANGES
@@ -29,6 +29,7 @@
* [CLOWNFISH-62] - Crash when passing Perl variable as decremented arg
* [CLOWNFISH-63] - Don't export private methods via Go
* [CLOWNFISH-69] - Clownfish::CFC missing from CPAN prereqs
+ * [CLOWNFISH-84] - Guarantee nul-terminated arg to strtod in Str_To_F64
Improvements:
diff --git a/runtime/core/Clownfish/String.c b/runtime/core/Clownfish/String.c
index 7c2c0fa..ac17bad 100644
--- a/runtime/core/Clownfish/String.c
+++ b/runtime/core/Clownfish/String.c
@@ -263,8 +263,8 @@
return retval;
}
-static double
-S_safe_to_f64(String *self) {
+double
+Str_To_F64_IMP(String *self) {
size_t amount = self->size < 511 ? self->size : 511;
char buf[512];
memcpy(buf, self->ptr, amount);
@@ -272,17 +272,6 @@
return strtod(buf, NULL);
}
-double
-Str_To_F64_IMP(String *self) {
- char *end;
- double value = strtod(self->ptr, &end);
- size_t consumed = end - self->ptr;
- if (consumed > self->size) { // strtod overran
- value = S_safe_to_f64(self);
- }
- return value;
-}
-
char*
Str_To_Utf8_IMP(String *self) {
char *buf = (char*)malloc(self->size + 1);