I have code in windows that calls to getsockname as following:
getsockname(*x,NULL,0)
the third parameter (namelen) is IN\Out parameter, and contains the size of the name buffer, in bytes. On return, the namelen parameter contains the actual size in bytes of the name parameter.
Now, my question is what will occure if the the name equals to null, and the namelen equals to zero?I saw taht in linux, if name is NULL, namelen is ignored, but the MSDN doesn’t mention anything about this case..
The MSDN link is here
the IBM link for linux is here
thanks in advance
In your code, you have passed zero for the third parameter.
The third parameter cannot be null, according to the specification, it must be a pointer to an integer giving the length of the second parameter, and on output gives the actual size of the address. Passing zero does not mean that you are saying no storage has been allocated, it means you are saying you haven’t passed the information nor anywhere to write the output value.
According to MSDN and Linux documentation, the function should return -1 and set
errnotoEFAULT( or WSAGetLastError toWSAEFAULTon in windows sockets). This is defined as “Using the name and namelen parameters as specified would result in an attempt to access storage outside of the caller’s address space.”However that’s not part of the Posix specification, and I don’t read that as requiring the function the check the addresses – just that it can. In other words, in Posix at least, this is undefined behaviour. That means anything can happen.
In Linux, you say you have observed that the two parameters are ignored if null is passed. I suspect that your code isn’t checking the return value, which will probably be -1 with
errnoset toEFAULT.Posix functions don’t throw exceptions. You should be checking the return code, possibly something like this: