Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 540596 - app-text/calibre-1.48-r1 - .../work/calibre/src/calibre/utils/lzx/lzxd.c:362:21: warning: iteration 24u invokes undefined behavior [-Waggressive-loop-optimizations]
Summary: app-text/calibre-1.48-r1 - .../work/calibre/src/calibre/utils/lzx/lzxd.c:362:...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: AMD64 Linux
: Normal normal (vote)
Assignee: Ian Stakenvicius (RETIRED)
URL: https://github.com/kovidgoyal/calibre...
Whiteboard:
Keywords: InVCS
Depends on:
Blocks:
 
Reported: 2015-02-19 00:38 UTC by Jeremi Piotrowski
Modified: 2017-04-24 00:08 UTC (History)
4 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
build log (file_540596.txt,30.29 KB, text/plain)
2015-02-19 00:43 UTC, Jeremi Piotrowski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeremi Piotrowski 2015-02-19 00:38:14 UTC
During the compilation of `lzxd.c` I noticed a warning:

> lzxd.c:362:21: warning: iteration 24u invokes undefined behavior [-Waggressive-loop-optimizations]

Looking at the build command I see that `calibre-1.48-r1` is compiling with `-O3`, so the build system must be overriding the user CFLAGS (I use -O2). Looking at the part of code that invokes this warning I see the following:
```
static unsigned char extra_bits[51];

static void lzxd_static_init(void) {
  int i, j;

  for (i = 0, j = 0; i < 51; i += 2) {
    extra_bits[i]   = j;
    extra_bits[i+1] = j;
    if ((i != 0) && (j < 17)) j++;
  }
  ...
```
Loop goes from 0 to 50 in steps of 2, last iteration is `i=50`, array size is 51, we write to `i` and `i+1` so last iteration writes outside the array. 

I don't know if this has any consequences later (didn't finish compiling), but since undefined behavior allows the compiler to do whatever it wants, this should be fixed.

Reproducible: Always

Steps to Reproduce:
1. Emerge calibre-1.48-r1
2. Wait until `####### Building extension lzx #######`
3. See warning.

Actual Results:  
Invokes undefined behavior, compiles with not user specified, higher optimization level.

Expected Results:  
No undefined behavior, respects user optimization level choice.

The command used for compilation:
---------------------------------
x86_64-pc-linux-gnu-gcc -Wall -DNDEBUG -fno-strict-aliasing -pipe -fPIC -march=core2 -O2 -pipe -pthread -I/usr/include/python2.7 -O3 -I/var/tmp/portage/app-text/calibre-1.48-r1/work/calibre/src/calibre/utils/lzx -c /var/tmp/portage/app-text/calibre-1.48-r1/work/calibre/src/calibre/utils/lzx/lzxd.c -o /var/tmp/portage/app-text/calibre-1.48-r1/work/calibre/build/objects/lzx/lzxd.o

