Shared XDR routines and pointer to .rodata section

514 views Asked by At

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?

2

There are 2 answers

1
Basile Starynkevitch On

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?

2
Basile Starynkevitch On

To transmit string to XDR you should use xdr_string not xdr_u_char; show us the *.x file for rpcgen ...