aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2016-07-07 23:50:39 -0400
committerDavid Robillard <d@drobilla.net>2019-12-19 08:43:27 -0500
commit28de6689533914888093d32d3075f58e6f32a4a1 (patch)
tree961b3e9fc79eabf4003d379fbab4c2404735310b
parentf16313b1c8f559ede8ca47b6b305269a78ea2709 (diff)
downloadserd-28de6689533914888093d32d3075f58e6f32a4a1.tar.gz
serd-28de6689533914888093d32d3075f58e6f32a4a1.tar.bz2
serd-28de6689533914888093d32d3075f58e6f32a4a1.zip
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.
-rw-r--r--NEWS1
-rw-r--r--serd/serd.h8
-rw-r--r--src/reader.c2
-rw-r--r--src/reader.h2
-rw-r--r--src/serd_internal.h2
-rw-r--r--src/string.c6
-rw-r--r--tests/serd_test.c5
7 files changed, 15 insertions, 11 deletions
diff --git a/NEWS b/NEWS
index 35beddd2..41e11525 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,7 @@
serd (1.0.1) unstable;
* Add SerdBuffer for mutable buffers to keep SerdChunk const-correct
+ * Make serd_strtod API const-correct
* Remove useless character counting from API
* Rename SerdChunk to SerdStringView
* Use char* for strings in public API
diff --git a/serd/serd.h b/serd/serd.h
index e8aff1e8..ba285255 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -316,13 +316,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 a0bf2ea2..f063358b 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -80,7 +80,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 88603faa..4b8441b1 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -75,7 +75,7 @@ push_byte(SerdReader* reader, Ref ref, const int c)
assert(c != EOF);
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) = (uint8_t)c;
diff --git a/src/serd_internal.h b/src/serd_internal.h
index 165aefd1..211d260e 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -208,7 +208,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 b564a874..7229d6b2 100644
--- a/src/string.c
+++ b/src/string.c
@@ -101,7 +101,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;
@@ -137,8 +137,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 04adb560..61a76864 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -39,11 +39,12 @@ 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);
assert(diff <= max_delta);
+ assert(end == strlen(buf));
}
static SerdStatus