Repository: qpid-proton Updated Branches: refs/heads/master 979ce2c45 -> b57e6ea7d
PROTON-1225: c++: taking address of element 0 of an empty string or vector Added x.empty() checks and assertions to all uses of the &x[0] idiom. Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/b57e6ea7 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/b57e6ea7 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/b57e6ea7 Branch: refs/heads/master Commit: b57e6ea7d7318caf83a36648164637ae2d3afa66 Parents: 979ce2c Author: Alan Conway <[email protected]> Authored: Thu Jun 2 14:56:57 2016 -0400 Committer: Alan Conway <[email protected]> Committed: Thu Jun 2 15:23:39 2016 -0400 ---------------------------------------------------------------------- proton-c/bindings/cpp/src/encoder.cpp | 5 ++++- proton-c/bindings/cpp/src/message.cpp | 7 +++++++ proton-c/bindings/cpp/src/sender.cpp | 3 +++ proton-c/bindings/cpp/src/types_internal.hpp | 4 ++-- 4 files changed, 16 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b57e6ea7/proton-c/bindings/cpp/src/encoder.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/encoder.cpp b/proton-c/bindings/cpp/src/encoder.cpp index d809cd9..a433091 100644 --- a/proton-c/bindings/cpp/src/encoder.cpp +++ b/proton-c/bindings/cpp/src/encoder.cpp @@ -35,6 +35,7 @@ #include <proton/codec.h> #include <algorithm> +#include <assert.h> namespace proton { namespace codec { @@ -68,9 +69,11 @@ bool encoder::encode(char* buffer, size_t& size) { void encoder::encode(std::string& s) { s.resize(std::max(s.capacity(), size_t(1))); // Use full capacity, ensure not empty - size_t size = s.size(); + size_t size = s.size(); + assert(!s.empty()); if (!encode(&s[0], size)) { s.resize(size); + assert(!s.empty()); encode(&s[0], size); } } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b57e6ea7/proton-c/bindings/cpp/src/message.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/message.cpp b/proton-c/bindings/cpp/src/message.cpp index 172da7b..44565b3 100644 --- a/proton-c/bindings/cpp/src/message.cpp +++ b/proton-c/bindings/cpp/src/message.cpp @@ -269,6 +269,7 @@ void message::encode(std::vector<char> &s) const { size_t sz = std::max(s.capacity(), size_t(512)); while (true) { s.resize(sz); + assert(!s.empty()); int err = pn_message_encode(pn_msg(), const_cast<char*>(&s[0]), &sz); if (err) { if (err != PN_OVERFLOW) @@ -288,16 +289,22 @@ std::vector<char> message::encode() const { } void message::decode(const std::vector<char> &s) { + if (s.empty()) + throw error("message decode: no data"); application_properties_.clear(); message_annotations_.clear(); delivery_annotations_.clear(); + assert(!s.empty()); check(pn_message_decode(pn_msg(), &s[0], s.size())); } void message::decode(proton::delivery delivery) { std::vector<char> buf; buf.resize(pn_delivery_pending(unwrap(delivery))); + if (buf.empty()) + throw error("message decode: no delivery pending on link"); proton::receiver link = delivery.receiver(); + assert(!buf.empty()); ssize_t n = pn_link_recv(unwrap(link), const_cast<char *>(&buf[0]), buf.size()); if (n != ssize_t(buf.size())) throw error(MSG("receiver read failure")); clear(); http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b57e6ea7/proton-c/bindings/cpp/src/sender.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/sender.cpp b/proton-c/bindings/cpp/src/sender.cpp index 380005f..f7ec686 100644 --- a/proton-c/bindings/cpp/src/sender.cpp +++ b/proton-c/bindings/cpp/src/sender.cpp @@ -30,6 +30,8 @@ #include "proton_bits.hpp" #include "contexts.hpp" +#include <assert.h> + namespace proton { sender::sender(pn_link_t *l): link(make_wrapper(l)) {} @@ -62,6 +64,7 @@ tracker sender::send(const message &message) { pn_delivery(pn_object(), pn_dtag(reinterpret_cast<const char*>(&id), sizeof(id))); std::vector<char> buf; message.encode(buf); + assert(!buf.empty()); pn_link_send(pn_object(), &buf[0], buf.size()); pn_link_advance(pn_object()); if (pn_link_snd_settle_mode(pn_object()) == PN_SND_SETTLED) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b57e6ea7/proton-c/bindings/cpp/src/types_internal.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/types_internal.hpp b/proton-c/bindings/cpp/src/types_internal.hpp index ea8b6ef..484dfc4 100644 --- a/proton-c/bindings/cpp/src/types_internal.hpp +++ b/proton-c/bindings/cpp/src/types_internal.hpp @@ -46,12 +46,12 @@ make_conversion_error(type_id want, type_id got, const std::string& msg=std::str /// Convert std::string to pn_bytes_t inline pn_bytes_t pn_bytes(const std::string& s) { - pn_bytes_t b = { s.size(), const_cast<char*>(&s[0]) }; + pn_bytes_t b = { s.size(), s.empty() ? 0 : const_cast<char*>(&s[0]) }; return b; } inline pn_bytes_t pn_bytes(const binary& s) { - pn_bytes_t b = { s.size(), reinterpret_cast<const char*>(&s[0]) }; + pn_bytes_t b = { s.size(), s.empty() ? 0 : reinterpret_cast<const char*>(&s[0]) }; return b; } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
