Module Name: src Committed By: dsl Date: Sat Jul 11 11:45:17 UTC 2009
Modified Files: src/regress/lib/libc/string/strlen: strlen_test.c Log Message: The local function pointer wasn't enough to stop gcc using the builtin strlen! Move to file scope and make 'volatile'. When test fails use write (printf might be broken) to report info. To aid testing new versions, use dlsym() for "test_strlen" defaulting to strlen() so that the rest of the program doesn't use the broken version. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/regress/lib/libc/string/strlen/strlen_test.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/regress/lib/libc/string/strlen/strlen_test.c diff -u src/regress/lib/libc/string/strlen/strlen_test.c:1.1 src/regress/lib/libc/string/strlen/strlen_test.c:1.2 --- src/regress/lib/libc/string/strlen/strlen_test.c:1.1 Tue Mar 15 15:57:58 2005 +++ src/regress/lib/libc/string/strlen/strlen_test.c Sat Jul 11 11:45:16 2009 @@ -28,14 +28,34 @@ #include <assert.h> #include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <dlfcn.h> void check_strlen(void); +size_t (*volatile strlen_fn)(const char *s); + +static void +write_num(int val) +{ + char buf[20]; + int i; + for (i = sizeof buf; --i >= 0;) { + buf[i] = '0' + val % 10; + val /= 10; + if (val == 0) { + write(2, buf + i, sizeof buf - i); + return; + } + } + write(2, "overflow", 8); +} + void check_strlen(void) { /* try to trick the compiler */ - size_t (*f)(const char *s) = strlen; int a; int t; @@ -102,9 +122,23 @@ for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) { memcpy(&buf[a], tab[t].val, tab[t].len + 1); - len = f(&buf[a]); + len = strlen_fn(&buf[a]); - assert(len == tab[t].len); + if (len != tab[t].len) { + /* Write error without using printf (and strlen) */ + write(2, "alignment ", 10); + write_num(a); + write(2, ", test ", 7); + write_num(t); + write(2, ", got len ", 10); + write_num(len); + write(2, ", not ", 6); + write_num(tab[t].len); + write(2, ", for '", 7); + write(2, tab[t].val, tab[t].len); + write(2, "'\n", 2); + exit(1); + } } } } @@ -112,6 +146,11 @@ int main(void) { + /* During testing it is useful have the rest of the program + * use a known good version! */ + strlen_fn = dlsym(dlopen(NULL, RTLD_LAZY), "test_strlen"); + if (!strlen_fn) + strlen_fn = strlen; check_strlen(); return 0; }