You are missing a #include <string.h>
in your code. Please try that—I am fairly sure it will work. The reason is that without the #include <string.h>
, there is no prototype for strndup()
in scope, so the compiler assumes that strndup()
returns an int
, and takes an unspecified number of parameters. That is obviously wrong. (I am assuming you re compiling in POSIX compliant mode, so strndup()
is available to you.)
For this reason, it is always useful to compile code with warnings enabled.
If your problem persists even after the change, there might be a bug.
Edit: Looks like there might be a problem with strndup()
on AIX: the problem seems to be in a broken strnlen()
function on AIX. If, even after #include <string.h>
you see the problem, it is likely you re seeing the bug. A google search shows a long list of results about it.
Edit 2:
Can you please try the following program and post the results?
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *test1 = "abcdefghijabcdefghijabcdefghijk";
char *test2 = "012345678901234567890123456789";
char *control = "01234567890123456789012345678";
char *verify;
free(strndup(test1, 30));
verify = strndup(test2, 29); /* shorter then first strndup !!! */
fprintf(stderr,">%s<
",verify);
if (strcmp(control, verify))
printf("strndup is broken
");
}
(Taken from https://bugzilla.samba.org/show_bug.cgi?id=1097#c10.)
Edit 3: After seeing your output, which is >01234567890123456789012345678<
, and with no strndup is broken
, I don t think your version of AIX has the strndup
bug.
Most likely you are corrupting memory somewhere (given the fact that the problem only appears in a large program, under certain conditions). Can you make a small, complete, compilable example that exhibits the stack corruption problem? Otherwise, you will have to debug your memory allocation/deallocation in your program. There are many programs to help you do that, such as valgrind, glibc mcheck, dmalloc, electricfence, etc.