Commit 273b5471 authored by Brian Silverman's avatar Brian Silverman

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.
parent ffe4a1c6
...@@ -70,7 +70,8 @@ int zmq::dealer_t::xsetsockopt (int option_, const void *optval_, ...@@ -70,7 +70,8 @@ int zmq::dealer_t::xsetsockopt (int option_, const void *optval_,
size_t optvallen_) size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); 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_) { switch (option_) {
case ZMQ_PROBE_ROUTER: case ZMQ_PROBE_ROUTER:
......
...@@ -92,7 +92,8 @@ int zmq::options_t::setsockopt (int option_, const void *optval_, ...@@ -92,7 +92,8 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
size_t optvallen_) size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); 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) #if defined (ZMQ_ACT_MILITANT)
bool malformed = true; // Did caller pass a bad option value? bool malformed = true; // Did caller pass a bad option value?
#endif #endif
......
...@@ -204,7 +204,8 @@ bool zmq::req_t::xhas_out () ...@@ -204,7 +204,8 @@ bool zmq::req_t::xhas_out ()
int zmq::req_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_) int zmq::req_t::xsetsockopt (int option_, const void *optval_, size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); 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_) { switch (option_) {
case ZMQ_REQ_CORRELATE: case ZMQ_REQ_CORRELATE:
......
...@@ -97,7 +97,8 @@ int zmq::router_t::xsetsockopt (int option_, const void *optval_, ...@@ -97,7 +97,8 @@ int zmq::router_t::xsetsockopt (int option_, const void *optval_,
size_t optvallen_) size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); 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_) { switch (option_) {
case ZMQ_CONNECT_RID: case ZMQ_CONNECT_RID:
......
...@@ -178,7 +178,8 @@ int zmq::stream_t::xsetsockopt (int option_, const void *optval_, ...@@ -178,7 +178,8 @@ int zmq::stream_t::xsetsockopt (int option_, const void *optval_,
size_t optvallen_) size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); 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_) { switch (option_) {
case ZMQ_CONNECT_RID: case ZMQ_CONNECT_RID:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment