gethost_r-align.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Since the reentrant gethost functions take a char * buffer,
  2. * we have to make sure they internally do not assume alignment.
  3. * The actual return values are not relevant. If the test fails,
  4. * it'll be due to an alignment exception which means the test
  5. * app is killed by the kernel.
  6. */
  7. #include <errno.h>
  8. #include <netdb.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <arpa/inet.h>
  13. #include <sys/socket.h>
  14. int main(int argc, char *argv[])
  15. {
  16. size_t i;
  17. char buf[1024];
  18. in_addr_t addr;
  19. addr = inet_addr("127.0.0.1");
  20. for (i = 0; i < sizeof(size_t) * 2; ++i) {
  21. struct hostent hent, *hentres;
  22. int ret, herr;
  23. printf("Testing misalignment of %2zi bytes: ", i);
  24. memset(&hent, 0x00, sizeof(hent));
  25. ret = gethostent_r(&hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  26. printf("%sgethostent_r() ", (ret ? "!!!" : ""));
  27. memset(&hent, 0x00, sizeof(hent));
  28. ret = gethostbyname_r("localhost", &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  29. printf("%sgethostbyname_r() ", (ret ? "!!!" : ""));
  30. memset(&hent, 0x00, sizeof(hent));
  31. ret = gethostbyname2_r("localhost", AF_INET, &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  32. printf("%sgethostbyname2_r() ", (ret ? "!!!" : ""));
  33. memset(&hent, 0x00, sizeof(hent));
  34. ret = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  35. printf("%sgethostbyaddr_r() ", (ret ? "!!!" : ""));
  36. puts("OK!");
  37. }
  38. return 0;
  39. }