diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-03-22 16:19:39 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-03-22 16:19:39 +0100 |
commit | 6a9e2743d149fecd5d7706b273dc69a45fe25ea2 (patch) | |
tree | f1d3d40d578467416659d96cee6a2a68b9c31889 /openpgp-ffi | |
parent | 1d35557a8f7866141f84b2764287f384a8e12872 (diff) |
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).
Diffstat (limited to 'openpgp-ffi')
-rw-r--r-- | openpgp-ffi/examples/armor.c | 10 | ||||
-rw-r--r-- | openpgp-ffi/examples/decrypt-with.c | 11 | ||||
-rw-r--r-- | openpgp-ffi/examples/encrypt-for.c | 12 | ||||
-rw-r--r-- | openpgp-ffi/examples/example.c | 11 | ||||
-rw-r--r-- | openpgp-ffi/examples/immutable-reference-demo.c | 10 | ||||
-rw-r--r-- | openpgp-ffi/examples/parser.c | 12 | ||||
-rw-r--r-- | openpgp-ffi/examples/reader.c | 11 | ||||
-rw-r--r-- | openpgp-ffi/examples/type-safety-demo.c | 10 | ||||
-rw-r--r-- | openpgp-ffi/examples/use-after-free-demo.c | 10 | ||||
-rw-r--r-- | openpgp-ffi/tests/c-tests.rs | 28 |
10 files changed, 110 insertions, 15 deletions
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 <assert.h> -#include <error.h> +/* 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 <stdio.h> #include <stdlib.h> #include <string.h> 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 <assert.h> -#include <error.h> +/* 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 <errno.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <sequoia/openpgp.h> 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 <error.h> +/* 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 <errno.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <sequoia/openpgp.h> 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 <errno.h> -#include <error.h> +/* 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 <stdio.h> #include <stdlib.h> +#include <string.h> #include <sequoia/openpgp.h> 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 <errno.h> -#include <error.h> +/* 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 <fcntl.h> #include <stdio.h> #include <stdlib.h> 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 <error.h> +/* 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 <errno.h> #include <stdio.h> #include <time.h> +#include <string.h> +#include <stdlib.h> #include <sequoia/openpgp.h> 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 <error.h> +/* 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 <errno.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <sequoia/openpgp.h> 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 <errno.h> -#include <error.h> +/* 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 <fcntl.h> #include <stdio.h> #include <stdlib.h> 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 <errno.h> -#include <error.h> +/* 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 <fcntl.h> #include <stdio.h> #include <stdlib.h> 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<String>, 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<String>, 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 <string.h>".into()); + test.insert(0, "#include <stdlib.h>".into()); + test.insert(0, "#include <stdio.h>".into()); test.insert(0, "#define _GNU_SOURCE".into()); } |