Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 25 May 2026 17:03:08 +0000
From:      Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 0f3e14870906 - main - lpd: Fix issues reported by clang-analyzer
Message-ID:  <6a1480cc.388b5.45c31314@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by des:

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

commit 0f3e14870906da22a7ca821fb2153d375157cac2
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-05-25 16:51:53 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-05-25 16:52:17 +0000

    lpd: Fix issues reported by clang-analyzer
    
    Also, unlink our temporary file if we fail to chmod it.
    
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D57185
---
 usr.sbin/lpr/lpd/printjob.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/usr.sbin/lpr/lpd/printjob.c b/usr.sbin/lpr/lpd/printjob.c
index 0e86e8de8fb9..5ed7f0409679 100644
--- a/usr.sbin/lpr/lpd/printjob.c
+++ b/usr.sbin/lpr/lpd/printjob.c
@@ -155,7 +155,7 @@ printjob(struct printer *pp)
 	register int i, nitems;
 	off_t pidoff;
 	pid_t printpid;
-	int errcnt, jobcount, statok, tempfd;
+	int errcnt, jobcount, tempfd;
 
 	jobcount = 0;
 	init(pp); /* set up capabilities */
@@ -190,9 +190,6 @@ printjob(struct printer *pp)
 		    pp->spool_dir);
 		exit(1);
 	}
-	statok = stat(pp->lock_file, &stb);
-	if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS))
-		exit(0);		/* printing disabled */
 	umask(S_IWOTH);
 	lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 
 		   LOCK_FILE_MODE);
@@ -203,12 +200,14 @@ printjob(struct printer *pp)
 		    pp->lock_file);
 		exit(1);
 	}
-	/*
-	 * If the initial call to stat() failed, then lock_file will have
-	 * been created by open().  Update &stb to match that new file.
-	 */
-	if (statok != 0)
-		statok = stat(pp->lock_file, &stb);
+	if (fstat(lfd, &stb) != 0) {
+		syslog(LOG_ERR, "%s: fstat(%s): %m", pp->printer,
+		    pp->lock_file);
+		exit(1);
+	}
+	if ((stb.st_mode & LFM_PRINT_DIS) != 0) {
+		exit(0);		/* printing disabled */
+	}
 	/* turn off non-blocking mode (was turned on for lock effects only) */
 	if (fcntl(lfd, F_SETFL, 0) < 0) {
 		syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer,
@@ -219,8 +218,7 @@ printjob(struct printer *pp)
 	/*
 	 * write process id for others to know
 	 */
-	sprintf(line, "%u\n", printpid);
-	pidoff = i = strlen(line);
+	pidoff = i = snprintf(line, sizeof(line), "%u\n", printpid);
 	if (write(lfd, line, i) != i) {
 		syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
 		    pp->lock_file);
@@ -248,9 +246,10 @@ printjob(struct printer *pp)
 		    tempstderr);
 		exit(1);
 	}
-	if ((i = fchmod(tempfd, 0664)) == -1) {
+	if (fchmod(tempfd, 0664) == -1) {
 		syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
 		    tempstderr);
+		(void) unlink(tempstderr);
 		exit(1);
 	}
 	/* lpd doesn't need it to be open, it just needs it to exist */
@@ -362,15 +361,18 @@ again:
 	goto again;
 }
 
-char	fonts[4][50];	/* fonts for troff */
+char	fonts[4][64];	/* fonts for troff */
 
-char ifonts[4][40] = {
+char	ifonts[4][64] = {
 	_PATH_VFONTR,
 	_PATH_VFONTI,
 	_PATH_VFONTB,
 	_PATH_VFONTS,
 };
 
+_Static_assert(sizeof(fonts) == sizeof(ifonts), "fonts != ifonts");
+_Static_assert(sizeof(fonts[0]) == sizeof(ifonts[0]), "fonts[0] != ifonts[0]");
+
 /*
  * The remaining part is the reading of the control file (cf)
  * and performing the various actions.
@@ -394,10 +396,9 @@ printit(struct printer *pp, char *file)
 	/*
 	 * Reset troff fonts.
 	 */
-	for (i = 0; i < 4; i++)
-		strcpy(fonts[i], ifonts[i]);
+	memcpy(fonts, ifonts, sizeof(fonts));
 	sprintf(&width[2], "%ld", pp->page_width);
-	strcpy(indent+2, "0");
+	strcpy(indent, "-i0");
 
 	/* initialize job-specific count of datafiles processed */
 	job_dfcnt = 0;
@@ -945,7 +946,7 @@ sendit(struct printer *pp, char *file)
 			strlcpy(indent+2, line + 1, sizeof(indent) - 2);
 		} else if (line[0] >= 'a' && line[0] <= 'z') {
 			dfcopies = 1;
-			strcpy(last, line);
+			memcpy(last, line, sizeof(last));
 			while ((i = get_line(cfp)) != 0) {
 				if (strcmp(last, line) != 0)
 					break;


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a1480cc.388b5.45c31314>