Ticket #9638 (closed defect: fixed)
wxCharBuffer doesn't copy wxString::mb_str() return value
| Reported by: | crjjrc | Owned by: | vaclavslavik |
|---|---|---|---|
| Priority: | high | Milestone: | 2.9.0 |
| Component: | base | Version: | 2.9-svn |
| Keywords: | Cc: | net147@… | |
| Blocked By: | Patch: | yes | |
| Blocking: |
Description
The other day I tried running ./myapp -h to get a usage statement from wxCmdLineParser, but no message was printed. I tried other apps and received various results: some statements appeared, others were interrupted by a non-printable character, some even worked okay. Of the samples, only sockets/baseserver exhibited the problem.
I've used gdb to track down the problem to wxMessageOutputStderr::Output. It appears that this line is causing trouble:
const wxWX2MBbuf buf = AppendLineFeedIfNeeded(str).mb_str();
When the problem manifests itself, gdb shows that buf's content is maligned. Perhaps since the temporary variable on the RHS goes out of scope sooner than it should? To test a workaround, I tried changing the code to:
wxString strn = AppendLineFeedIfNeeded(str);
const wxWX2MBbuf buf = strn.mb_str();
const wxWX2MBbuf buf2 = AppendLineFeedIfNeeded(str).mb_str();
Now, gdb shows that buf is valid while buf2 (the original, assigned via a temporary) is not:
(gdb)
wxMessageOutputStderr::Output (this=0x8073da0, str=@0xbfea6b5c)
at ../src/common/msgout.cpp:146
146 const wxWX2MBbuf buf = strn.mb_str();
(gdb) n
147 const wxWX2MBbuf buf2 = AppendLineFeedIfNeeded(str).mb_str();
(gdb)
149 if ( buf ) {
(gdb) print buf
$1 = {<wxCharTypeBuffer<char>> = {
m_str = 0x8074864 "Usage: baseserver [-h] [--verbose] [-t] [-e] [-m <num>] [-p <num>]\n -h, --help \tshow this help message\n --verbose \tgenerate verbose log messages\n -t, --threads \tUse thread based worker"...,
m_owned = false}, <No data fields>}
(gdb) print buf2
$2 = {<wxCharTypeBuffer<char>> = {m_str = 0x8074a24 "",
m_owned = false}, <No data fields>}
I don't why, but it appears like buf2's string is going out of scope and the buffer is getting overwritten. If the temporary's causing the problem, I've attached my patch to create a named string that stays in scope.
$ g++ --version
g++ (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
$ wx-config --version
2.9.0
- Chris

