I used rpcgen to generate the client and server stub for program I'm developing. So, the stubs use XDR to encapsulate data and send them through the net. When I execute this piece of code, a segmentation fault is thrown:
char *str = "Hello!";
my_remote_call(str, strlen(str));
Instead, no problems if I modify it in this way:
char *str = "Hello!";
char *str2 = (char*) malloc(strlen(str));
memcpy(str2, str, strlen(str));
my_remote_call(str2, strlen(str2));
With GDB I found the segmentation fault is generated in the xdr_u_char() function called by my_remote_call(). My question is:
in the first case the Hello string is allocated in the .rodata section by compiler while in the second a part of heap is used to memorize the string. Might the segmentation fault be generate because the xdr_u_char signature require explicitly
char*
and not a
const char*
as you can see here? So in this case means that the xdr_u_char() function changes my data?
I believe it is changing data when it is receiving, and not sending, it. Are you sure your remote call is indeed using XDR with
XDR_ENCODE
mode?