tst-sem15.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Test for SEM_VALUE_MAX overflow detection: BZ #18434.
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <semaphore.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. static int
  22. do_test (void)
  23. {
  24. sem_t s;
  25. if (sem_init (&s, 0, SEM_VALUE_MAX))
  26. {
  27. printf ("sem_init: %m\n");
  28. return 1;
  29. }
  30. int result = 0;
  31. int value = 0xdeadbeef;
  32. if (sem_getvalue (&s, &value))
  33. {
  34. printf ("sem_getvalue: %m\n");
  35. result = 1;
  36. }
  37. else
  38. {
  39. printf ("sem_getvalue after init: %d\n", value);
  40. if (value != SEM_VALUE_MAX)
  41. {
  42. printf ("\tshould be %d\n", SEM_VALUE_MAX);
  43. result = 1;
  44. }
  45. }
  46. errno = 0;
  47. if (sem_post(&s) == 0)
  48. {
  49. puts ("sem_post at SEM_VALUE_MAX succeeded!");
  50. result = 1;
  51. }
  52. else
  53. {
  54. printf ("sem_post at SEM_VALUE_MAX: %m (%d)\n", errno);
  55. if (errno != EOVERFLOW)
  56. {
  57. printf ("\tshould be %s (EOVERFLOW = %d)\n",
  58. strerror (EOVERFLOW), EOVERFLOW);
  59. result = 1;
  60. }
  61. }
  62. value = 0xbad1d00d;
  63. if (sem_getvalue (&s, &value))
  64. {
  65. printf ("sem_getvalue: %m\n");
  66. result = 1;
  67. }
  68. else
  69. {
  70. printf ("sem_getvalue after post: %d\n", value);
  71. if (value != SEM_VALUE_MAX)
  72. {
  73. printf ("\tshould be %d\n", SEM_VALUE_MAX);
  74. result = 1;
  75. }
  76. }
  77. if (sem_destroy (&s))
  78. {
  79. printf ("sem_destroy: %m\n");
  80. result = 1;
  81. }
  82. return result;
  83. }
  84. #define TEST_FUNCTION do_test ()
  85. #include "../test-skeleton.c"