aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2022-11-16 03:17:36 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2023-01-24 05:12:44 +0000
commita8b6a13b0aa3408a7793dad22ad0193a2c32477f (patch)
tree9707e20e397cb436b34b9588e33a72288778bc05
parent95a52eb29ca93fb457ac618b8de7a754700e55c4 (diff)
downloadsrc-a8b6a13b0aa3408a7793dad22ad0193a2c32477f.tar.gz
src-a8b6a13b0aa3408a7793dad22ad0193a2c32477f.zip
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 (cherry picked from commit 6100374ccf2644d3fd233bde8b8f4e73d9953c30)
-rw-r--r--usr.bin/diff/diffreg.c39
1 files changed, 21 insertions, 18 deletions
diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index 94998a364715..800f489e510a 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -1655,10 +1655,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;
@@ -1669,26 +1666,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");
+ }
}
/*