gethost_r-align.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #if defined(__GLIBC__) || defined(__UCLIBC__)
  25. memset(&hent, 0x00, sizeof(hent));
  26. ret = gethostent_r(&hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  27. printf("%sgethostent_r() ", (ret ? "!!!" : ""));
  28. #endif
  29. memset(&hent, 0x00, sizeof(hent));
  30. ret = gethostbyname_r("localhost", &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  31. printf("%sgethostbyname_r() ", (ret ? "!!!" : ""));
  32. memset(&hent, 0x00, sizeof(hent));
  33. ret = gethostbyname2_r("localhost", AF_INET, &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  34. printf("%sgethostbyname2_r() ", (ret ? "!!!" : ""));
  35. memset(&hent, 0x00, sizeof(hent));
  36. ret = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf + i, sizeof(buf) - i, &hentres, &herr);
  37. printf("%sgethostbyaddr_r() ", (ret ? "!!!" : ""));
  38. puts("OK!");
  39. }
  40. return 0;
  41. }