diff --git a/docs/changes.txt b/docs/changes.txt index fd699e54be..5436bc93f3 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -579,6 +579,7 @@ Major new features in this release All: +- Fix silent data loss in wx[F]File::Write(wxString) if conversion fails. - Make wxString::FromCDouble() work when the global C++ locale is not the C one. wxMSW: diff --git a/src/common/ffile.cpp b/src/common/ffile.cpp index 64dc25e3f1..962242ebe8 100644 --- a/src/common/ffile.cpp +++ b/src/common/ffile.cpp @@ -153,17 +153,28 @@ size_t wxFFile::Write(const void *pBuf, size_t nCount) bool wxFFile::Write(const wxString& s, const wxMBConv& conv) { - const wxWX2MBbuf buf = s.mb_str(conv); - if ( !buf ) - return false; + // Writing nothing always succeeds -- and simplifies the check for + // conversion failure below. + if ( s.empty() ) + return true; + + const wxWX2MBbuf buf = s.mb_str(conv); #if wxUSE_UNICODE - const size_t size = buf.length(); + const size_t size = buf.length(); + + if ( !size ) + { + // This means that the conversion failed as the original string wasn't + // empty (we explicitly checked for this above) and in this case we + // must fail too to indicate that we can't save the data. + return false; + } #else - const size_t size = s.length(); + const size_t size = s.length(); #endif - return Write(buf, size) == size; + return Write(buf, size) == size; } bool wxFFile::Flush() diff --git a/src/common/file.cpp b/src/common/file.cpp index b2a5aca736..9d5571d5ef 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -356,17 +356,28 @@ size_t wxFile::Write(const void *pBuf, size_t nCount) bool wxFile::Write(const wxString& s, const wxMBConv& conv) { - const wxWX2MBbuf buf = s.mb_str(conv); - if ( !buf ) - return false; + // Writing nothing always succeeds -- and simplifies the check for + // conversion failure below. + if ( s.empty() ) + return true; + + const wxWX2MBbuf buf = s.mb_str(conv); #if wxUSE_UNICODE - const size_t size = buf.length(); + const size_t size = buf.length(); + + if ( !size ) + { + // This means that the conversion failed as the original string wasn't + // empty (we explicitly checked for this above) and in this case we + // must fail too to indicate that we can't save the data. + return false; + } #else - const size_t size = s.length(); + const size_t size = s.length(); #endif - return Write(buf, size) == size; + return Write(buf, size) == size; } // flush