altstack: Use ptrint instead of bare casts
authorDavid Gibson <david@gibson.dropbear.id.au>
Fri, 3 Jun 2016 08:42:01 +0000 (18:42 +1000)
committerDan Good <dan@dancancode.com>
Thu, 16 Jun 2016 20:18:52 +0000 (20:18 +0000)
Functions invoked with altstack take a void * parameter.  However, the
test program wants to pass an integer, and so uses the trick of casting
the integer values to (void *) and back again.

The ptrint() module handles exactly this case in a more portable and
(somewhat) typesafe way, so use that instead.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/altstack/_info
ccan/altstack/test/run.c

index 2713bd0e1534ef42a36e6ea8d0f6ffda51ab3aab..7accf8648108841b7f42e0fb9f97e8d5a9e919e6 100644 (file)
@@ -122,6 +122,11 @@ int main(int argc, char *argv[])
        if (strcmp(argv[1], "depends") == 0)
                return 0;
 
+       if (strcmp(argv[1], "testdepends") == 0) {
+               printf("ccan/ptrint\n");
+               return 0;
+       }
+
        if (strcmp(argv[1], "ported") == 0) {
 #ifdef __x86_64__
                printf("\n");
index 23dd2e93ef69e83be1312b01da37f88b86d7ea85..61710fda3b9c8dbddab5318ad55caf8aa7d023f6 100644 (file)
@@ -8,6 +8,7 @@
 #include <unistd.h>
 #include <sys/mman.h>
 #include <ccan/tap/tap.h>
+#include <ccan/ptrint/ptrint.h>
 #include <ccan/altstack/altstack.h>
 #include <stdio.h>
 
@@ -53,7 +54,7 @@ static void __attribute__((optimize("O0"))) dn(unsigned long i)
 }
 static void *wrap(void *i)
 {
-       dn((unsigned long) i);
+       dn(ptr2int(i));
        return wrap;
 }
 
@@ -86,43 +87,43 @@ int main(void)
 
        plan_tests(50);
 
-       chkfail(getrlimit_,     altstack(8*MiB, wrap, 0, 0) == -1, e(getrlimit_),
+       chkfail(getrlimit_,     altstack(8*MiB, wrap, int2ptr(0), 0) == -1, e(getrlimit_),
                0,
                0);
 
-       chkfail(setrlimit_,     altstack(8*MiB, wrap, 0, 0) == -1, e(setrlimit_),
+       chkfail(setrlimit_,     altstack(8*MiB, wrap, int2ptr(0), 0) == -1, e(setrlimit_),
                getrlimit_,
                0);
 
-       chkfail(mmap_,          altstack(8*MiB, wrap, 0, 0) == -1, e(mmap_),
+       chkfail(mmap_,          altstack(8*MiB, wrap, int2ptr(0), 0) == -1, e(mmap_),
                getrlimit_|setrlimit_,
                setrlimit_);
 
-       chkfail(sigaltstack_,   altstack(8*MiB, wrap, 0, 0) == -1, e(sigaltstack_),
+       chkfail(sigaltstack_,   altstack(8*MiB, wrap, int2ptr(0), 0) == -1, e(sigaltstack_),
                getrlimit_|setrlimit_|mmap_,
                setrlimit_|munmap_);
 
-       chkfail(sigaction_,     altstack(8*MiB, wrap, 0, 0) == -1, e(sigaction_),
+       chkfail(sigaction_,     altstack(8*MiB, wrap, int2ptr(0), 0) == -1, e(sigaction_),
                getrlimit_|setrlimit_|mmap_|sigaltstack_,
                setrlimit_|munmap_|sigaltstack_);
 
-       chkfail(munmap_,        altstack(8*MiB, wrap, 0, 0) ==  1, e(munmap_),
+       chkfail(munmap_,        altstack(8*MiB, wrap, int2ptr(0), 0) ==  1, e(munmap_),
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_,
                setrlimit_|sigaltstack_|sigaction_);
        if (fail = 0, munmap(m_, msz_) == -1)
                err(1, "munmap");
 
-       chkok(                  altstack(1*MiB, wrap, (void *) 1000000, 0) == -1, EOVERFLOW,
+       chkok(                  altstack(1*MiB, wrap, int2ptr(1000000), 0) == -1, EOVERFLOW,
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_,
                setrlimit_|munmap_|sigaltstack_|sigaction_);
 
        // be sure segv catch is repeatable (SA_NODEFER)
-       chkok(                  altstack(1*MiB, wrap, (void *) 1000000, 0) == -1, EOVERFLOW,
+       chkok(                  altstack(1*MiB, wrap, int2ptr(1000000), 0) == -1, EOVERFLOW,
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_,
                setrlimit_|munmap_|sigaltstack_|sigaction_);
 
        used = 1;
-       chkfail(munmap_,        altstack(1*MiB, wrap, (void *) 1000000, 0) == -1, EOVERFLOW,
+       chkfail(munmap_,        altstack(1*MiB, wrap, int2ptr(1000000), 0) == -1, EOVERFLOW,
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_,
                setrlimit_|sigaltstack_|sigaction_);
        if (fail = 0, munmap(m_, msz_) == -1)
@@ -149,7 +150,7 @@ int main(void)
        ok1(strcmp(buf, estr "\n") == 0);
 
        used = 1;
-       chkok(                  altstack(8*MiB, wrap, (void *) 1000000, 0) == -1, EOVERFLOW,
+       chkok(                  altstack(8*MiB, wrap, int2ptr(1000000), 0) == -1, EOVERFLOW,
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_,
                setrlimit_|munmap_|sigaltstack_|sigaction_);
 
@@ -157,7 +158,7 @@ int main(void)
        ok1(used >= 8*MiB - pgsz && used <= 8*MiB + pgsz);
 
        used = 0;
-       chkok(                  altstack(8*MiB, wrap, (void *) 100000, 0) == 0, 0,
+       chkok(                  altstack(8*MiB, wrap, int2ptr(100000), 0) == 0, 0,
                getrlimit_|setrlimit_|mmap_|sigaltstack_|sigaction_|munmap_,
                setrlimit_|munmap_|sigaltstack_|sigaction_);