From 947b2ebaee2a4a6e5b59ec90856dd66eae63b3f2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 9 Jun 2013 02:17:32 +0000 Subject: Fix crashes when comparing variable sized Atoms. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@5135 a436a847-0d15-0410-975c-d299462d15a1 --- ingen/Atom.hpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ingen/Atom.hpp b/ingen/Atom.hpp index a355f840..7a3e5797 100644 --- a/ingen/Atom.hpp +++ b/ingen/Atom.hpp @@ -17,6 +17,7 @@ #ifndef INGEN_ATOM_HPP #define INGEN_ATOM_HPP +#include #include #include #include @@ -89,12 +90,13 @@ public: } inline bool operator==(const Atom& other) const { - if (is_reference()) { - return !memcmp(_body.ptr, other._body.ptr, sizeof(LV2_Atom) + _atom.size); + if (_atom.type != other._atom.type || + _atom.size != other._atom.size) { + return false; } - return (_atom.type == other._atom.type && - _atom.size == other._atom.size && - _body.val == other._body.val); + return is_reference() + ? !memcmp(_body.ptr, other._body.ptr, sizeof(LV2_Atom) + _atom.size) + : _body.val == other._body.val; } inline bool operator!=(const Atom& other) const { @@ -103,11 +105,11 @@ public: inline bool operator<(const Atom& other) const { if (_atom.type == other._atom.type) { - if (is_reference()) { - return memcmp(_body.ptr, other._body.ptr, _atom.size) < 0; - } else { - return memcmp(&_body.val, &other._body.val, _atom.size) < 0; - } + const uint32_t min_size = std::min(_atom.size, other._atom.size); + const int cmp = is_reference() + ? memcmp(_body.ptr, other._body.ptr, min_size) + : memcmp(&_body.val, &other._body.val, min_size); + return cmp < 0 || (cmp == 0 && _atom.size < other._atom.size); } return type() < other.type(); } -- cgit v1.2.1