From nobody Wed Nov 16 03:23:11 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NBpKD0DcTz4hNfr; Wed, 16 Nov 2022 03:23:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NBpKC6fc4z4MFb; Wed, 16 Nov 2022 03:23:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668568991; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=sZUWs7N1KE+NBg2D0w4s4WFHYTBK0LEWxmS1OuM3FII=; b=X7Uhyon9mGCIobRoZfa0Kj2fOUrk9+AEAshIYQv2QRmDu5R2+9vIYsmaFDQJhhR1OnO2jf ZY+gtyRWbUBVizyJmske4T4JMxxpblUPbo4iJhvrG22OzfKXOhsQr06yZuOYi2lGk/05LK jJ7ijNQsasJjwM48/b5SOZvnWQka2V2O8tHdXXP0PYKI7TTpIVhoC0X1DKF5OrWxx+X0mD G8M+F7m7YA6c79OMERdwULUEs+4vg5gD6pYI0hv25tOi+B+p1XZBnpTUBZuRtHmAIE0RSn zEPTk/aUTrOR+oGayV/Bt9srckA2zEJZS/ojolZWdkciWKUSv4bIIJqSJ2UAaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668568991; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=sZUWs7N1KE+NBg2D0w4s4WFHYTBK0LEWxmS1OuM3FII=; b=ZHqYBijNlUToUI6soQ3aL19o04gmXHRCeC1ACTdDU29XhUyklN4pPyBUWG0eHjqeg/t3CG Ce4AtYauMVtbmwFCuMspUrcBGowW983KcIK4imb2Np+ecCUquOvPj+Zvw/cHpM4O6SWjfJ uQv5IK8Tlgf4AMC3JEaXHEc3TGn4/o7sOJ2C8vxQ+DtePXjQNBEArF3ksHWFqUIVpQVTmK Ng6k2h4QLIPIvCdw4rQIziDLksXWBbiNnpHTFR73XYuagHkJMooB6NoL4x3HW75BFGowtz u5cNkav9lZCa+we4b9aef/TrkkukwWLk5doNNbKCEzAQt0GkccOWGsg7Ewc2vA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1668568991; a=rsa-sha256; cv=none; b=HRlAyQ8cziDJde5oLWq9s+5fDtxqzqabRZXcquo59Xyqcjlq4fwzhav5UhqeUr09VLBzO0 s/zbvU8RAEj344lHEPPZ+LCmv7qhE+7cHYdunRobA21FdKVrUpvRUT4DB47EDDMeKur+Ut q5kLMxd4iPgYG7v9BHHDdfT8+z4cExkUQXTUeLsywx4BbuirIeEJ5m3vfJ2XsxnYWzAfY9 SuU3Kz8PsTmW+XGe+cLRnpgyxuzWUf9qQWHlfTZylH0S7Ju21oIMDI1ZO7tIVkF1FqaW3Z vZHJzoKOgNlv+KYQ7T9TMqFfWhCZD4vVvlORZGpYOrW2IOYdAYPqQvahOAMZsw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NBpKC5l6jzn5R; Wed, 16 Nov 2022 03:23:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2AG3NB1j055772; Wed, 16 Nov 2022 03:23:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AG3NB9F055771; Wed, 16 Nov 2022 03:23:11 GMT (envelope-from git) Date: Wed, 16 Nov 2022 03:23:11 GMT Message-Id: <202211160323.2AG3NB9F055771@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 6100374ccf26 - main - diff: Don't (ab)use sprintf() as a kind of strcat(). List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6100374ccf2644d3fd233bde8b8f4e73d9953c30 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=6100374ccf2644d3fd233bde8b8f4e73d9953c30 commit 6100374ccf2644d3fd233bde8b8f4e73d9953c30 Author: John Baldwin AuthorDate: 2022-11-16 03:17:36 +0000 Commit: John Baldwin 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"); + } } /*