git: 980ead5949be - stable/14 - lpd: Fix issues reported by clang-analyzer
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 28 May 2026 08:27:31 UTC
The branch stable/14 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=980ead5949bedcfbc72c57907ff775b030e0cb31
commit 980ead5949bedcfbc72c57907ff775b030e0cb31
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-28 08:27:12 +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
(cherry picked from commit 0f3e14870906da22a7ca821fb2153d375157cac2)
---
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 63d0101f6ff0..e85cc5dd280e 100644
--- a/usr.sbin/lpr/lpd/printjob.c
+++ b/usr.sbin/lpr/lpd/printjob.c
@@ -167,7 +167,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 */
@@ -202,9 +202,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);
@@ -215,12 +212,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,
@@ -231,8 +230,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);
@@ -260,9 +258,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 */
@@ -374,15 +373,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.
@@ -406,10 +408,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;
@@ -957,7 +958,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;