1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-03 09:00:21 +00:00

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
This commit is contained in:
John Baldwin 2022-11-15 19:17:36 -08:00
parent 4e0771714d
commit 6100374ccf

View File

@ -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");
}
}
/*