Package: libdmalloc-dev
Version: 5.5.1-1.1
Severity: normal

Version of gcc: gcc-4.6 4.6.1-3
Version of libc6-dev: 2.13-10

When using any level of optimization in a program that enables GNU libc
extensions (defines _GNU_SOURCE), gcc turns strndup into a macro, which
confuses line 484 of dmalloc.h.

To illustrate, a test case, a.c

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

  #include <dmalloc.h>

  int main(void)
  {
    static const char *s = "1234567";

    char *p = strndup(s,4);
    printf("%s\n", p);

    return 0;
  }

Compilation of a.c

  gcc                     a.c -ldmalloc && ./a.out # prints "1234"
  gcc -O2                 a.c -ldmalloc && ./a.out # prints "1234"
  gcc -D_GNU_SOURCE=1     a.c -ldmalloc && ./a.out # prints "1234"
  gcc -D_GNU_SOURCE=1 -O2 a.c -ldmalloc && ./a.out # error

Looking at the output of gcc -E, line 484 turns from

  char    *strndup(const char *string, const DMALLOC_SIZE len);

into (split across several lines)

  char *(__extension__ (__builtin_constant_p (const char *string) &&
  ((size_t)(const void *)((const char *string) + 1) - (size_t)(const void
  *)(const char *string) == 1) ? (((__const char *) (const char
  *string))[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({
  size_t __len = strlen (const char *string) + 1; size_t __n = (const
  size_t len); char *__retval; if (__n < __len) __len = __n + 1; __retval
  = (char *) malloc (__len); if (__retval != ((void *)0)) { __retval[__len
  - 1] = '\0'; __retval = (char *) memcpy (__retval, const char *string,
  __len - 1); } __retval; })) : __strndup (const char *string, const
  size_t len)));

which fails to compile with the error:

  /usr/include/dmalloc.h:484:7: error: expected identifier or ‘(’ before
  ‘__extension__’

I'm not sure if this is a bug in dmalloc, or user error; maybe I did
something wrong, or perhaps dmalloc isn't designed to be used with
programs using both GCC optimization and GNU libc extensions. Either
way, a workaround is "#undef strndup" before "#include <dmalloc.h>".

Script with the above test case attached.

-- 
http://rjy.org.uk/

Attachment: a.sh
Description: Bourne shell script

Reply via email to