[Bug 1685385] Re: GCC generates bad code with zero length array and -O3

Steve Ellcey 1685385 at bugs.launchpad.net
Thu Apr 27 15:50:08 UTC 2017


This bug has been fixed on the GCC mainline at FSF.  It is PR 80533 on
the FSF Bugzilla.

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to gcc-defaults in Ubuntu.
https://bugs.launchpad.net/bugs/1685385

Title:
  GCC generates bad code with zero length array and -O3

Status in gcc-defaults package in Ubuntu:
  New

Bug description:
  With the program below, if I compile it with '-O3 -DFLEX' I get good code but if I compile it with '-O3 -UFLEX' then I get bad code.  The program is (unfortunately) not runnable but if you generate
  assembly language you can see that the good version has a loop with ldr/str/ldr/str and the bad
  version has a loop with ldr/ldr/str/str.  I.e. the second load has been moved to be before the first store and if they are referencing the same memory that will result in incorrect behavior.

  It is unclear to me why declaring the array 'o' at the end of struct 's' as a zero length array
  instead of a C99 flexible array would affect the code generation but it does.  The version with
  the flexible array works, the version with a zero length array does not work (i.e. it moves the
  second load up to before the first store).

  Test case (compile with -O3 and either -DFLEX or -UFLEX):

  struct q {
  	int b;
  };
  struct r {
     int n;
     struct q slot[0];
  };
  struct s {
     int n;
  #ifdef FLEX
   long int o[];
  #else
   long int o[0];
  #endif
  };
  extern int x, y, m;
  extern struct s *a;
  extern struct r *b;
  extern void bar();
  int foo() {
     int i,j;
     for (i = 0; i < m; i++) {
     	a->o[i] = sizeof(*a);
     	b = ((struct r *)(((char *)a) + a->o[a->n]));
  	for (j = 0; j < 10; j++) {
  		b->slot[j].b = 0;
     	}
          bar();
    }
  }

  ProblemType: Bug
  DistroRelease: Ubuntu 16.04
  Package: gcc 4:5.3.1-1ubuntu1
  Uname: Linux 4.10.0-rc2-00045-g2748079 aarch64
  ApportVersion: 2.20.1-0ubuntu2.5
  Architecture: arm64
  Date: Fri Apr 21 16:07:53 2017
  JournalErrors:
   Error: command ['journalctl', '-b', '--priority=warning', '--lines=1000'] failed with exit code 1: Failed to search journal ACL: Operation not supported
   No journal files were opened due to insufficient permissions.
  ProcEnviron:
   TERM=xterm-256color
   PATH=(custom, no user)
   XDG_RUNTIME_DIR=<set>
   LANG=en_US.UTF-8
   SHELL=/bin/bash
  SourcePackage: gcc-defaults
  UpgradeStatus: No upgrade log present (probably fresh install)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1685385/+subscriptions



More information about the foundations-bugs mailing list