From 6a9e2743d149fecd5d7706b273dc69a45fe25ea2 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 22 Mar 2019 16:19:39 +0100 Subject: openpgp-ffi: Replace glibc's error function. - To make the tests and examples more portable, provide our own roughly compatible replacement for glibc's error(3). --- openpgp-ffi/examples/armor.c | 10 ++++++++- openpgp-ffi/examples/decrypt-with.c | 11 +++++++++- openpgp-ffi/examples/encrypt-for.c | 12 ++++++++++- openpgp-ffi/examples/example.c | 11 +++++++++- openpgp-ffi/examples/immutable-reference-demo.c | 10 ++++++++- openpgp-ffi/examples/parser.c | 12 ++++++++++- openpgp-ffi/examples/reader.c | 11 +++++++++- openpgp-ffi/examples/type-safety-demo.c | 10 ++++++++- openpgp-ffi/examples/use-after-free-demo.c | 10 ++++++++- openpgp-ffi/tests/c-tests.rs | 28 +++++++++++++++++++------ 10 files changed, 110 insertions(+), 15 deletions(-) (limited to 'openpgp-ffi') diff --git a/openpgp-ffi/examples/armor.c b/openpgp-ffi/examples/armor.c index 42395811..49ed533c 100644 --- a/openpgp-ffi/examples/armor.c +++ b/openpgp-ffi/examples/armor.c @@ -1,6 +1,14 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include diff --git a/openpgp-ffi/examples/decrypt-with.c b/openpgp-ffi/examples/decrypt-with.c index ae5e871d..d30b2d52 100644 --- a/openpgp-ffi/examples/decrypt-with.c +++ b/openpgp-ffi/examples/decrypt-with.c @@ -3,10 +3,19 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include +#include #include diff --git a/openpgp-ffi/examples/encrypt-for.c b/openpgp-ffi/examples/encrypt-for.c index 4a92d023..de2abd64 100644 --- a/openpgp-ffi/examples/encrypt-for.c +++ b/openpgp-ffi/examples/encrypt-for.c @@ -2,9 +2,19 @@ encrypt a file. */ #define _GNU_SOURCE -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include +#include +#include #include diff --git a/openpgp-ffi/examples/example.c b/openpgp-ffi/examples/example.c index 46a45529..3c561761 100644 --- a/openpgp-ffi/examples/example.c +++ b/openpgp-ffi/examples/example.c @@ -1,8 +1,17 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include +#include #include diff --git a/openpgp-ffi/examples/immutable-reference-demo.c b/openpgp-ffi/examples/immutable-reference-demo.c index a0ada27b..47efbdd6 100644 --- a/openpgp-ffi/examples/immutable-reference-demo.c +++ b/openpgp-ffi/examples/immutable-reference-demo.c @@ -1,6 +1,14 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include diff --git a/openpgp-ffi/examples/parser.c b/openpgp-ffi/examples/parser.c index 5ded2de2..dc404fec 100644 --- a/openpgp-ffi/examples/parser.c +++ b/openpgp-ffi/examples/parser.c @@ -2,10 +2,20 @@ * also serves as a simple benchmark. */ #define _GNU_SOURCE -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include +#include +#include #include diff --git a/openpgp-ffi/examples/reader.c b/openpgp-ffi/examples/reader.c index b0273ebb..13c47f69 100644 --- a/openpgp-ffi/examples/reader.c +++ b/openpgp-ffi/examples/reader.c @@ -1,8 +1,17 @@ #define _GNU_SOURCE -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include +#include #include diff --git a/openpgp-ffi/examples/type-safety-demo.c b/openpgp-ffi/examples/type-safety-demo.c index ad4870e2..d2bc523e 100644 --- a/openpgp-ffi/examples/type-safety-demo.c +++ b/openpgp-ffi/examples/type-safety-demo.c @@ -1,6 +1,14 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include diff --git a/openpgp-ffi/examples/use-after-free-demo.c b/openpgp-ffi/examples/use-after-free-demo.c index e64a3c1f..30863c81 100644 --- a/openpgp-ffi/examples/use-after-free-demo.c +++ b/openpgp-ffi/examples/use-after-free-demo.c @@ -1,6 +1,14 @@ #define _GNU_SOURCE #include -#include +/* Roughly glibc compatible error reporting. */ +#define error(S, E, F, ...) do { \ + fprintf (stderr, (F), __VA_ARGS__); \ + int s = (S), e = (E); \ + if (e) { fprintf (stderr, ": %s", strerror (e)); } \ + fprintf (stderr, "\n"); \ + fflush (stderr); \ + if (s) { exit (s); } \ + } while (0) #include #include #include diff --git a/openpgp-ffi/tests/c-tests.rs b/openpgp-ffi/tests/c-tests.rs index 37c00231..6a2d04e0 100644 --- a/openpgp-ffi/tests/c-tests.rs +++ b/openpgp-ffi/tests/c-tests.rs @@ -262,9 +262,7 @@ fn run(ldpath: &Path, exe: &Path) -> io::Result<()> { /// Wraps the code in a main function if none exists. fn wrap_with_main(test: &mut Vec, offset: usize) { - if has_main(test) { - return; - } + let needs_wrapping = ! has_main(test); let mut last_include = 0; for (n, line) in test.iter().enumerate() { @@ -273,10 +271,28 @@ fn wrap_with_main(test: &mut Vec, offset: usize) { } } - test.insert(last_include + 1, "int main() {".into()); - test.insert(last_include + 2, format!("#line {}", last_include + 1 + offset)); + test.insert(last_include + 1, + "#define error(S, E, F, ...) do { \\\n\ + fprintf (stderr, (F), __VA_ARGS__); \\\n\ + int s = (S), e = (E); \\\n\ + if (e) { fprintf (stderr, \": %s\", strerror (e)); } \\\n\ + fprintf (stderr, \"\\n\"); \\\n\ + fflush (stderr); \\\n\ + if (s) { exit (s); } \\\n\ + } while (0)".into()); + + if needs_wrapping { + test.insert(last_include + 1, "int main() {".into()); + } + test.insert(last_include + 2, format!("#line {}", last_include + offset + + if needs_wrapping {1} else {0})); let last = test.len(); - test.insert(last, "}".into()); + if needs_wrapping { + test.insert(last, "}".into()); + } + test.insert(0, "#include ".into()); + test.insert(0, "#include ".into()); + test.insert(0, "#include ".into()); test.insert(0, "#define _GNU_SOURCE".into()); } -- cgit v1.2.3