git: 6100374ccf26 - main - diff: Don't (ab)use sprintf() as a kind of strcat().

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 16 Nov 2022 03:23:11 UTC
The branch main has been updated by jhb:

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

commit 6100374ccf2644d3fd233bde8b8f4e73d9953c30
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-11-16 03:17:36 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-11-16 03:17:36 +0000

    diff: Don't (ab)use sprintf() as a kind of strcat().
    
    Previously print_header() used sprintf() of a buffer to itself as a
    kind of string builder but without checking for overflows.  This
    raised -Wformat-truncation and -Wrestrict warnings in GCC.  Instead,
    just conditionally print the new timestamp fields after the initial
    strftime()-formatted string.  While here, use sizeof(buf) with
    strftime() rather than a magic number.
    
    Reviewed by:    bapt
    Differential Revision:  https://reviews.freebsd.org/D36814
---
 usr.bin/diff/diffreg.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index b5e1f23b2d00..def8a4e05974 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -1624,10 +1624,7 @@ static void
 print_header(const char *file1, const char *file2)
 {
 	const char *time_format;
-	char buf1[256];
-	char buf2[256];
-	char end1[10];
-	char end2[10];
+	char buf[256];
 	struct tm tm1, tm2, *tm_ptr1, *tm_ptr2;
 	int nsec1 = stb1.st_mtim.tv_nsec;
 	int nsec2 = stb2.st_mtim.tv_nsec;
@@ -1638,26 +1635,32 @@ print_header(const char *file1, const char *file2)
 		time_format = "%c";
 	tm_ptr1 = localtime_r(&stb1.st_mtime, &tm1);
 	tm_ptr2 = localtime_r(&stb2.st_mtime, &tm2);
-	strftime(buf1, 256, time_format, tm_ptr1);
-	strftime(buf2, 256, time_format, tm_ptr2);
-	if (!cflag) {
-		strftime(end1, 10, "%z", tm_ptr1);
-		strftime(end2, 10, "%z", tm_ptr2);
-		sprintf(buf1, "%s.%.9d %s", buf1, nsec1, end1);
-		sprintf(buf2, "%s.%.9d %s", buf2, nsec2, end2);
-	}
 	if (label[0] != NULL)
 		printf("%s %s\n", diff_format == D_CONTEXT ? "***" : "---",
 		    label[0]);
-	else
-		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "***" : "---",
-		    file1, buf1);
+	else {
+		strftime(buf, sizeof(buf), time_format, tm_ptr1);
+		printf("%s %s\t%s", diff_format == D_CONTEXT ? "***" : "---",
+		    file1, buf);
+		if (!cflag) {
+			strftime(buf, sizeof(buf), "%z", tm_ptr1);
+			printf(".%.9d %s", nsec1, buf);
+		}
+		printf("\n");
+	}
 	if (label[1] != NULL)
 		printf("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
 		    label[1]);
-	else
-		printf("%s %s\t%s\n", diff_format == D_CONTEXT ? "---" : "+++",
-		    file2, buf2);
+	else {
+		strftime(buf, sizeof(buf), time_format, tm_ptr2);
+		printf("%s %s\t%s", diff_format == D_CONTEXT ? "---" : "+++",
+		    file2, buf);
+		if (!cflag) {
+			strftime(buf, sizeof(buf), "%z", tm_ptr2);
+			printf(".%.9d %s", nsec2, buf);
+		}
+		printf("\n");
+	}
 }
 
 /*