28 Jul 2012, 04:50
Generic-user-small

Dennis Lambe (6 posts)

When I compile unity_fixture.c with gcc 4.4.3 and -O0, it compiles cleanly. However, when I compile it with -O1 or greater, I get the following warning:


gcc -IUnity/src -IUnity/extras/fixture/src -O1 -c Unity/extras/fixture/src/unity_fixture.c -o test/build/unity_fixture.o
Unity/extras/fixture/src/unity_fixture.c: In function ‘unity_malloc’:
Unity/extras/fixture/src/unity_fixture.c:202: warning: incompatible implicit declaration of built-in function ‘malloc'

unity_fixture.c includes <stdlib.h> before any reference to malloc, so I can’t see why this would be happening, let alone only happening at some optimization levels.

It’s causing trouble for me because I’d like to use -Werror on my builds.

21 Dec 2012, 20:37
Generic-user-small

Dennis Lambe (6 posts)

I’ve tracked this bug down. The short answer is, unity_fixture.c should include string.h before unity_fixture.h.

It seems that string.h on Linux may include a minimal stdlib.h which includes definitions for malloc and calloc (see bits/string2.h and the macro __need_malloc_and_calloc).

Since unity_fixture.h is included in unity_fixture.c before string.h, a preprocessor definition of malloc -> unity_malloc exists when string.h includes stdlib.h, and stdlib.h accidentally emits a prototype for a function named unity_malloc instead of one named malloc. stdlib.h also sets __malloc_and_calloc_defined so that it doesn’t try to define those functions twice, so the later explicit including of stdlib.h doesn’t get us the malloc and calloc prototypes we want.

phew.

09 Jan 2013, 17:47
Generic-user-small

Dennis Lambe (6 posts)

I got the fix for this bug merged upstream. Details here:

https://github.com/ThrowTheSwitch/Unity/pull/25

  You must be logged in to comment