read_write_all: use calloc instead of huge stack var in tests.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 26 Sep 2010 07:34:03 +0000 (17:04 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 26 Sep 2010 07:34:03 +0000 (17:04 +0930)
Valgrind was giving a spurious error.

ccan/read_write_all/test/run-read_all.c
ccan/read_write_all/test/run-write_all.c

index ee6a980032beba1776cfb9e8752437e19bbd1faa..b8f978ff2b9af246e777bfb3406813234c420c4c 100644 (file)
@@ -26,11 +26,12 @@ static void got_signal(int sig)
 
 int main(int argc, char *argv[])
 {
 
 int main(int argc, char *argv[])
 {
-       char buffer[BUFSZ*2] = { 0 };
+       char *buffer;
        char c = 0;
        int status;
        pid_t child;
 
        char c = 0;
        int status;
        pid_t child;
 
+       buffer = calloc(BUFSZ, 2);
        plan_tests(6);
 
        /* We fork and torture parent. */
        plan_tests(6);
 
        /* We fork and torture parent. */
@@ -44,7 +45,7 @@ int main(int argc, char *argv[])
                /* Child.  Make sure parent ready, then write in two parts. */
                if (read(p2c[0], &c, 1) != 1)
                        exit(1);
                /* 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)
                if (!write_all(c2p[1], buffer, BUFSZ))
                        exit(2);
                if (kill(getppid(), SIGUSR1) != 0)
@@ -63,8 +64,8 @@ int main(int argc, char *argv[])
        close(c2p[1]);
        signal(SIGUSR1, got_signal);
        ok1(write(p2c[1], &c, 1) == 1);
        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,
        ok1(sigcount == 1);
        ok1(wait(&status) == child);
        ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,
index 50bd729c78d7590d3d362ec9a7d39e41a10254e0..5b310688aea6283ec178515c92ed81d2e4474a18 100644 (file)
@@ -24,11 +24,12 @@ static void got_signal(int sig)
 
 int main(int argc, char *argv[])
 {
 
 int main(int argc, char *argv[])
 {
-       char buffer[BUFSZ*2] = { 0 };
+       char *buffer;
        int p2c[2];
        int status;
        pid_t child;
 
        int p2c[2];
        int status;
        pid_t child;
 
+       buffer = calloc(BUFSZ, 1);
        plan_tests(4);
 
        /* We fork and torture parent. */
        plan_tests(4);
 
        /* We fork and torture parent. */
@@ -43,11 +44,11 @@ int main(int argc, char *argv[])
                        exit(1);
                if (kill(getppid(), SIGUSR1) != 0)
                        exit(2);
                        exit(1);
                if (kill(getppid(), SIGUSR1) != 0)
                        exit(2);
-               if (!read_all(p2c[0], buffer+1, sizeof(buffer)-1))
+               if (!read_all(p2c[0], buffer+1, BUFSZ-1))
                        exit(3);
                        exit(3);
-               if (memchr(buffer, 0, sizeof(buffer))) {
+               if (memchr(buffer, 0, BUFSZ)) {
                        fprintf(stderr, "buffer has 0 at offset %ti\n",
                        fprintf(stderr, "buffer has 0 at offset %ti\n",
-                               memchr(buffer, 0, sizeof(buffer)) - (void *)buffer);
+                               memchr(buffer, 0, BUFSZ) - (void *)buffer);
                        exit(4);
                }
                exit(0);
                        exit(4);
                }
                exit(0);
@@ -56,9 +57,9 @@ int main(int argc, char *argv[])
                err(1, "forking");
 
        close(p2c[0]);
                err(1, "forking");
 
        close(p2c[0]);
-       memset(buffer, 0xff, sizeof(buffer));
+       memset(buffer, 0xff, BUFSZ);
        signal(SIGUSR1, got_signal);
        signal(SIGUSR1, got_signal);
-       ok1(write_all(p2c[1], buffer, sizeof(buffer)));
+       ok1(write_all(p2c[1], buffer, BUFSZ));
        ok1(sigcount == 1);
        ok1(wait(&status) == child);
        ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,
        ok1(sigcount == 1);
        ok1(wait(&status) == child);
        ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,