diff --git a/src/options.cpp b/src/options.cpp index a5c61f24..3edc5fcd 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -761,7 +761,8 @@ int zmq::options_t::setsockopt (int option_, case ZMQ_METADATA: if (optvallen_ > 0 && !is_int) { - const std::string s (static_cast (optval_)); + const std::string s (static_cast (optval_), + optvallen_); const size_t pos = s.find (':'); if (pos != std::string::npos && pos != 0 && pos != s.length () - 1) { diff --git a/tests/test_socket_options_fuzzer.cpp b/tests/test_socket_options_fuzzer.cpp index dd9ee8f7..adbac6c4 100644 --- a/tests/test_socket_options_fuzzer.cpp +++ b/tests/test_socket_options_fuzzer.cpp @@ -31,31 +31,36 @@ #include #endif -#include -#include - #include "testutil.hpp" #include "testutil_unity.hpp" +#ifdef ZMQ_DISCONNECT_MSG +#define LAST_OPTION ZMQ_DISCONNECT_MSG +#else +#define LAST_OPTION ZMQ_BINDTODEVICE +#endif + extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { - setup_test_context (); - void *s = test_context_socket (ZMQ_XPUB); int option; + void *ctx = zmq_ctx_new (); + TEST_ASSERT_NOT_NULL (ctx); + void *server = zmq_socket (ctx, ZMQ_XPUB); + TEST_ASSERT_NOT_NULL (server); - // A lot of options expect a well-formatted string - ((uint8_t *)data)[size - 1] = 0; + if (!size) + return 0; - for (option = ZMQ_AFFINITY; option < ZMQ_BINDTODEVICE + 1; ++option) { - uint8_t out[512]; - size_t out_size = 512; + for (option = ZMQ_AFFINITY; option <= LAST_OPTION; ++option) { + uint8_t out[8192]; + size_t out_size = 8192; - zmq_setsockopt(s, option, data, size); - zmq_getsockopt(s, option, out, &out_size); + zmq_setsockopt (server, option, data, size); + zmq_getsockopt (server, option, out, &out_size); } - test_context_socket_close_zero_linger (s); - teardown_test_context (); + zmq_close (server); + zmq_ctx_term (ctx); return 0; } @@ -66,8 +71,8 @@ void test_socket_options_fuzzer () uint8_t **data; size_t *len, num_cases = 0; if (fuzzer_corpus_encode ( - "tests/libzmq-fuzz-corpora/test_socket_options_fuzzer_seed_corpus", &data, - &len, &num_cases) + "tests/libzmq-fuzz-corpora/test_socket_options_fuzzer_seed_corpus", + &data, &len, &num_cases) != 0) exit (77);