From: Simon Ruderich Date: Fri, 21 Jun 2013 16:49:11 +0000 (+0200) Subject: tests: Check return values of write() and dup2(). X-Git-Tag: 0.1~23 X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=commitdiff_plain;h=219a9882ba55e2dc60f0ab40a2a3afcc7130417c tests: Check return values of write() and dup2(). --- diff --git a/tests/example.c b/tests/example.c index 3d165e5..54f6c95 100644 --- a/tests/example.c +++ b/tests/example.c @@ -25,6 +25,7 @@ #include #include "../src/compiler.h" +#include "example.h" int main(int argc, char **argv unused) { @@ -35,22 +36,21 @@ int main(int argc, char **argv unused) { errno = ENOMEM; perror("error!"); - write(STDERR_FILENO, "write to stderr 2", 17); - write(STDOUT_FILENO, "write to stdout 2", 17); + xwrite(STDERR_FILENO, "write to stderr 2", 17); + xwrite(STDOUT_FILENO, "write to stdout 2", 17); fprintf(stderr, "\n"); fprintf(stdout, "\n"); fflush(stdout); - /* Check usage of tracked_fds_list (at least in parts). No error checking - * here! */ - dup2(STDERR_FILENO, 471); - dup2(471, 42); - write(471, "more on stderr\n", 15); + /* Check usage of tracked_fds_list (at least in parts). */ + xdup2(STDERR_FILENO, 471); + xdup2(471, 42); + xwrite(471, "more on stderr\n", 15); close(471); - dup2(STDOUT_FILENO, 471); - write(42, "stderr ...\n", 11); - write(471, "more on stdout\n", 15); + xdup2(STDOUT_FILENO, 471); + xwrite(42, "stderr ...\n", 11); + xwrite(471, "more on stdout\n", 15); /* Glibc uses __overflow() for this ... */ putc_unlocked('x', stderr); diff --git a/tests/example.h b/tests/example.h index facbc30..7c27c5a 100644 --- a/tests/example.h +++ b/tests/example.h @@ -36,3 +36,22 @@ } \ fflush(stdout); \ } else + +static ssize_t xwrite(int fd, void const *buf, size_t count) { + ssize_t result = write(fd, buf, count); + if (result == -1) { + perror("write"); + exit(EXIT_FAILURE); + } + /* Ignore short writes here. Doesn't matter for test cases. */ + return result; +} + +static int xdup2(int oldfd, int newfd) { + int result = dup2(oldfd, newfd); + if (result == -1) { + perror("dup2"); + exit(EXIT_FAILURE); + } + return result; +} diff --git a/tests/example_exec.c b/tests/example_exec.c index 2d22ce0..1220f85 100644 --- a/tests/example_exec.c +++ b/tests/example_exec.c @@ -27,6 +27,7 @@ #include #include +#include "example.h" #include "../src/compiler.h" @@ -123,7 +124,7 @@ int main(int argc unused, char **argv) { printf("\nCHECKING COLORING.\n\n"); fflush(stdout); - dup2(2, 3); + xdup2(2, 3); char *args[] = { argv0, NULL }; char *envp[] = { ldpreload, NULL }; @@ -131,24 +132,24 @@ int main(int argc unused, char **argv) { execve(argv[0], args, envp); return EXIT_FAILURE; } else if (!skip--) { - dup2(2, 4); + xdup2(2, 4); execl(argv[0], argv0, NULL); return EXIT_FAILURE; } else if (!skip--) { - dup2(2, 5); + xdup2(2, 5); execlp(argv[0], argv0, NULL); return EXIT_FAILURE; } else if (!skip--) { - dup2(2, 6); + xdup2(2, 6); char *envp[] = { ldpreload, NULL }; execle(argv[0], argv0, NULL, envp); return EXIT_FAILURE; } else if (!skip--) { - dup2(2, 7); + xdup2(2, 7); /* Test closing a few descriptors. */ close(5); @@ -159,7 +160,7 @@ int main(int argc unused, char **argv) { execv(argv[0], args); return EXIT_FAILURE; } else if (!skip--) { - dup2(2, 8); + xdup2(2, 8); /* And a few more. */ close(7); @@ -177,7 +178,7 @@ int main(int argc unused, char **argv) { close(3); close(8); - dup2(2, 5); + xdup2(2, 5); char *args[] = { argv0, NULL }; char *envp[] = { ldpreload, "COLORED_STDERR_FDS=5,", NULL }; @@ -188,7 +189,7 @@ int main(int argc unused, char **argv) { char *args[] = { argv0, NULL }; char *envp[] = { ldpreload, NULL }; - dup2(5, 6); + xdup2(5, 6); close(5); execve(argv[0], args, envp);