emerge --info
--------------
Portage 2.2.14 (python 2.7.9-final-0, default/linux/amd64/13.0/desktop, gcc-4.8.3, glibc-2.19-r1, 3.17.8-gentoo-r1 x86_64)
=================================================================
System uname: Linux-3.17.8-gentoo-r1-x86_64-Pentium-R-_Dual-Core_CPU_T4400_@_2.20GHz-with-gentoo-2.2
KiB Mem:     2010256 total,    123448 free
KiB Swap:    2999904 total,   2897020 free
Timestamp of tree: Tue, 17 Feb 2015 13:45:01 +0000
ld GNU ld (Gentoo 2.24 p1.4) 2.24
app-shells/bash:          4.2_p53
dev-java/java-config:     2.2.0
dev-lang/perl:            5.20.1-r4
dev-lang/python:          2.7.9-r2, 3.4.2
dev-util/cmake:           2.8.12.2-r1
dev-util/pkgconfig:       0.28-r1
sys-apps/baselayout:      2.2
sys-apps/openrc:          0.13.9
sys-apps/sandbox:         2.6-r1
sys-devel/autoconf:       2.69
sys-devel/automake:       1.11.6-r1, 1.13.4
sys-devel/binutils:       2.24-r3
sys-devel/gcc:            4.8.3
sys-devel/gcc-config:     1.7.3
sys-devel/libtool:        2.4.4
sys-devel/make:           4.0-r1
sys-kernel/linux-headers: 3.16 (virtual/os-headers)
sys-libs/glibc:           2.19-r1
Repositories: gentoo crossdev gentoo-zh java sublime-text stuff raiagent science vaca local
Installed sets: @android
ACCEPT_KEYWORDS="amd64"
ACCEPT_LICENSE="* -@EULA"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-march=core2 -O2 -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/share/gnupg/qualified.txt /var/lib/hsqldb"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/php/apache2-php5.5/ext-active/ /etc/php/cgi-php5.5/ext-active/ /etc/php/cli-php5.5/ext-active/ /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo /etc/texmf/language.dat.d /etc/texmf/language.def.d /etc/texmf/updmap.d /etc/texmf/web2c"
CXXFLAGS="-march=core2 -O2 -pipe"
DISTDIR="/usr/portage/distfiles"
FCFLAGS="-O2 -pipe"
FEATURES="assume-digests binpkg-logs config-protect-if-modified distlocks ebuild-locks fixlafiles merge-sync news parallel-fetch preserve-libs protect-owned sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync"
FFLAGS="-O2 -pipe"
GENTOO_MIRRORS="http://gentoo.mirror.pw.edu.pl/"
LANG="en_US.utf8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
MAKEOPTS="-j3"
PKGDIR="/usr/portage/packages"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"
PORTAGE_TMPDIR="/var/tmp"
PORTDIR="/usr/portage"
PORTDIR_OVERLAY="/usr/local/portage/crossdev /var/lib/layman/gentoo-zh /var/lib/layman/java /var/lib/layman/sublime-text /var/lib/layman/stuff /var/lib/layman/raiagent /var/lib/layman/science /var/lib/layman/vaca /usr/local/portage/overlay"
SYNC="rsync://rsync.pl.gentoo.org/gentoo-portage"
USE="X a52 aac acl acpi alsa amd64 berkdb bindist branding bzip2 cairo cdda cdr cleartype cli consolekit corefonts cracklib crypt cups cxx dbus dri dts dvd dvdr emboss encode exif fam firefox flac fontconfig fortran gdbm gif glamor gpm gtk iconv ipv6 jpeg lcms ldap libnotify mad mmx mng modules mp3 mp4 mpeg multilib ncurses networkmanager nls nptl ogg opengl openmp openrc pam pango pcre pdf png policykit ppds qt3support qt4 readline sdl session sna spell sse sse2 sse3 ssl ssse3 startup-notification svg tcpd threads tiff truetype type3 udev udisks unicode upower usb vaapi vorbis wifi wxwidgets x264 xcb xft xml xv xvid zlib" ABI_X86="64" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="kexi words flow plan sheets stage tables krita karbon braindump author" CAMERAS="ptp2" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="mmx mmxext sse sse2 sse3 ssse3" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf superstar2 timing tsip tripmate tnt ublox ubx" INPUT_DEVICES="evdev synaptics" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php5-5" PYTHON_SINGLE_TARGET="python2_7" PYTHON_TARGETS="python2_7 python3_4" RUBY_TARGETS="ruby19 ruby20" USERLAND="GNU" VIDEO_CARDS="intel i915 i965" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account"
USE_PYTHON="3.4"
Unset:  CPPFLAGS, CTARGET, EMERGE_DEFAULT_OPTS, INSTALL_MASK, LC_ALL, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS
Comment 1 Jeremi Piotrowski 2015-02-19 00:43:31 UTC
Created attachment 396916 [details]
build log
Comment 2 Jeremi Piotrowski 2015-02-19 00:49:55 UTC
N
Comment 3 Jeremi Piotrowski 2015-02-19 00:57:09 UTC
The `O3` flag is added due to the python build system, probably not worth trying to fight that one. So best to just remove the UB, e.g. by changing the size of `extra_bits` to 52 (doesn't seem to have implications).
Comment 4 Jeroen Roovers (RETIRED) gentoo-dev 2015-02-19 07:37:26 UTC
-Waggressive-loop-optimizations has nothing to do with -O3.
Comment 5 Jonas Stein gentoo-dev 2017-04-22 21:46:47 UTC
app-text/calibre-1.48-r1 is not in the tree anymore.

does this still happen in >=app-text/calibre-2.78.0, or can we close the bug?
Comment 6 Jeremi Piotrowski 2017-04-23 18:04:40 UTC
I still see the same warning with calibre-2.78.0 and looking at the calibre git repository (https://github.com/kovidgoyal/calibre) I see that that code hasn't been touched since 2008. However, since this is actually an upstream QA issue, I'm fine with this bug getting closed.
Comment 7 Zac Medico gentoo-dev 2017-04-23 22:45:27 UTC
The bug was fixed in these 2 commits to the libmspack library:

https://github.com/kyz/libmspack/commit/6a42ddd1d472afeaf0f7da91e16b60ab2063fb92
https://github.com/kyz/libmspack/commit/ce3cc03aa500dd9c0b6b820f9519f6b6b9dede05

I'll submit a pull request to have them included in calibre.