[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#1038134: g++-12: Conditional compilation error in optimized mode



Package: g++-12
Version: 12.2.0-14
Severity: normal
X-Debbugs-Cc: ivans@isle.spb.ru

Dear Maintainer,

When I compile c++ code which has an error (method invoked on
null class pointer) the following problem occurs: the actual
call does not crash, since 'this' pointer is not really used
in called method, but conditional below works incorrectly.
Here's the simplified code in question:

      else {
         np = getstr(cp);
         if (!::strcasecmp(np, "info"))
            current = channel->get_class(ErrorCode::Info);
         else if (!::strcasecmp(np, "internal"))
            current = channel->get_class(ErrorCode::Internal);
	 . . . . . .
         else {
            Log::Error("%s:%d: unrecognized error class [%s]", _fname, _lineno, np);
            current = 0;
            continue;
         }
      }

      if (!channel || !current)
         continue;

      np = getstr(cp);

channel can be 0 after first 'else', but get_class() does not crash,
since it just returns a computed pointer to array item inside the
object, so that current is set to some invalid value like 0x120.
It proceeds to the 'if (!channel || !current)' conditional and
with given values (channel == 0, current == 0x120) continue should
be executed. Instead the control falls through to the next line.

This error occurs only when the code is compiled with -O2 or -O3.
When -O0 is used, the conditional works properly. The error is
not observed when compiled with g++-11.

The error also dissappears when I fix my error (replace first
'else {' by 'else if (channel) {').

If this information is of any interest I can send combined
c++/assembly listings for cases with and without optimization.

Best regards,

-- System Information:
Debian Release: 12.0
  APT prefers stable-security
  APT policy: (500, 'stable-security'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 5.10.0-23-amd64 (SMP w/2 CPU threads)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE=en_US:en
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages g++-12 depends on:
ii  gcc-12            12.2.0-14
ii  gcc-12-base       12.2.0-14
ii  libc6             2.36-9
ii  libgmp10          2:6.2.1+dfsg1-1.1
ii  libisl23          0.25-1
ii  libmpc3           1.3.1-1
ii  libmpfr6          4.2.0-1
ii  libstdc++-12-dev  12.2.0-14
ii  libzstd1          1.5.4+dfsg2-5
ii  zlib1g            1:1.2.13.dfsg-1

g++-12 recommends no packages.

Versions of packages g++-12 suggests:
pn  g++-12-multilib  <none>
pn  gcc-12-doc       <none>

-- no debconf information


Reply to: