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

source code of glibc/sysdeps/pthread/tst-sem15.c