From 632997e449b7b7192287f777cb40819ca0339d09 Mon Sep 17 00:00:00 2001 From: Laurent Poujoulat Date: Fri, 2 Dec 2016 20:47:43 +0100 Subject: [PATCH] Fix parsing RGBA string in wxColour::FromString() Using "%20c" in scanf() doesn't work unless we really have exactly 20 characters, so use "%20[^)]" to take up to 20 characters until the closing parenthesis instead. Closes #17739. --- docs/changes.txt | 1 + src/common/colourcmn.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index daeca82066..d0414db3df 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -117,6 +117,7 @@ All (GUI): - Fix position of the rectangle returned by wxDataViewCtrl::GetItemRect(). - Add wxDataViewRenderer::GetAccessibleDescription(). - Improve wxImage::Scale() handling of pixels with alpha channel (Tim Kosse). +- Fix parsing of RGBA strings in wxColour (Laurent Poujoulat). wxGTK: diff --git a/src/common/colourcmn.cpp b/src/common/colourcmn.cpp index 805eb0202d..49b7c0c6b0 100644 --- a/src/common/colourcmn.cpp +++ b/src/common/colourcmn.cpp @@ -105,8 +105,8 @@ bool wxColourBase::FromString(const wxString& str) // use point as decimal separator, regardless of locale. So parse // the tail of the string manually by putting it in a buffer and // using wxString::ToCDouble() below. Notice that we can't use "%s" - // for this as it stops at white space and we need "%c" to avoid - // this and really get all the rest of the string into the buffer. + // for this as it stops at white space, so we use "[^)] )" to take + // everything until the closing bracket. const unsigned len = str.length(); // always big enough wxCharBuffer alphaBuf(len); @@ -118,7 +118,7 @@ bool wxColourBase::FromString(const wxString& str) // Construct the format string which ensures that the last argument // receives all the rest of the string. wxString formatStr; - formatStr << wxS("( %d , %d , %d , %") << len << 'c'; + formatStr << wxS("( %d , %d , %d , %") << len << wxS("[^)] )"); // Notice that we use sscanf() here because if the string is not // ASCII it can't represent a valid RGB colour specification anyhow @@ -134,10 +134,9 @@ bool wxColourBase::FromString(const wxString& str) // Notice that we must explicitly specify the length to get rid of // trailing NULs. wxString alphaStr(alphaPtr, wxStrlen(alphaPtr)); - if ( alphaStr.empty() || alphaStr.Last() != ')' ) + if ( alphaStr.empty() ) return false; - alphaStr.RemoveLast(); alphaStr.Trim(); double a;