Module Name:    src
Committed By:   sjg
Date:           Wed Sep 28 16:34:47 UTC 2022

Modified Files:
        src/usr.bin/make: main.c meta.c

Log Message:
Don't ignore return from snprintf or getcwd


To generate a diff of this commit:
cvs rdiff -u -r1.582 -r1.583 src/usr.bin/make/main.c
cvs rdiff -u -r1.200 -r1.201 src/usr.bin/make/meta.c

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

Modified files:

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.582 src/usr.bin/make/main.c:1.583
--- src/usr.bin/make/main.c:1.582	Sat May  7 17:49:47 2022
+++ src/usr.bin/make/main.c	Wed Sep 28 16:34:47 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.582 2022/05/07 17:49:47 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.583 2022/09/28 16:34:47 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.582 2022/05/07 17:49:47 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.583 2022/09/28 16:34:47 sjg Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -704,8 +704,10 @@ Main_SetObjdir(bool writable, const char
 	va_end(ap);
 
 	if (path[0] != '/') {
-		snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path);
-		path = buf2;
+		if (snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path) <= MAXPATHLEN)
+			path = buf2;
+		else
+			return false;
 	}
 
 	/* look for the directory and try to chdir there */

Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.200 src/usr.bin/make/meta.c:1.201
--- src/usr.bin/make/meta.c:1.200	Fri Apr 15 12:28:16 2022
+++ src/usr.bin/make/meta.c	Wed Sep 28 16:34:47 2022
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.200 2022/04/15 12:28:16 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.201 2022/09/28 16:34:47 sjg Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -274,15 +274,19 @@ meta_name(char *mname, size_t mnamelen,
     /* on some systems dirname may modify its arg */
     tp = bmake_strdup(tname);
     dtp = dirname(tp);
-    if (strcmp(dname, dtp) == 0)
-	snprintf(mname, mnamelen, "%s.meta", tname);
-    else {
+    if (strcmp(dname, dtp) == 0) {
+	if (snprintf(mname, mnamelen, "%s.meta", tname) >= (int)mnamelen)
+	    mname[mnamelen - 1] = '\0';
+    } else {
+	int x;
+
 	ldname = strlen(dname);
 	if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
-	    snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
+	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
 	else
-	    snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
-
+	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
+	if (x >= (int)mnamelen)
+	    mname[mnamelen - 1] = '\0';
 	/*
 	 * Replace path separators in the file name after the
 	 * current object directory path.
@@ -762,7 +766,9 @@ meta_job_error(Job *job, GNode *gn, bool
     }
     if (gn != NULL)
 	Global_Set(".ERROR_TARGET", GNode_Path(gn));
-    getcwd(cwd, sizeof cwd);
+    if (getcwd(cwd, sizeof cwd) == NULL)
+	Punt("Cannot get cwd: %s", strerror(errno));
+
     Global_Set(".ERROR_CWD", cwd);
     if (pbm->meta_fname[0] != '\0') {
 	Global_Set(".ERROR_META_FILE", pbm->meta_fname);
@@ -1436,18 +1442,18 @@ meta_oodate(GNode *gn, bool oodate)
 				continue; /* no point */
 
 			    /* Check vs latestdir */
-			    snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p);
-			    sdirs[sdx++] = fname1;
+			    if (snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p) < (int)(sizeof fname1))
+				sdirs[sdx++] = fname1;
 
 			    if (strcmp(latestdir, lcwd) != 0) {
 				/* Check vs lcwd */
-				snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p);
-				sdirs[sdx++] = fname2;
+				if (snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p) < (int)(sizeof fname2))
+				    sdirs[sdx++] = fname2;
 			    }
 			    if (strcmp(lcwd, cwd) != 0) {
 				/* Check vs cwd */
-				snprintf(fname3, sizeof fname3, "%s/%s", cwd, p);
-				sdirs[sdx++] = fname3;
+				if (snprintf(fname3, sizeof fname3, "%s/%s", cwd, p) < (int)(sizeof fname3))
+				    sdirs[sdx++] = fname3;
 			    }
 			}
 			sdirs[sdx++] = NULL;

Reply via email to