shadow-maint/shadow

test_xasprintf fails with -D_FORTIFY_SOURCE=2

Closed this issue · 1 comments

zeha commented

I'd like to enable the unittests at build-time in the Debian packaging. However, Debian builds always set -D_FORTIFY_SOURCE=2. With that in CPPFLAGS, test_xasprintf fails (see below).

Maybe this is to be expected, or maybe not? Could you clarify if this points to a real problem, or if skipping the test seems acceptable?

tests/unit$ make check V=1
...
gcc -DHAVE_CONFIG_H -I. -I../..  -I../../lib -I../.. -D_FORTIFY_SOURCE=2   -g -O2 -MT ../../lib/string/test_xasprintf-sprintf.o -MD -MP -MF ../../lib/string/.deps/test_xasprintf-sprintf.Tpo -c -o ../../lib/string/test_xasprintf-sprintf.o `testc
mv -f ../../lib/string/.deps/test_xasprintf-sprintf.Tpo ../../lib/string/.deps/test_xasprintf-sprintf.Po
gcc -DHAVE_CONFIG_H -I. -I../..  -I../../lib -I../.. -D_FORTIFY_SOURCE=2   -g -O2 -MT test_xasprintf-test_xasprintf.o -MD -MP -MF .deps/test_xasprintf-test_xasprintf.Tpo -c -o test_xasprintf-test_xasprintf.o `test -f 'test_xasprintf.c' || echoc
test_xasprintf.c: In function 'test_xasprintf_exit':
test_xasprintf.c:73:33: warning: passing argument 1 of 'xasprintf' discards 'volatile' qualifier from pointer target type [-Wdiscarded-qualifiers]
   73 |                 len = xasprintf(&p, "foo%s", "bar");
      |                                 ^~
In file included from test_xasprintf.c:18:
../../lib/string/sprintf.h:40:27: note: expected 'char ** restrict' but argument is of type 'char * volatile*'
   40 | xasprintf(char **restrict s, const char *restrict fmt, ...)
      |           ~~~~~~~~~~~~~~~~^
mv -f .deps/test_xasprintf-test_xasprintf.Tpo .deps/test_xasprintf-test_xasprintf.Po
/bin/bash ../../libtool  --tag=CC   --mode=link gcc   -g -O2 -Wl,-wrap,vasprintf -Wl,-wrap,exit   -o test_xasprintf ../../lib/string/test_xasprintf-sprintf.o test_xasprintf-test_xasprintf.o -lcmocka   -lbsd
libtool: link: gcc -g -O2 -Wl,-wrap -Wl,vasprintf -Wl,-wrap -Wl,exit -o test_xasprintf ../../lib/string/test_xasprintf-sprintf.o test_xasprintf-test_xasprintf.o  -lcmocka -lbsd

Result:

FAIL: test_xasprintf
====================

[==========] tests: Running 2 test(s).
[ RUN      ] test_xasprintf_exit
[  ERROR   ] --- 0
[   LINE   ] --- test_xasprintf.c:74: error: Failure!
[  FAILED  ] test_xasprintf_exit
[ RUN      ] test_xasprintf_ok
[  ERROR   ] --- %s() has remaining non-returned values.
: __wrap_vasprintftest_xasprintf.c:96: note: remaining item was declared here

[  FAILED  ] test_xasprintf_ok
[==========] tests: 2 test(s) run.
[  PASSED  ] 0 test(s).
[  FAILED  ] tests: 2 test(s), listed below:
[  FAILED  ] test_xasprintf_exit
[  FAILED  ] test_xasprintf_ok

That's expected. It's a common problem when using volatile (and thus when using longjmp(3)), since functions are not prepared for handling volatile stuff.

Maybe we could add a wrapper to silence that warning:

[[gnu:noipa]]
static int
xasprintf_volatile(char *volatile *restrict s, const char *restrict fmt, ...)
{
	int      len;
	va_list  ap;

	va_start(ap, fmt);
	len = xvasprintf((char **) s, fmt, ap);
	va_end(ap);

	return len;
}

This would also be more correct.