title: Safely converting from struct sockaddr to struct sockaddr_storage
body: <p>I have a function which takes in "struct sockaddr *" as a parameter (let's call this input_address), and then I need to operate on that address, which may be a sockaddr_in or sockaddr_in6, since I support both IPv4 and IPv6.</p>
  
  <p>I'm getting some memory corruption and trying to track it down to it's source, and in the process found some code that seems suspect, so I would like to validate if this is the right way to do things.</p>
  
  <pre><code>struct sockaddr_storage *input_address_storage = (struct sockaddr_storage *) input_address;
  struct sockaddr_storage result = [UtilityClass performSomeOperation: *input_address_storage];
  </code></pre>
  
  <p>At first I thought the cast in the first line was safe, but then in the second line I need to dereference that pointer, which seems like it may be wrong. The reason I am concerned is that it may end up copying memory that is beyond where the original structure is (since sockaddr_in is shorter than sockaddr_in6). I am not sure if this could cause a memory corruption (my guess is no), but nevertheless this code gives me a bad feeling.</p>
  
  <p>I can't change the fact my function takes a "struct sockaddr *", so it seems like it would be difficult to work around this type of code, and yet I want to avoid copying from a memory location where I shouldn't be.</p>
  
  <p>If anyone can validate whether what I am doing is wrong, and the best way to fix this, I'd appreciate it.</p>
  
  <p>EDIT: An admin had changed my C tag for C# for some reason. The code I gave is primarily C, with one function call from objective C that doesn't really matter. That call could have been C.</p>