git: eb4d86d529e2 - main - getty: Avoid NULL deref if stdin is not a tty.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 04 Nov 2022 15:31:45 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=eb4d86d529e2523a19fd7454976923319954a49d

commit eb4d86d529e2523a19fd7454976923319954a49d
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2022-11-04 14:43:06 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2022-11-04 14:54:01 +0000

    getty: Avoid NULL deref if stdin is not a tty.
    
    Sponsored by:   Klara, Inc.
    Obtained from:  Apple OSS Distributions
    Differential Revision: https://reviews.freebsd.org/D37265
---
 libexec/getty/main.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/libexec/getty/main.c b/libexec/getty/main.c
index bc01ab83b8b2..b8af86c22e92 100644
--- a/libexec/getty/main.c
+++ b/libexec/getty/main.c
@@ -212,9 +212,14 @@ main(int argc, char *argv[])
 	 * that the file descriptors are already set up for us.
 	 * J. Gettys - MIT Project Athena.
 	 */
-	if (argc <= 2 || strcmp(argv[2], "-") == 0)
-		snprintf(ttyn, sizeof(ttyn), "%s", ttyname(STDIN_FILENO));
-	else {
+	if (argc <= 2 || strcmp(argv[2], "-") == 0) {
+		char *n = ttyname(STDIN_FILENO);
+		if (n == NULL) {
+			syslog(LOG_ERR, "ttyname: %m");
+			exit(1);
+		}
+		snprintf(ttyn, sizeof(ttyn), "%s", n);
+	} else {
 		snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]);
 		if (strcmp(argv[0], "+") != 0) {
 			chown(ttyn, 0, 0);