booksbyus / zguide

Learning and Using ØMQ
http://zguide.zeromq.org
Other
3.44k stars 1.65k forks source link

Incorrect usage for recvfrom in [udpping1: UDP discovery, model 1 in C] #334

Open realjiangms opened 11 years ago

realjiangms commented 11 years ago

In the sample: udpping1: UDP discovery, model 1 in C Following seems not correct usage:

struct sockaddr_in si_that;
socklen_t si_len;
ssize_t size = recvfrom (fd, buffer, PING_MSG_SIZE, 0, &si_that, &si_len);

Valgrind will report

Syscall param socketcall.recvfrom(fromlen_in) points to uninitialised byte(s)

This is quite misleading, as recvfrom man page says:

The argument addrlen is a value-result argument, which the caller
       should initialize before the call to the size of the buffer associated with src_addr
hintjens commented 11 years ago

Thanks for spotting this...

realjiangms commented 11 years ago

Correct usage should be

struct sockaddr_in si_that = { 0 };
socklen_t si_len = sizeof(si_that);
ssize_t size = recvfrom (fd, buffer, PING_MSG_SIZE, 0, &si_that, &si_len);
realjiangms commented 11 years ago

So quick response! I even haven't finished my post!!! Amazing ^_^

hintjens commented 11 years ago

OK, I've fixed this and pushed new code; there were also some bool/Bool errors (this changed in CZMQ).

realjiangms commented 11 years ago

Thank you! Will the page http://zguide.zeromq.org/page:all also be updated? Someone like me may copy the code directly from it

hintjens commented 11 years ago

Ok, I'll update it later today.

Thanks! On Apr 30, 2013 3:59 PM, "realjiangms" notifications@github.com wrote:

Thank you! Will the page http://zguide.zeromq.org/page:all also be updated? Someone like me may copy the code directly from it

— Reply to this email directly or view it on GitHubhttps://github.com/imatix/zguide/issues/334#issuecomment-17228663 .