Module Name:    src
Committed By:   riastradh
Date:           Thu Dec 19 20:07:16 UTC 2024

Modified Files:
        src/tests/lib/libc/sys: t_getitimer.c

Log Message:
t_getitimer: Test invalid itimerval arguments.

Use sig_atomic_t while here.

Ex-POSIX (POSIX.1-2008; the interface was removed in POSIX.1-2024)
prescribes EINVAL for all of these cases:

SYNOPSIS

    #include <sys/time.h>

    int getitimer(int which, struct itimerval *value);
    int setitimer(int which, const struct itimerval *restrict value,
           struct itimerval *restrict ovalue);
[...]
ERRORS

    The setitimer() function shall fail if:

    [EINVAL]
        The value argument is not in canonical form. (In canonical
        form, the number of microseconds is a non-negative integer
        less than 1000000 and the number of seconds is a non-negative
        integer.)

https://pubs.opengroup.org/onlinepubs/9699919799/functions/setitimer.html


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/lib/libc/sys/t_getitimer.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/sys/t_getitimer.c
diff -u src/tests/lib/libc/sys/t_getitimer.c:1.4 src/tests/lib/libc/sys/t_getitimer.c:1.5
--- src/tests/lib/libc/sys/t_getitimer.c:1.4	Mon Apr  4 19:33:46 2022
+++ src/tests/lib/libc/sys/t_getitimer.c	Thu Dec 19 20:07:16 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: t_getitimer.c,v 1.4 2022/04/04 19:33:46 andvar Exp $ */
+/* $NetBSD: t_getitimer.c,v 1.5 2024/12/19 20:07:16 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_getitimer.c,v 1.4 2022/04/04 19:33:46 andvar Exp $");
+__RCSID("$NetBSD: t_getitimer.c,v 1.5 2024/12/19 20:07:16 riastradh Exp $");
 
 #include <sys/time.h>
 
@@ -40,15 +40,17 @@ __RCSID("$NetBSD: t_getitimer.c,v 1.4 20
 #include <string.h>
 #include <unistd.h>
 
-static bool	fail;
-static void	sighandler(int);
+#include "h_macros.h"
+
+static sig_atomic_t	fired;
+static void		sighandler(int);
 
 static void
 sighandler(int signo)
 {
 
 	if (signo == SIGALRM || signo == SIGVTALRM)
-		fail = false;
+		fired = 1;
 }
 
 ATF_TC(getitimer_empty);
@@ -125,8 +127,7 @@ ATF_TC_BODY(setitimer_basic, tc)
 	it.it_interval.tv_sec = 0;
 	it.it_interval.tv_usec = 0;
 
-	fail = true;
-
+	fired = 0;
 	ATF_REQUIRE(signal(SIGALRM, sighandler) != SIG_ERR);
 	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);
 
@@ -139,7 +140,7 @@ ATF_TC_BODY(setitimer_basic, tc)
 	 */
 	(void)sleep(1);
 
-	if (fail != false)
+	if (!fired)
 		atf_tc_fail("timer did not fire");
 }
 
@@ -201,6 +202,41 @@ ATF_TC_BODY(setitimer_old, tc)
 		atf_tc_fail("setitimer(2) did not store old values");
 }
 
+ATF_TC(setitimer_invalidtime);
+ATF_TC_HEAD(setitimer_invalidtime, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test invalid values in setitimer(2)");
+}
+
+ATF_TC_BODY(setitimer_invalidtime, tc)
+{
+	const struct itimerval it[] = {
+		[0] = { .it_value = {-1, 0} },
+		[1] = { .it_value = {0, -1} },
+		[2] = { .it_value = {0, 1000001} },
+		[3] = { .it_value = {1, 0}, .it_interval = {-1, 0} },
+		[4] = { .it_value = {1, 0}, .it_interval = {0, -1} },
+		[5] = { .it_value = {1, 0}, .it_interval = {0, 1000001} },
+	};
+	sigset_t sigs, mask;
+	unsigned i;
+
+	RL(sigemptyset(&sigs));
+	RL(sigaddset(&sigs, SIGALRM));
+	RL(sigprocmask(SIG_BLOCK, &sigs, &mask));
+
+	for (i = 0; i < __arraycount(it); i++) {
+		fprintf(stderr, "case %u\n", i);
+		ATF_CHECK_ERRNO(EINVAL,
+		    setitimer(ITIMER_REAL, &it[i], NULL) == -1);
+	}
+
+	/* Wait up to 2sec to make sure no timer got set anyway. */
+	ATF_CHECK_ERRNO(EAGAIN,
+	    sigtimedwait(&sigs, NULL, &(const struct timespec){2, 0}) == -1);
+	RL(sigprocmask(SIG_SETMASK, &mask, NULL));
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
@@ -209,6 +245,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, setitimer_basic);
 	ATF_TP_ADD_TC(tp, setitimer_err);
 	ATF_TP_ADD_TC(tp, setitimer_old);
+	ATF_TP_ADD_TC(tp, setitimer_invalidtime);
 
 	return atf_no_error();
 }

Reply via email to