0

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?

Dario
  • 215
  • 2
  • 10

2 Answers2

0

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?

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I think so because the server should print some infos when the corresponding server-side function is called. And no infos are printed. – Dario Nov 06 '11 at 11:24
0

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I know, I know, you're right, but it was only an example. The problem is that the string I'm handling are not NULL-terminated. I just wanted to understand the cause of segmentation fault. Sorry, I couldn't show the .x file, it for my thesis and there're some legal constraints on. :( – Dario Nov 06 '11 at 12:00
  • 1
    You could debug, using the debuggable variant of the `libc`; on a Debian or Ubuntu system, install the `libc6-dbg` package and set `LD_LIBRARY_PATH` appropriately. And you should look at the source code of the XDR portion of `libc`. – Basile Starynkevitch Nov 06 '11 at 14:18