Good Person
Good Person

Reputation: 1443

Does accept() should modify address_len on failure?

In the following partial code:

addrlen = sizeof(addr);
accept(sk, (struct sockaddr *)&addr, &addrlen);

If accept fails, is it possible for addrlen to be 0 or otherwise != sizeof(addr)?

Upvotes: 1

Views: 70

Answers (3)

mark4o
mark4o

Reputation: 60843

POSIX was not clear on this, and reportedly FreeBSD did set *address_len to 0 on some error conditions, although most systems leave it unchanged on error. The next POSIX revision will clarify that *address_len shall not be modified on error. At least until the next POSIX revision becomes ubiquitous, applications would be wise not to assume anything about the value of *address_len if an error is returned.

Upvotes: 3

Edward Tomasz Napierala
Edward Tomasz Napierala

Reputation: 1836

It shouldn't, but I seem to remember a recently fixed bug where that actually happened.

Upvotes: 2

user207421
user207421

Reputation: 310885

I doubt you can rely on it, and I can't see any reason why you would want to either. The first thing you should examine is the return code, which is -1 on error, and only examine the address/address length on success.

Upvotes: 1

Related Questions