Bug 259826 - Gentoo QA Notice: "poor programming practices" ("dereferencing type-punned pointer" in kmimetypefactory.cpp, dds.cpp and kcrash.cpp)
Summary: Gentoo QA Notice: "poor programming practices" ("dereferencing type-punned po...
Status: RESOLVED FIXED
Alias: None
Product: kdelibs
Classification: Unmaintained
Component: general (show other bugs)
Version: 4.5
Platform: Gentoo Packages Linux
: NOR normal
Target Milestone: ---
Assignee: kdelibs bugs
URL:
Keywords:
: 311225 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-12-14 12:30 UTC by Martin Walch
Modified: 2013-05-19 19:20 UTC (History)
7 users (show)

See Also:
Latest Commit:
Version Fixed In: 4.11
Sentry Crash Report:


Attachments
Fix type-punning QA problems in kdelibs (patch against current master) (2.82 KB, text/plain)
2013-05-13 02:59 UTC, Michael Pyne
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Walch 2010-12-14 12:30:59 UTC
Version:           4.5 (using KDE 4.5.4) 
OS:                Linux

When installing kdelibs (4.5.4) on Gentoo (amd64), portage complains about some
compiler warnings (gcc 4.4.5):

QA Notice: Package has poor programming practices which may compile fine but exhibit random runtime failures.
 /var/tmp/portage/kde-base/kdelibs-4.5.4/work/kdelibs-4.5.4/kdecore/services/kmimetypefactory.cpp:527: warning: dereferencing type-punned pointer will break strict-aliasing rules
 /var/tmp/portage/kde-base/kdelibs-4.5.4/work/kdelibs-4.5.4/kimgio/dds.cpp:490: warning: dereferencing type-punned pointer will break strict-aliasing rules
 /var/tmp/portage/kde-base/kdelibs-4.5.4/work/kdelibs-4.5.4/kimgio/dds.cpp:500: warning: dereferencing type-punned pointer will break strict-aliasing rules
 /var/tmp/portage/kde-base/kdelibs-4.5.4/work/kdelibs-4.5.4/kdeui/util/kcrash.cpp:493: warning: dereferencing type-punned pointer will break strict-aliasing rules

Please do not file a Gentoo bug and instead report the above QA issues directly to the upstream developers of this software.


I did not check against trunk, but I suppose that the first warning is gone, but the others remain.

Reproducible: Always
Comment 1 Martin Walch 2010-12-14 16:19:33 UTC
some words about dds.cpp:

line 500:       b = (uint &) bits[3];

looks a bit dangerous as bits is defined as uchar bits[6];
For sizeof(uint) == 32, this will read 8 bits beyond the end of the array. I do not know if uint has a fixed width here. But if not, things may be even worse.


line 498: bit_array[7] = uchar(b & 0x07); b >>= 3;
line 508: bit_array[15] = uchar(b & 0x07); b >>= 3;

b >>= 3 can be dropped as the results are discarded
Comment 2 Christoph Feck 2010-12-14 16:25:31 UTC
It is worse. The code does not respect endianness of the CPU.

    b = (uint &) bits[3];

should probably be converted to

    b = bits[3] | (bits[4] << 8) | (bits[5] << 16);

Unfortunately, I have no test files to find regressions, so I don't really want to touch it.
Comment 3 Duncan 2011-07-12 02:59:34 UTC
Here's the report from kdelibs-4.6.95 (4.7-rc2).  Very similar, except the first one, kmimetypefactory, is now kmimetyperepository:

 * QA Notice: Package has poor programming practices which may compile
 *            fine but exhibit random runtime failures.
 * /tmp/portage/kde-base/kdelibs-4.6.95/work/kdelibs-4.6.95/kdecore/services/kmimetyperepository.cpp:444:127: warning: dereferencing type-punned pointer will break strict-aliasing rules
 * /tmp/portage/kde-base/kdelibs-4.6.95/work/kdelibs-4.6.95/kimgio/dds.cpp:490:33: warning: dereferencing type-punned pointer will break strict-aliasing rules
 * /tmp/portage/kde-base/kdelibs-4.6.95/work/kdelibs-4.6.95/kimgio/dds.cpp:500:28: warning: dereferencing type-punned pointer will break strict-aliasing rules
 * /tmp/portage/kde-base/kdelibs-4.6.95/work/kdelibs-4.6.95/kdeui/util/kcrash.cpp:609:26: warning: dereferencing type-punned pointer will break strict-aliasing rules
Comment 4 Christoph Feck 2012-12-06 14:52:32 UTC
*** Bug 311225 has been marked as a duplicate of this bug. ***
Comment 5 Michael Pyne 2013-05-13 02:59:57 UTC
Created attachment 79865 [details]
Fix type-punning QA problems in kdelibs (patch against current master)

I noticed this bug on a kdelibs-bugs mail from one of the duplicates, was surprised no one had been able to close it so decided to fix myself.

Christoph, would you mind reviewing for sanity before I go and commit (only to master at this point)?

I've tested as best I can (even to the point of starting a process from klauncher and force-crashing it to make sure kcrash can still read the PID).

I've even tested the DDS reader (though that code has other issues of its own), the Civ IV game has DDS format files which all appear to still read correctly (I had to use Okteta to make sure the DDS files had the right FourCC format to hit the affected code).
Comment 6 Christoph Feck 2013-05-13 12:34:09 UTC
Thanks Michael for testing the DDS reader and KCrash.

As for the MIME stuff, David might want to have a look at it, he also knows if it's better to commit to 4.10 branch or master.
Comment 7 Michael Pyne 2013-05-14 02:10:54 UTC
dfaure, would you mind reviewing my proposed type-punning fix for kmimetyperepository (the first attachment to this bug) to see if it is suitable for inclusion to master and/or KDE/4.10 of kdelibs?
Comment 8 Michael Pyne 2013-05-19 19:20:57 UTC
Git commit 01c098da6a43a23bacfd7ffe60393b06dd401deb by Michael Pyne.
Committed on 18/05/2013 at 23:08.
Pushed by mpyne into branch 'master'.

Fix type-punning/non-standard-aliasing issues.

Reported aeons ago as bug 259826, somehow evaded interest up to this
point.

The issue is essentially that the C++ standard allows compilers to
optimize some forms of crazy type changes, which makes the flagged code
dangerous in the face of "future optimization improvements".

None of the flagged code was so crazy that it couldn't have been
implemented in a more straightforward fashion though. The only change
I'm not 100%-alright with is the change to dds.cpp, but that code has
other issues anyways (though I did test that it still works as much as I
could).

However because of the risk of breakage I do not intend to backport.
Hopefully sufficiently-smart compilers will wait until the next major
release.
FIXED-IN:4.11

M  +4    -3    kdecore/services/kmimetyperepository.cpp
M  +1    -2    kdeui/util/kcrash.cpp
M  +5    -4    kimgio/dds.cpp

http://commits.kde.org/kdelibs/01c098da6a43a23bacfd7ffe60393b06dd401deb