llvm/compiler-rt/test/sanitizer_common/TestCases/Linux/get_sock_peer_name.cpp

// Test that ASan doesn't raise false alarm when getsockname and getpeername
// are called with addrlen=nullptr;
//
// RUN: %clangxx %s -o %t && %run %t 2>&1

#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>

int main() {
  const int fd = socket(AF_INET, SOCK_DGRAM, 0);
  assert(fd >= 0);

  const sockaddr_in sin = {
      .sin_family = AF_INET,
      .sin_port = htons(1234),
      .sin_addr =
          {
              .s_addr = htonl(INADDR_LOOPBACK),
          },
  };
  assert(connect(fd, reinterpret_cast<const sockaddr *>(&sin), sizeof(sin)) ==
         0);

  errno = 0;
  assert(getsockname(fd, nullptr, nullptr) == -1);
  assert(errno == EFAULT);

  errno = 0;
  assert(getpeername(fd, nullptr, nullptr) == -1);
  assert(errno == EFAULT);

  return 0;
}