From 6d57362026dcd33f6502b968cee57851d16ee665 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 7 Jul 2016 23:50:39 -0400 Subject: Make serd_strtod API const-correct This is an API breakage, but a minor one (particularly since NULL is allowed) that avoids the flaw in the C API. --- serd/serd.h | 8 +++++--- src/reader.c | 2 +- src/reader.h | 2 +- src/serd_internal.h | 2 +- src/string.c | 6 +++--- tests/serd_test.c | 9 +++++++-- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/serd/serd.h b/serd/serd.h index 4f5501f6..51b00217 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -317,13 +317,15 @@ serd_strlen(const char* str, SerdNodeFlags* flags); /** Parse a string to a double. - The API of this function is identical to the standard C strtod function, + The API of this function is similar to the standard C strtod function, except this function is locale-independent and always matches the lexical - format used in the Turtle grammar (the decimal point is always "."). + format used in the Turtle grammar (the decimal point is always "."). The + end parameter is an offset from the start of `str` to avoid the + const-correctness issues of the strtod API. */ SERD_API double -serd_strtod(const char* str, char** endptr); +serd_strtod(const char* str, size_t* end); /** Decode a base64 string. diff --git a/src/reader.c b/src/reader.c index 026364ae..89f3cca0 100644 --- a/src/reader.c +++ b/src/reader.c @@ -76,7 +76,7 @@ Ref push_node_padded(SerdReader* reader, size_t maxlen, SerdType type, const char* str, size_t n_bytes) { - char* mem = (char*)serd_stack_push_aligned( + void* mem = serd_stack_push_aligned( &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); SerdNode* const node = (SerdNode*)mem; diff --git a/src/reader.h b/src/reader.h index 18b587c8..d5c8595b 100644 --- a/src/reader.h +++ b/src/reader.h @@ -66,7 +66,7 @@ static inline SerdStatus push_byte(SerdReader* reader, Ref ref, const uint8_t c) { SERD_STACK_ASSERT_TOP(reader, ref); - char* const s = serd_stack_push(&reader->stack, 1); + char* const s = (char*)serd_stack_push(&reader->stack, 1); SerdNode* const node = (SerdNode*)(reader->stack.buf + ref); ++node->n_bytes; *(s - 1) = c; diff --git a/src/serd_internal.h b/src/serd_internal.h index 18141b8a..d4f01029 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -195,7 +195,7 @@ serd_stack_free(SerdStack* stack) stack->size = 0; } -static inline char* +static inline void* serd_stack_push(SerdStack* stack, size_t n_bytes) { const size_t new_size = stack->size + n_bytes; diff --git a/src/string.c b/src/string.c index 0417d249..381de470 100644 --- a/src/string.c +++ b/src/string.c @@ -100,7 +100,7 @@ read_sign(const char** sptr) } double -serd_strtod(const char* str, char** endptr) +serd_strtod(const char* str, size_t* end) { double result = 0.0; @@ -136,8 +136,8 @@ serd_strtod(const char* str, char** endptr) result *= pow(10, expt * expt_sign); } - if (endptr) { - *endptr = (char*)s; + if (end) { + *end = s - str; } return result * sign; diff --git a/tests/serd_test.c b/tests/serd_test.c index 8a91ab42..a8d17349 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -38,13 +38,18 @@ test_strtod(double dbl, double max_delta) char buf[1024]; snprintf(buf, sizeof(buf), "%f", dbl); - char* endptr = NULL; - const double out = serd_strtod(buf, &endptr); + size_t end = 0; + const double out = serd_strtod(buf, &end); const double diff = fabs(out - dbl); if (diff > max_delta) { FAILF("Parsed %lf != %lf (delta %lf)\n", dbl, out, diff); } + + if (end != strlen(buf)) { + FAILF("Parsed %lf length %zu != %zu\n", end, strlen(buf)); + } + return 0; } -- cgit v1.2.1