0

In a x86 stack I needn't fill sin_zero with 2 "push 0" in POSIX-compliant architectures. But must I in Windows?

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
}; 
Rob
  • 99
  • 1
  • 9
  • 2
    `sin_zero` in the [sockaddr_in](https://msdn.microsoft.com/en-us/library/zx63b042.aspx) structure is documented as: *"Padding to make structure the same size as SOCKADDR."* If you want to manually construct a `sockaddr_in` instance on the stack, you must make enough room for it. Whether you use `sub esp,8` or 2 `push 0xbabe2bed` instructions is irrelevant. – IInspectable Feb 01 '15 at 11:25
  • mm I understand. But in Windows, for example, is it obligatory I fill this 8 bytes? – Rob Feb 01 '15 at 12:27
  • The structure is defined to have a specific layout. This is true for all platforms. Whether you fill the 8 bytes in `sin_zero` with data, or merely allot space for them, is of no relevance. If you are having trouble translating this into assembly, use a C compiler instead and have it do the math. – IInspectable Feb 01 '15 at 13:15
  • @IInspectable: I think the confusion is the name of this element: It is named "sin_zero" and not "sin_unused" what suggests that it is mandataory to fill this element with zeros - independent of the programming language used. – Martin Rosenau Feb 01 '15 at 14:37

1 Answers1

0

Generally, it is recommended to actually zero sin_zero.

Officially it is not required by the standard, but some sources claim that not zero-ing can result in undefined behavior on some architectures. Something I also observed in a project myself.

Furthermore, WinAPI documentation mentions the following about sin_zero:

Reserved for system use. A WSK application should set the contents of this array to zero.

More info in this question.

Ben
  • 1,519
  • 23
  • 39