Skip to content

Commit d5028a0

Browse files
committed
Use NODEV macro instead of explicit (dev_t)-1.
Also fix an assignment of dev_t to -1 that should be NODEV. Bug #1074.
1 parent 73cbe4e commit d5028a0

File tree

7 files changed

+23
-19
lines changed

7 files changed

+23
-19
lines changed

include/sudo_compat.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 1996, 1998-2005, 2008, 2009-2023
4+
* Copyright (c) 1996, 1998-2005, 2008, 2009-2024
55
* Todd C. Miller <[email protected]>
66
*
77
* Permission to use, copy, modify, and distribute this software for any
@@ -40,6 +40,10 @@
4040
* Macros and functions that may be missing on some operating systems.
4141
*/
4242

43+
#ifndef NODEV
44+
# define NODEV ((dev_t)-1) /* non-existent device */
45+
#endif
46+
4347
/*
4448
* Given the pointer x to the member m of the struct s, return
4549
* a pointer to the containing structure.

lib/util/ttyname_dev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2012-2018 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2012-2018, 2024 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above

plugins/sudoers/policy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
398398
ctx->user.gid = (gid_t)-1;
399399
ctx->user.uid = (gid_t)-1;
400400
ctx->user.umask = (mode_t)-1;
401-
ctx->user.ttydev = (dev_t)-1;
401+
ctx->user.ttydev = NODEV;
402402
for (cur = info->user_info; *cur != NULL; cur++) {
403403
if (MATCHES(*cur, "user=")) {
404404
CHECK(*cur, "user=");
@@ -597,7 +597,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
597597
}
598598

599599
/* ttydev is only set in user_info[] for API 1.22 and above. */
600-
if (ctx->user.ttydev == (dev_t)-1 && ctx->user.ttypath != NULL) {
600+
if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) {
601601
struct stat sb;
602602
if (stat(ctx->user.ttypath, &sb) == 0)
603603
ctx->user.ttydev = sb.st_rdev;

plugins/sudoers/timestamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2014-2023 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2014-2024 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -397,7 +397,7 @@ ts_init_key(const struct sudoers_context *ctx,
397397
sudo_warnx("unknown time stamp ticket type %d", ticket_type);
398398
FALLTHROUGH;
399399
case tty:
400-
if (ctx->user.ttydev != (dev_t)-1) {
400+
if (ctx->user.ttydev != NODEV) {
401401
/* tty-based time stamp */
402402
entry->type = TS_TTY;
403403
entry->u.ttydev = ctx->user.ttydev;

src/regress/ttyname/check_ttyname.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ main(int argc, char *argv[])
6969
char *tty_libc = NULL, *tty_sudo = NULL;
7070
char pathbuf[PATH_MAX];
7171
bool verbose = false;
72-
dev_t ttydev = -1;
72+
dev_t ttydev = NODEV;
7373
int ch, errors = 0, ntests = 1;
7474

7575
initprogname(argc > 0 ? argv[0] : "check_ttyname");
@@ -87,7 +87,7 @@ main(int argc, char *argv[])
8787

8888
/* Lookup tty name using kernel info if possible. */
8989
ttydev = get_process_ttyname(pathbuf, sizeof(pathbuf));
90-
if (ttydev != (dev_t)-1) {
90+
if (ttydev != NODEV) {
9191
char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
9292
const char *errstr;
9393
dev_t newdev;

src/sudo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ get_user_info(struct user_details *ud)
619619
}
620620

621621
ttydev = get_process_ttyname(path, sizeof(path));
622-
if (ttydev != (dev_t)-1) {
622+
if (ttydev != NODEV) {
623623
if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1)
624624
goto oom;
625625
/* The terminal device file may be missing in a chroot() jail. */

src/ttyname.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ get_process_ttyname(char *name, size_t namelen)
105105
struct sudo_kinfo_proc *ki_proc = NULL;
106106
size_t size = sizeof(*ki_proc);
107107
int mib[6], rc, serrno = errno;
108-
dev_t ttydev = (dev_t)-1;
108+
dev_t ttydev = NODEV;
109109
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);
110110

111111
/*
@@ -133,7 +133,7 @@ get_process_ttyname(char *name, size_t namelen)
133133
}
134134
errno = ENOENT;
135135
if (rc != -1) {
136-
if ((dev_t)ki_proc->sudo_kp_tdev != (dev_t)-1) {
136+
if ((dev_t)ki_proc->sudo_kp_tdev != NODEV) {
137137
errno = serrno;
138138
ttydev = (dev_t)ki_proc->sudo_kp_tdev;
139139
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
@@ -162,7 +162,7 @@ get_process_ttyname(char *name, size_t namelen)
162162
dev_t
163163
get_process_ttyname(char *name, size_t namelen)
164164
{
165-
dev_t ttydev = (dev_t)-1;
165+
dev_t ttydev = NODEV;
166166
struct psinfo psinfo;
167167
char path[PATH_MAX];
168168
ssize_t nread;
@@ -181,7 +181,7 @@ get_process_ttyname(char *name, size_t namelen)
181181
ttydev = makedev(major64(psinfo.pr_ttydev), minor64(psinfo.pr_ttydev));
182182
#endif
183183
/* On AIX, pr_ttydev is 0 (not -1) when no terminal is present. */
184-
if (ttydev != 0 && ttydev != (dev_t)-1) {
184+
if (ttydev != 0 && ttydev != NODEV) {
185185
errno = serrno;
186186
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
187187
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
@@ -192,7 +192,7 @@ get_process_ttyname(char *name, size_t namelen)
192192
}
193193
goto done;
194194
}
195-
ttydev = (dev_t)-1;
195+
ttydev = NODEV;
196196
}
197197
} else {
198198
struct stat sb;
@@ -216,7 +216,7 @@ get_process_ttyname(char *name, size_t namelen)
216216
errno = ENOENT;
217217

218218
done:
219-
if (ttydev == (dev_t)-1)
219+
if (ttydev == NODEV)
220220
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
221221
"unable to resolve tty via %s", path);
222222

@@ -233,7 +233,7 @@ dev_t
233233
get_process_ttyname(char *name, size_t namelen)
234234
{
235235
const char path[] = "/proc/self/stat";
236-
dev_t ttydev = (dev_t)-1;
236+
dev_t ttydev = NODEV;
237237
char *cp, buf[1024];
238238
int serrno = errno;
239239
pid_t ppid = 0;
@@ -335,7 +335,7 @@ get_process_ttyname(char *name, size_t namelen)
335335
done:
336336
if (fd != -1)
337337
close(fd);
338-
if (ttydev == (dev_t)-1)
338+
if (ttydev == NODEV)
339339
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
340340
"unable to resolve tty via %s", path);
341341

@@ -351,7 +351,7 @@ get_process_ttyname(char *name, size_t namelen)
351351
dev_t
352352
get_process_ttyname(char *name, size_t namelen)
353353
{
354-
dev_t ttydev = (dev_t)-1;
354+
dev_t ttydev = NODEV;
355355
int rc, serrno = errno;
356356
struct pst_status pst;
357357
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);
@@ -418,6 +418,6 @@ get_process_ttyname(char *name, size_t namelen)
418418
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
419419
"unable to resolve tty via ttyname");
420420
errno = ENOENT;
421-
debug_return_dev_t((dev_t)-1);
421+
debug_return_dev_t(NODEV);
422422
}
423423
#endif

0 commit comments

Comments
 (0)