read_write_all: use calloc instead of huge stack var in tests.
[ccan] / ccan / read_write_all / test / run-read_all.c
index 2790b64d5ce5c59ea57f5029cc7b6453a7f45a7a..b8f978ff2b9af246e777bfb3406813234c420c4c 100644 (file)
@@ -1,8 +1,8 @@
 /* FIXME: Do something tricky to ensure we really do loop in read_all. */
 
-#include "read_write_all/read_write_all.h"
-#include "read_write_all/read_write_all.c"
-#include "tap/tap.h"
+#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/read_write_all/read_write_all.c>
+#include <ccan/tap/tap.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <signal.h>
@@ -26,11 +26,12 @@ static void got_signal(int sig)
 
 int main(int argc, char *argv[])
 {
-       char buffer[BUFSZ*2] = { 0 };
+       char *buffer;
        char c = 0;
        int status;
        pid_t child;
 
+       buffer = calloc(BUFSZ, 2);
        plan_tests(6);
 
        /* We fork and torture parent. */
@@ -39,10 +40,12 @@ int main(int argc, char *argv[])
        child = fork();
 
        if (!child) {
+               close(p2c[1]);
+               close(c2p[0]);
                /* Child.  Make sure parent ready, then write in two parts. */
                if (read(p2c[0], &c, 1) != 1)
                        exit(1);
-               memset(buffer, 0xff, sizeof(buffer));
+               memset(buffer, 0xff, BUFSZ*2);
                if (!write_all(c2p[1], buffer, BUFSZ))
                        exit(2);
                if (kill(getppid(), SIGUSR1) != 0)
@@ -50,17 +53,19 @@ int main(int argc, char *argv[])
                /* Make sure they get signal. */
                if (read(p2c[0], &c, 1) != 1)
                        exit(4);
-               if (write(c2p[1], buffer, PIPE_BUF) != PIPE_BUF)
+               if (write(c2p[1], buffer, BUFSZ) != BUFSZ)
                        exit(5);
                exit(0);
        }
        if (child == -1)
                err(1, "forking");
 
+       close(p2c[0]);
+       close(c2p[1]);
        signal(SIGUSR1, got_signal);
        ok1(write(p2c[1], &c, 1) == 1);
-       ok1(read_all(c2p[0], buffer, sizeof(buffer)));
-       ok1(memchr(buffer, 0, sizeof(buffer)) == NULL);
+       ok1(read_all(c2p[0], buffer, BUFSZ*2));
+       ok1(memchr(buffer, 0, BUFSZ*2) == NULL);
        ok1(sigcount == 1);
        ok1(wait(&status) == child);
        ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,