potential-memleaks.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define FGETS_BUFSIZE 64
  5. /* Problem with FreeBSD 10.3 fgets() with stdin. */
  6. static void
  7. pl_freebsd_fgets(void)
  8. {
  9. char buf[FGETS_BUFSIZE];
  10. if (fgets(buf, FGETS_BUFSIZE, stdin) == NULL)
  11. exit(1);
  12. }
  13. /* Problem with FreeBSD 12.0 and printf(). */
  14. static void
  15. pl_freebsd_printf_space(void)
  16. {
  17. printf(" ");
  18. }
  19. /* Problem with FreeBSD 12.0 and printf(). */
  20. static void
  21. pl_freebsd_printf_space_newline(void)
  22. {
  23. printf(" \n");
  24. }
  25. /* Problem with FreeBSD 12.0 and strerror(). */
  26. static void
  27. pl_freebsd_strerror(void)
  28. {
  29. char * str = strerror(0);
  30. (void)str; /* UNUSED */
  31. }
  32. #define MEMLEAKTEST(x) { #x, x }
  33. static const struct memleaktest {
  34. const char * const name;
  35. void (* const volatile func)(void);
  36. } tests[] = {
  37. MEMLEAKTEST(pl_freebsd_fgets),
  38. MEMLEAKTEST(pl_freebsd_printf_space),
  39. MEMLEAKTEST(pl_freebsd_printf_space_newline),
  40. MEMLEAKTEST(pl_freebsd_strerror)
  41. };
  42. static const int num_tests = sizeof(tests) / sizeof(tests[0]);
  43. int
  44. main(int argc, char * argv[])
  45. {
  46. int i;
  47. if (argc == 2) {
  48. /* Run the relevant function. */
  49. for (i = 0; i < num_tests; i++) {
  50. if ((strcmp(argv[1], tests[i].name)) == 0) {
  51. tests[i].func();
  52. goto success;
  53. }
  54. }
  55. /* We didn't find the desired function name. */
  56. goto err0;
  57. } else {
  58. /* Print test names. */
  59. for (i = 0; i < num_tests; i++)
  60. printf("%s\n", tests[i].name);
  61. }
  62. success:
  63. /* Success! */
  64. exit(0);
  65. err0:
  66. /* Failure! */
  67. exit(1);
  68. }