c - strndup call is currupting stack frames -


i have seen strange behavior "strndup" call on aix 5.3 , 6.1. if call strndup size more size of actual source string length, there stack corruption after call.

following sample code issue can come:

int main () {     char *dst_str = null;     char src_str[1023] = "sample string";      dst_str = strndup(src_str, sizeof(src_str));      free(dst_str);     return 0; } 

does have experienced behavior?

if yes please let me know.

as per observation, there must patch os issue got fixed. not patch if @ there any. please throw light.

thanks & regards, thumbeti

you missing #include <string.h> in code. please try that—i sure work. reason without #include <string.h>, there no prototype strndup() in scope, compiler assumes strndup() returns int, , takes unspecified number of parameters. wrong. (i assuming you're compiling in posix compliant mode, strndup() available you.)

for reason, useful compile code warnings enabled.

if problem persists after change, there might bug.

edit: looks there might problem strndup() on aix: problem seems in broken strnlen() function on aix. if, after #include <string.h> see problem, you're seeing bug. google search shows long list of results it.

edit 2:

can please try following program , post 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 first strndup !!! */      fprintf(stderr,">%s<\n",verify);      if (strcmp(control, verify))          printf("strndup broken\n"); } 

(taken https://bugzilla.samba.org/show_bug.cgi?id=1097#c10.)

edit 3: after seeing output, >01234567890123456789012345678<, , no strndup broken, don't think version of aix has strndup bug.

most corrupting memory somewhere (given fact problem appears in large program, under conditions). can make small, complete, compilable example exhibits stack corruption problem? otherwise, have debug memory allocation/deallocation in program. there many programs that, such valgrind, glibc mcheck, dmalloc, electricfence, etc.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -