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

            Bug ID: 78074
           Summary: gcc-6.2.0 miscompiles calloc reimplementation
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: phelps at pobox dot com
  Target Milestone: ---

For better or worse, tcsh provides its own malloc/calloc/realloc/free
functions.  With gcc-6.2.0 (as supplied by Ubuntu-12.10, and also compiled from
sources), I'm seeing its realloc compiled into a infinite loop.  Here is a
stripped down test case that loops indefinitely:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void *calloc(size_t nmemb, size_t size)
{
    void *p = malloc(nmemb * size);
    if (p != NULL)
        memset(p, 0, nmemb * size);
    return p;
}


int main(int argc, char *argv[])
{
    char *value = calloc(64, 1);
    printf("value=%p\n", value);
    exit(0);
}

With -O2, it looks like gcc is interpreting the call to malloc as a call to
calloc instead:

(gdb) disassemble calloc
Dump of assembler code for function calloc:
   0x0000000000000760 <+0>:     imul   %rsi,%rdi
   0x0000000000000764 <+4>:     mov    $0x1,%esi
   0x0000000000000769 <+9>:     jmpq   0x760 <calloc>
End of assembler dump.

There's clearly something special about the names here: replacing calloc with
xcalloc produces code that runs fine.

Reply via email to