Dangerous KConfigSkeleton and KMKernel::config() interaction

David Faure faure at kde.org
Thu Aug 12 13:23:05 BST 2004

There is a hidden danger in using KConfigXT in an application which also uses
the old KConfig way (e.g. KGlobal::config()) for other settings, and which doesn't 
use KConfigGroup to read those settings.

Such code might look ok, but isn't:

void SomeClass::readConfig()
  KConfig* config = KGlobal::config();

  // Note the use of KConfigGroupSaver - (setGroup calls would have the same problem of course)
  KConfigGroupSaver saver( config, someGroup );
  mEntry1 = config->readEntry( "Entry1" );

  // Some use of KConfigXT - could also be hidden in a method called from here....
  // GlobalSettings is the KConfigSkeleton-derived class (singleton)
  mSomeSetting = GlobalSettings::someKConfigXTsetting();

  mEntry2 = config->readEntry( "Entry2" );

The danger here, is that if the above is the first call to a GlobalSettings method, 
it will trigger the creation of the KConfigSkeleton, which will parse all the settings
known to kconfigxt, which will change the group!
Since KConfigSkeleton is using KSharedConfig, this will also change the group
in "config", so the reading of Entry2 will fail.


1) always using KConfigGroup instead of KConfigGroupSaver or manual setGroup() calls.
The object-orientation of KConfigGroup ensures that the KConfigSkeleton parsing
won't interfer.

2) ensuring that the kconfigxt parsing happens at application startup, so that it
doesn't happen at such an unexpected time.

KMail patch:
The attached patch applies solution 2) to KMail.
It has the above problem with s/SomeClass/KMFolderCachedImap/
and s/KGlobal/KMKernel/. Due to the group name being very dynamic
(dependent on the folder id), the reading of the folder configuration doesn't
use KConfigXT. The call to a GlobalSettings value is hidden in kmailicalifaceimpl,
and might only happen in the proko2 branch right now, but this is a risk of
bugs that might creep up unexpectedly, or might even be there already,
so IMHO it's worth applying the (obviously safe) patch to HEAD + branch.
Note that it won't slow down startup since other code (e.g. KMMsgDict ctor)
uses GlobalSettings already (a bit later on than the folder config reading stuff).

Thanks to Lutz Rogowski who spent hours debugging this problem...

David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kmkernel.diff
Type: text/x-diff
Size: 719 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20040812/a2d6eb9b/attachment.diff>
-------------- next part --------------
KMail developers mailing list
KMail-devel at kde.org

More information about the kde-core-devel mailing list