diff options
author | Tobias Stoeckmann <tobias@stoeckmann.org> | 2020-08-22 11:35:50 +0200 |
---|---|---|
committer | Tobias Stoeckmann <tobias@stoeckmann.org> | 2020-08-22 11:35:50 +0200 |
commit | 4298431150df9a83390a14006217c230e684994b (patch) | |
tree | 1b3294f56fc7b49bf3eed3d41ffebe73561802ab | |
parent | 2b439ea59857747067e8272011ad67303e0d4cf1 (diff) | |
download | json-c-4298431150df9a83390a14006217c230e684994b.tar.gz |
Properly format errnos in _json_c_strerror
The function _json_c_strerror does not properly format unknown errnos.
The int to ascii loop ignores the leading digit if the number can be
divided by 10 and if an errno has been formatted, shorter errnos would
not properly terminate the newly created string, showing the ending
numbers of the previous output.
A test case has been added to show these effects.
Since this function has been introduced for tests, the effect of this on
real life code is basically non-existing. First an environment variable
has to be set to activate this strerror code and second an unknown errno
would have to be encountered.
-rw-r--r-- | strerror_override.c | 3 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/test_strerror.c | 11 | ||||
-rw-r--r-- | tests/test_strerror.expected | 2 | ||||
l--------- | tests/test_strerror.test | 1 |
5 files changed, 18 insertions, 2 deletions
diff --git a/strerror_override.c b/strerror_override.c index 7a262f7..a3dd377 100644 --- a/strerror_override.c +++ b/strerror_override.c @@ -94,7 +94,7 @@ char *_json_c_strerror(int errno_in) } // It's not one of the known errno values, return the numeric value. - for (ii = 0; errno_in > 10; errno_in /= 10, ii++) + for (ii = 0; errno_in >= 10; errno_in /= 10, ii++) { digbuf[ii] = "0123456789"[(errno_in % 10)]; } @@ -105,5 +105,6 @@ char *_json_c_strerror(int errno_in) { errno_buf[start_idx] = digbuf[ii]; } + errno_buf[start_idx] = '\0'; return errno_buf; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 125f615..0c5c26e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,12 +32,13 @@ foreach(TESTNAME test_printbuf test_set_serializer test_set_value + test_strerror test_util_file test_visit test_object_iterator) add_executable(${TESTNAME} ${TESTNAME}.c) -if(${TESTNAME} STREQUAL test_util_file) +if(${TESTNAME} STREQUAL test_strerror OR ${TESTNAME} STREQUAL test_util_file) # For output consistency, we need _json_c_strerror() in some tests: target_sources(${TESTNAME} PRIVATE ../strerror_override.c) endif() diff --git a/tests/test_strerror.c b/tests/test_strerror.c new file mode 100644 index 0000000..1780564 --- /dev/null +++ b/tests/test_strerror.c @@ -0,0 +1,11 @@ +#include "strerror_override.h" +#include "strerror_override_private.h" + +#include <stdio.h> + +int main(int argc, char **argv) +{ + puts(strerror(10000)); + puts(strerror(999)); + return 0; +} diff --git a/tests/test_strerror.expected b/tests/test_strerror.expected new file mode 100644 index 0000000..b6b3bb6 --- /dev/null +++ b/tests/test_strerror.expected @@ -0,0 +1,2 @@ +ERRNO=10000 +ERRNO=999 diff --git a/tests/test_strerror.test b/tests/test_strerror.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_strerror.test @@ -0,0 +1 @@ +test_basic.test
\ No newline at end of file |