Index | Thread | Search

From:
Marc Espie <marc.espie.openbsd@gmail.com>
Subject:
new warning in make
To:
millert@openbsd.org
Cc:
tech@openbsd.org
Date:
Thu, 25 Sep 2025 11:35:28 +0200

Download raw body.

Thread
cc now warns over arch.c
/usr/src/usr.bin/make/arch.c:839:3: warning: 'snprintf' will always be truncated; specified size is 12, but format string expands to at least 13 [-Wformat-truncation]
  839 |                 snprintf(arHeader.ar_date, sizeof(arHeader.ar_date),
      |                 ^


Pretty sure it's correct, considering that snprintf will truncate with a 0.

Printing to a temp buffer and copying only the string ought to fix it
if I'm not mistaken.

Index: arch.c
===================================================================
RCS file: /vide/cvs/src/usr.bin/make/arch.c,v
diff -u -p -r1.94 arch.c
--- arch.c	4 Sep 2023 11:35:11 -0000	1.94
+++ arch.c	25 Sep 2025 09:33:07 -0000
@@ -836,8 +836,9 @@ ArchTouch(const char *archive, const cha
 
 	arch = ArchFindMember(archive, member, &arHeader, "r+");
 	if (arch != NULL) {
-		snprintf(arHeader.ar_date, sizeof(arHeader.ar_date),
-		    "%-12ld", (long) time(NULL));
+		char temp[sizeof(arHeader.ar_date)+1];
+		snprintf(temp, sizeof(temp), "%-12ld", (long) time(NULL));
+		memcpy(arHeader.ar_date, temp, sizeof(arHeader.ar_date));
 		if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0)
 			(void)fwrite(&arHeader, sizeof(struct ar_hdr), 1, arch);
 		fclose(arch);