Have different exit codes for errors vs exceptions

This commit is contained in:
Nigel Tao committed 2020-02-23 17:05:49 +11:00
1 parent b1af7b9b41
commit 9cc2c25f3d
3 files changed
+61 -17

No files matched your search

+28 -7
View File
@@ -27,6 +27,7 @@ for a C++ compiler $CXX, such as clang++ or g++.
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
// 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;
}
+25 -7
View File
@@ -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;
}
+8 -3
View File
@@ -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;
}