From 273b54715e3b0ed0da67df6d863ac44293239640 Mon Sep 17 00:00:00 2001 From: Brian Silverman Date: Sat, 6 Feb 2016 22:20:42 -0500 Subject: [PATCH] Use memcpy instead of assuming option values are aligned Otherwise, it's undefined behavior. ubsan catches alignment issues in the libzmq test suite without this. --- src/dealer.cpp | 3 ++- src/options.cpp | 3 ++- src/req.cpp | 3 ++- src/router.cpp | 3 ++- src/stream.cpp | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/dealer.cpp b/src/dealer.cpp index 3d7b62b8..ce182224 100644 --- a/src/dealer.cpp +++ b/src/dealer.cpp @@ -70,7 +70,8 @@ int zmq::dealer_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_) { bool is_int = (optvallen_ == sizeof (int)); - int value = is_int? *((int *) optval_): 0; + int value = 0; + if (is_int) memcpy(&value, optval_, sizeof (int)); switch (option_) { case ZMQ_PROBE_ROUTER: diff --git a/src/options.cpp b/src/options.cpp index 1acabae1..3f8fa639 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -92,7 +92,8 @@ int zmq::options_t::setsockopt (int option_, const void *optval_, size_t optvallen_) { bool is_int = (optvallen_ == sizeof (int)); - int value = is_int? *((int *) optval_): 0; + int value = 0; + if (is_int) memcpy(&value, optval_, sizeof (int)); #if defined (ZMQ_ACT_MILITANT) bool malformed = true; // Did caller pass a bad option value? #endif diff --git a/src/req.cpp b/src/req.cpp index 874a80ad..ab10c072 100644 --- a/src/req.cpp +++ b/src/req.cpp @@ -204,7 +204,8 @@ bool zmq::req_t::xhas_out () int zmq::req_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_) { bool is_int = (optvallen_ == sizeof (int)); - int value = is_int? *((int *) optval_): 0; + int value = 0; + if (is_int) memcpy(&value, optval_, sizeof (int)); switch (option_) { case ZMQ_REQ_CORRELATE: diff --git a/src/router.cpp b/src/router.cpp index 64a16628..1beba58d 100644 --- a/src/router.cpp +++ b/src/router.cpp @@ -97,7 +97,8 @@ int zmq::router_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_) { bool is_int = (optvallen_ == sizeof (int)); - int value = is_int? *((int *) optval_): 0; + int value = 0; + if (is_int) memcpy(&value, optval_, sizeof (int)); switch (option_) { case ZMQ_CONNECT_RID: diff --git a/src/stream.cpp b/src/stream.cpp index e63ca41d..192bd36d 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -178,7 +178,8 @@ int zmq::stream_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_) { bool is_int = (optvallen_ == sizeof (int)); - int value = is_int? *((int *) optval_): 0; + int value = 0; + if (is_int) memcpy(&value, optval_, sizeof (int)); switch (option_) { case ZMQ_CONNECT_RID: