diff options
| author | Gordon Sim <gsim@apache.org> | 2011-04-14 13:16:03 +0000 |
|---|---|---|
| committer | Gordon Sim <gsim@apache.org> | 2011-04-14 13:16:03 +0000 |
| commit | 006eaf324314b8314f0969ef58a9a4f08483ae84 (patch) | |
| tree | 9c61d2d95cf1d4ca08be74da32189b164ea2e647 /cpp/src/qpid | |
| parent | 4a62fe7fccd348b887f68bf782e8d56e89c964c6 (diff) | |
| download | qpid-python-006eaf324314b8314f0969ef58a9a4f08483ae84.tar.gz | |
QPID-3206: added special cases to catch negative numeric string conversions to unsigned values
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1092219 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid')
| -rw-r--r-- | cpp/src/qpid/types/Variant.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/cpp/src/qpid/types/Variant.cpp b/cpp/src/qpid/types/Variant.cpp index 9cc3cfe5fc..8d1a439631 100644 --- a/cpp/src/qpid/types/Variant.cpp +++ b/cpp/src/qpid/types/Variant.cpp @@ -111,11 +111,24 @@ class VariantImpl template<class T> T convertFromString() const { std::string* s = reinterpret_cast<std::string*>(value.v); - try { - return boost::lexical_cast<T>(*s); - } catch(const boost::bad_lexical_cast&) { - throw InvalidConversion(QPID_MSG("Cannot convert " << *s)); + if (std::numeric_limits<T>::is_signed || s->find('-') != 0) { + //lexical_cast won't fail if string is a negative number and T is unsigned + try { + return boost::lexical_cast<T>(*s); + } catch(const boost::bad_lexical_cast&) { + //don't return, throw exception below + } + } else { + //T is unsigned and number starts with '-' + try { + //handle special case of negative zero + if (boost::lexical_cast<int>(*s) == 0) return 0; + //else its a non-zero negative number so throw exception at end of function + } catch(const boost::bad_lexical_cast&) { + //wasn't a valid int, therefore not a valid uint + } } + throw InvalidConversion(QPID_MSG("Cannot convert " << *s)); } }; |
