diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index a6ee3553..ce24ed9f 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -27,6 +27,7 @@ for a C++ compiler $CXX, such as clang++ or g++. #include #include +#include // Wuffs ships as a "single file C library" or "header file library" as per // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt @@ -94,7 +95,7 @@ size_t indent; const char* read_src() { if (src.meta.closed) { - return "main: read error: unexpected EOF"; + return "main: internal error: read requested on a closed source"; } src.compact(); if (src.meta.wi >= src.data.len) { @@ -471,13 +472,33 @@ const char* main1(int argc, char** argv) { return nullptr; } +int compute_exit_code(const char* status_msg) { + if (!status_msg) { + return 0; + } + size_t n = strnlen(status_msg, 2047); + if (n >= 2047) { + status_msg = "main: internal error: error message is too long"; + n = strnlen(status_msg, 2047); + } + fprintf(stderr, "%s\n", status_msg); + // Return an exit code of 1 for regular (forseen) errors, e.g. badly + // formatted or unsupported input. + // + // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive + // run-time checks found that an internal invariant did not hold. + // + // Automated testing, including badly formatted inputs, can therefore + // discriminate between expected failure (exit code 1) and unexpected failure + // (other non-zero exit codes). Specifically, exit code 2 for internal + // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 + // linux) for a segmentation fault (e.g. null pointer dereference). + return strstr(status_msg, "internal error:") ? 2 : 1; +} + int main(int argc, char** argv) { const char* z0 = main1(argc, argv); const char* z1 = flush_dst(); - const char* z = z0 ? z0 : z1; - if (z) { - fprintf(stderr, "%s\n", z); - return 1; - } - return 0; + int exit_code = compute_exit_code(z0 ? z0 : z1); + return exit_code; } diff --git a/example/zcat/zcat.c b/example/zcat/zcat.c index 6cc90317..7652c48c 100644 --- a/example/zcat/zcat.c +++ b/example/zcat/zcat.c @@ -151,11 +151,30 @@ static const char* decode() { } } -int fail(const char* msg) { +int compute_exit_code(const char* status_msg) { + if (!status_msg) { + return 0; + } + size_t n = strnlen(status_msg, 2047); + if (n >= 2047) { + status_msg = "main: internal error: error message is too long"; + n = strnlen(status_msg, 2047); + } const int stderr_fd = 2; - ignore_return_value(write(stderr_fd, msg, strnlen(msg, 4095))); + ignore_return_value(write(stderr_fd, status_msg, n)); ignore_return_value(write(stderr_fd, "\n", 1)); - return 1; + // Return an exit code of 1 for regular (forseen) errors, e.g. badly + // formatted or unsupported input. + // + // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive + // run-time checks found that an internal invariant did not hold. + // + // Automated testing, including badly formatted inputs, can therefore + // discriminate between expected failure (exit code 1) and unexpected failure + // (other non-zero exit codes). Specifically, exit code 2 for internal + // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 + // linux) for a segmentation fault (e.g. null pointer dereference). + return strstr(status_msg, "internal error:") ? 2 : 1; } int main(int argc, char** argv) { @@ -163,13 +182,12 @@ int main(int argc, char** argv) { prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); #endif - const char* status_msg = decode(); - int status_code = status_msg ? fail(status_msg) : 0; + int exit_code = compute_exit_code(decode()); #if defined(WUFFS_EXAMPLE_USE_SECCOMP) // Call SYS_exit explicitly instead of SYS_exit_group implicitly. // SECCOMP_MODE_STRICT allows only the former. - syscall(SYS_exit, status_code); + syscall(SYS_exit, exit_code); #endif - return status_code; + return exit_code; } diff --git a/fuzz/c/fuzzlib/fuzzlib.c b/fuzz/c/fuzzlib/fuzzlib.c index a91e1e4c..74e6baa7 100644 --- a/fuzz/c/fuzzlib/fuzzlib.c +++ b/fuzz/c/fuzzlib/fuzzlib.c @@ -56,9 +56,14 @@ static const char* llvmFuzzerTestOneInput(const uint8_t* data, size_t size) { }); const char* msg = fuzz(&src, hash); - if (msg && strstr(msg, "internal error:")) { - fprintf(stderr, "internal errors shouldn't occur: \"%s\"\n", msg); - intentional_segfault(); + if (msg) { + if (strnlen(msg, 2047) >= 2047) { + msg = "fuzzlib: internal error: error message is too long"; + } + if (strstr(msg, "internal error:")) { + fprintf(stderr, "internal errors shouldn't occur: \"%s\"\n", msg); + intentional_segfault(); + } } return msg; }