From fd06aba05381055ab56e1ec81d56055b66462f0b Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 26 Apr 2018 15:36:13 -0700 Subject: fixes #375 integer types are error prone This change converts the various integer types like nng_socket in the public API to opaque structures that are passed by value. Basically we just wrap the integer ID. This "hack" give us strong type checks by the compiler (yay!), at the expense of not being able to directly use these as numbers (so comparisions for example don't work, and neither does initialization to zero using the normal method. Comparison of disassembly output shows that at least with the optimizer enabled there is no difference in the compiler output between using a structure or an integral value. --- tests/pair1.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'tests/pair1.c') diff --git a/tests/pair1.c b/tests/pair1.c index ef3fe263..8a27bc6e 100644 --- a/tests/pair1.c +++ b/tests/pair1.c @@ -28,13 +28,14 @@ TestMain("PAIRv1 protocol", { const char * templ = "inproc://pairv1/%u"; char addr[NNG_MAXADDRLEN + 1]; - nng_socket s1 = 0; - nng_socket c1 = 0; - nng_socket c2 = 0; + nng_socket s1; + nng_socket c1; + nng_socket c2; nng_duration tmo; uint32_t v; size_t sz; + s1.id = c1.id = c2.id = 0; atexit(nng_fini); Convey("Given a few sockets", { @@ -210,7 +211,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1 != 0); + So(p1.id != 0); nng_msg_free(msg); So(nng_msg_alloc(&msg, 0) == 0); @@ -219,10 +220,10 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "TWO"); p2 = nng_msg_get_pipe(msg); - So(p2 != 0); + So(p2.id != 0); nng_msg_free(msg); - So(p1 != p2); + So(p1.id != p2.id); So(nng_msg_alloc(&msg, 0) == 0); @@ -307,13 +308,15 @@ TestMain("PAIRv1 protocol", { nng_msleep(20); Convey("Send/recv work", { + nng_pipe p; So(nng_msg_alloc(&msg, 0) == 0); APPENDSTR(msg, "GAMMA"); So(nng_msg_header_append_u32(msg, 1) == 0); So(nng_msg_header_len(msg) == sizeof(uint32_t)); So(nng_sendmsg(c1, msg, 0) == 0); So(nng_recvmsg(s1, &msg, 0) == 0); - So(nng_msg_get_pipe(msg) != 0); + p = nng_msg_get_pipe(msg); + So(p.id != 0); CHECKSTR(msg, "GAMMA"); So(nng_msg_header_len(msg) == sizeof(uint32_t)); So(nng_msg_header_trim_u32(msg, &hops) == 0); @@ -328,7 +331,8 @@ TestMain("PAIRv1 protocol", { CHECKSTR(msg, "EPSILON"); So(nng_msg_header_len(msg) == sizeof(uint32_t)); So(nng_msg_header_trim_u32(msg, &hops) == 0); - So(nng_msg_get_pipe(msg) != 0); + p = nng_msg_get_pipe(msg); + So(p.id != 0); So(hops == 2); nng_msg_free(msg); }); @@ -470,7 +474,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1 != 0); + So(p1.id != 0); So(nng_msg_header_trim_u32(msg, &hops) == 0); So(hops == 1); nng_msg_free(msg); @@ -481,12 +485,12 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "TWO"); p2 = nng_msg_get_pipe(msg); - So(p2 != 0); + So(p2.id != 0); So(nng_msg_header_trim_u32(msg, &hops) == 0); So(hops == 1); nng_msg_free(msg); - So(p1 != p2); + So(p1.id != p2.id); So(nng_msg_alloc(&msg, 0) == 0); nng_msg_set_pipe(msg, p1); @@ -514,7 +518,7 @@ TestMain("PAIRv1 protocol", { So(nng_recvmsg(s1, &msg, 0) == 0); CHECKSTR(msg, "ONE"); p1 = nng_msg_get_pipe(msg); - So(p1 != 0); + So(p1.id != 0); nng_msg_free(msg); nng_close(c1); -- cgit v1.2.3-70-g09d2