public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/96040] New: Compiled code causes SIGBUS at -O2
@ 2020-07-02 21:21 josephcsible at gmail dot com
  2020-07-02 22:04 ` [Bug ipa/96040] [10/11 Regression] " jakub at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: josephcsible at gmail dot com @ 2020-07-02 21:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96040

            Bug ID: 96040
           Summary: Compiled code causes SIGBUS at -O2
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---
            Target: x86_64-linux-gnu

Consider this C code:

int puts(const char *);
int snprintf(char *, unsigned long, const char *, ...);
unsigned long strspn(const char *, const char *);

struct TValue {
  union {
    long long i;
    double n;
  } value_;
  unsigned char tt_;
};

static int tostringbuff (struct TValue *num, char *str) {
  int len;
  if (num->tt_ == 3) {
    len = snprintf(str,50,"%lld",num->value_.i);
  } else {
    len = snprintf(str,50,"%.14g",num->value_.n);
    if (str[strspn(str, "-0123456789")] == '\0') {
      str[len++] = '.';
      str[len++] = '0';
    }
  }
  return len;
}

void unused (int *buff, struct TValue *num) {
  char junk[50];
  *buff += tostringbuff(num, junk);
}

char space[400];

void addnum2buff (int *buff, struct TValue *num) __attribute__((__noinline__));
void addnum2buff (int *buff, struct TValue *num) {
  *buff += tostringbuff(num, space);
}

int main(void) {
    int buff = 0;
    struct TValue num;
    num.value_.n = 1.0;
    num.tt_ = 19;
    addnum2buff(&buff, &num);
    puts(space);
}

It's supposed to print "1.0". When compiled with "gcc -O2", it instead crashes
with SIGBUS. This appears to be a regression, since it works fine on GCC 9.

The minimization is my own, but the bug was originally found in the wild by
actboy168 compiling Lua 5.4.0 on Arch Linux:
http://lua-users.org/lists/lua-l/2020-07/msg00001.html

https://godbolt.org/z/RMc3RX

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2020-07-04 17:52 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-02 21:21 [Bug c/96040] New: Compiled code causes SIGBUS at -O2 josephcsible at gmail dot com
2020-07-02 22:04 ` [Bug ipa/96040] [10/11 Regression] " jakub at gcc dot gnu.org
2020-07-03  2:02 ` josephcsible at gmail dot com
2020-07-03  6:50 ` rguenth at gcc dot gnu.org
2020-07-03  6:58 ` rguenth at gcc dot gnu.org
2020-07-03  9:04 ` jamborm at gcc dot gnu.org
2020-07-03 10:32 ` jamborm at gcc dot gnu.org
2020-07-03 10:35 ` jakub at gcc dot gnu.org
2020-07-03 11:34 ` jamborm at gcc dot gnu.org
2020-07-03 11:50 ` jakub at gcc dot gnu.org
2020-07-03 12:47 ` jamborm at gcc dot gnu.org
2020-07-03 15:41 ` cvs-commit at gcc dot gnu.org
2020-07-04 17:48 ` cvs-commit at gcc dot gnu.org
2020-07-04 17:52 ` jamborm at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).