subsurface/qt-ui
Alberto Mardegan 5e0a3cdad8 Fix usage of temporary QByteArrays
This commit fixes three different things:
 - a memory leak in WeightModel::setData()
 - getSetting() calling strdup() on a QByteArray
 - a possible usage of memory after deallocation

Here's an explanation of the last issue (taken from the mailing list, slightly
adapted):

toByteArray(), as well as others "toSomething()" methods, returns
a new object which the compiler allocates on the stack.  The compiler
will consider it a temporary data, and destroy it on the next line.  So,
when one does

	char *text= value.toByteArray().data(); // line 1
	if (strcmp(description, text)) {        // line 2

the compiler creates a QByteArray on line 1, calls ::data() on it, which
returns a valid char *, and assigns its value to "text".  So far, so
good.  But before jumping to line 2, the compiler destroys the temporary
QByteArray, and this will in turn invoke the QByteArray destructor,
which will destroy the internal data. The result is that on line 2,
"text" will point to some memory which has already been freed.

One solution is to store a copy of the temporary QByteArray into a local
variable: the compiler will still destroy the temporary QByteArray it created,
but (thanks to the reference-counted data sharing built in QByteArray) now the
destructor will see that the data is referenced by another instance of
QByteArray (the local variable "ba") and will not free the internal data.
In this way, the internal data will be available until the local variable is
destroyed, which will happen at the end of the {} block where it is defined.

Please note that when one uses the data in the same line, one doesn't need to
worry about this issue. In fact,

  text = strdup(value.toString().toUtf8().data());

works just fine.

Signed-off-by: Alberto Mardegan <mardy@users.sourceforge.net>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-05-24 06:18:27 -07:00
..
marble Work around broken Marble on Debian 2013-05-18 09:49:37 -07:00
divelistview.cpp The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
divelistview.h Added code for handling of show / hide of columns. 2013-05-21 16:51:49 -03:00
downloadfromdivecomputer.cpp Implement default dive computer and device 2013-05-22 23:24:33 -07:00
downloadfromdivecomputer.h The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
downloadfromdivecomputer.ui Implement rudimentary divecomputer download 2013-05-20 21:55:56 -07:00
globe.cpp The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
globe.h The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
kmessagewidget.cpp Fixed the 'click in close doesn't close' on KMessageWidget. 2013-05-22 08:10:02 -03:00
kmessagewidget.h Compile fix 2013-05-17 22:27:50 -07:00
maintab.cpp Improve wording on buttons when editing a dive 2013-05-24 06:15:28 -07:00
maintab.h Remove obsolete slots 2013-05-23 18:35:04 -07:00
maintab.ui Improve wording on buttons when editing a dive 2013-05-24 06:15:28 -07:00
mainwindow.cpp Improve wording in askSaveChanges() 2013-05-24 06:16:10 -07:00
mainwindow.h Replace GError handling with a kMessageWidget based approach 2013-05-21 23:39:49 -07:00
mainwindow.ui Replace GError handling with a kMessageWidget based approach 2013-05-21 23:39:49 -07:00
modeldelegates.cpp Correctly add new weight / cylinder types, even when hitting 'tab' 2013-05-23 23:26:17 -07:00
modeldelegates.h Add weightsystem delegate to enable editing of weightsystem 2013-05-23 21:36:33 -07:00
models.cpp Fix usage of temporary QByteArrays 2013-05-24 06:18:27 -07:00
models.h Add weightsystem delegate to enable editing of weightsystem 2013-05-23 21:36:33 -07:00
plotareascene.cpp Add Qtr_ macros that uses gettext in a tr() compatible manner 2013-04-13 20:44:02 -07:00
plotareascene.h Add Qtr_ macros that uses gettext in a tr() compatible manner 2013-04-13 20:44:02 -07:00
profilegraphics.cpp The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
profilegraphics.h The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
starwidget.cpp The never ending, futile fight for whitespace consistency 2013-05-22 21:31:45 -07:00
starwidget.h Added option to edit the selected dive. 2013-05-18 17:06:48 -07:00