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.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/788087e7 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/788087e7 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/788087e7 Branch: refs/heads/0.5 Commit: 788087e7e6a544bdba1e42b5bd30f2761071afed Parents: 7ecc951 Author: Marvin Humphrey <mar...@rectangular.com> Authored: Mon Mar 14 16:05:23 2016 -0700 Committer: Nick Wellnhofer <wellnho...@aevum.de> Committed: Fri Mar 25 10:01:38 2016 +0100 ---------------------------------------------------------------------- CHANGES | 1 + runtime/core/Clownfish/String.c | 15 ++------------- 2 files changed, 3 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/788087e7/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index d295a0b..ffa2e67 100644 --- a/CHANGES +++ b/CHANGES @@ -29,6 +29,7 @@ Revision history for Apache Clownfish * [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: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/788087e7/runtime/core/Clownfish/String.c ---------------------------------------------------------------------- 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 @@ Str_BaseX_To_I64_IMP(String *self, uint32_t base) { 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 @@ S_safe_to_f64(String *self) { 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);