From f1e6af089adaa0b30defd9ec42a60510880f1469 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 21 Feb 2021 19:05:13 +0100 Subject: [PATCH] Fix parsing unsigned numbers in wxIntegerValidatorBase Use wxNumberFormatter::FromString() overload taking wxULongLong_t to allow parsing numbers greater than LLONG_MAX. --- include/wx/valnum.h | 2 +- src/common/valnum.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/wx/valnum.h b/include/wx/valnum.h index c7858dec13..76386debf4 100644 --- a/include/wx/valnum.h +++ b/include/wx/valnum.h @@ -303,7 +303,7 @@ protected: // Provide methods for wxNumValidator use. wxString ToString(LongestValueType value) const; - static bool FromString(const wxString& s, LongestValueType *value); + bool FromString(const wxString& s, LongestValueType *value) const; void DoSetMin(LongestValueType min) { m_min = min; } LongestValueType DoGetMin() const { return m_min; } diff --git a/src/common/valnum.cpp b/src/common/valnum.cpp index 8704e72fce..b4adef753b 100644 --- a/src/common/valnum.cpp +++ b/src/common/valnum.cpp @@ -234,9 +234,29 @@ wxString wxIntegerValidatorBase::ToString(LongestValueType value) const } bool -wxIntegerValidatorBase::FromString(const wxString& s, LongestValueType *value) +wxIntegerValidatorBase::FromString(const wxString& s, + LongestValueType *value) const { - return wxNumberFormatter::FromString(s, value); + if ( CanBeNegative() ) + { + return wxNumberFormatter::FromString(s, value); + } + else + { + // Parse as unsigned to ensure we don't accept minus sign here. +#ifdef wxULongLong_t + wxULongLong_t uvalue; +#else + unsigned long uvalue; +#endif + if ( !wxNumberFormatter::FromString(s, &uvalue) ) + return false; + + // This cast is lossless. + *value = static_cast(uvalue); + + return true; + } } bool