This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/25496] New: [m68k] Compiled Code Segfaults


gcc -O2 -falign-loops=4 -fomit-frame-pointer -funroll-loops -o buggy buggy.c

$ ./buggy
DBname = >>English.xmg<<
Segmentation fault

If you drop any single one of the parameters, it works. Dropping -O2 to
-O1 also works.

I wasn't able to find a simpler file that illustrated the problem, but
the attached are pretty short.

This code works fine on the other debian ports.


$ gcc -v
Using built-in specs.
Target: m68k-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,treelang --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit
--enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk
--enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre
--enable-mpfr --disable-werror --enable-checking=release m68k-linux-gnu
Thread model: posix
gcc version 4.0.3 20051201 (prerelease) (Debian 4.0.2-5)


onestring:
1,"debug:\n"

buggy.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define X_MAXNUM 1832
#define X_HEADER        "CSOUND_STRINGS\n"

#define DEBUG (1)

long benlong(long lval)       /* coerce a natural long into a bigendian long */
{
    char  benchar[4];
    char *p = benchar;

    *p++ = (char)(0xFF & (lval >> 24));
    *p++ = (char)(0xFF & (lval >> 16));
    *p++ = (char)(0xFF & (lval >> 8));
    *p   = (char)(0xFF & lval);
    return(*(long *)benchar);
}

/* String file will have a header string (X_HEADER) and then 10 characters
   making a language for identification
*/

int main(int argc, char **argv)
{
    char buff[256];
    long strings[X_MAXNUM];
    long loc, baseloc;
    int j;
    int n;
    long item = 0;
    FILE *db;
    FILE *raw;
    char dbname[16];
    char lang[30] = {'E', 'n', 'g', 'l', 'i', 's', 'h', '\0'};
    int order = ('t'<<24)|('x'<<16)|('t'<<8);

    if (argc>=2) raw = fopen(argv[1], "rb");
    else raw = fopen("onestring", "rb");
    if (raw == NULL) {
      fprintf(stderr, "Failed to open input file\n");
      exit(1);
    }
    if (argc==3) {
      /* 7 is length of `English' and there are 10 maximum */
      int len = strlen(argv[2]);
      if (len>29) len = 29;
      strncpy(lang, argv[2], len);
      memset(lang+len, '\0', 30-len); /* Null rest */
    }
    strcpy(dbname, lang); strcat(dbname, ".xmg"); /* ****** */
    if (DEBUG) fprintf(stderr, "DBname = >>%s<<\n", dbname);
    db = fopen(dbname, "wb");
    if (db == NULL) {
      fprintf(stderr, "Failed to create DB file\n");
      exit(1);
    }
    fwrite(&order, sizeof(int), 1, db);
    fwrite(X_HEADER, sizeof(X_HEADER)-1, 1, db);
    fwrite(lang, sizeof(char), 30, db);
    n = X_MAXNUM;
    n = benlong(n);
    fwrite(&n, sizeof(long), 1, db);
    baseloc = ftell(db);
    for (j=0; j<X_MAXNUM; j++) strings[j] = 0L;
    fwrite(strings, sizeof(long), X_MAXNUM, db); /* Write header */
    loc = ftell(db);
    if (DEBUG) fprintf(stderr, "Baseloc=%lx Loc=%lx\n", baseloc, loc);
    for (;;) {                  /* Read the text until ended */
      long n = 0;
      long i;
      int ch = getc(raw);
      while (ch=='\r' || ch =='\n') ch = getc(raw);
      if (DEBUG) fprintf(stderr, "Read '%c'(%.2x)\n", ch, ch);
      while (isdigit(ch)) {
        if (DEBUG) fprintf(stderr, "Read '%c'(%.2x)\n", ch, ch);
        n = n*10+ch-'0';
        ch = getc(raw);
      }
      if (DEBUG) fprintf(stderr, "String# %ld\n", n);
      if (ch==EOF) break;
      if (ch!=',') {
        fprintf(stderr,
                "item %ld/%ld: Syntax error -- expecting comma got '%c'%2x\n",
                item, n, ch, ch);
        exit(1);
      }
      item = n;
      i = 0;
      while ((ch=getc(raw))!='"') ;
                                /* Now read the string */
      while ((ch = getc(raw))!='"') {
        if (ch=='\\') {
          ch=getc(raw);
          switch (ch) {
          case 'a':
            ch = '\a'; break;
          case 'b':
            ch = '\b'; break;
          case 'n':
            ch = '\n'; break;
          case 'r':
            ch = '\r'; break;
          case 't':
            ch = '\t'; break;
          default:
            break;
          }
        }
        buff[i++]=ch;
      }
      buff[i++] = '\0';
      strings[n] = loc;
      n = benlong(i);
      fwrite(&n, sizeof(long), 1, db);
      fwrite(buff, sizeof(char), i, db);
      loc = ftell(db);
      while ((ch=getc(raw))!='\n');
    }
    fseek(db, baseloc, SEEK_SET);
    for (j=0; j<X_MAXNUM; j++) strings[j] = benlong(strings[j]);
    fwrite(strings, sizeof(long), X_MAXNUM, db); /* rewrite header */
    fclose(raw);
    fclose(db);
    fprintf(stderr, "OK\n");
    return(0);
}


-- 
           Summary: [m68k] Compiled Code Segfaults
           Product: gcc
           Version: 4.0.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: stephen at marenka dot net
 GCC build triplet: m68k-linux-gnu
  GCC host triplet: m68k-linux-gnu
GCC target triplet: m68k-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25496


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]