From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 984F3385B835; Thu, 16 Apr 2020 09:42:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 984F3385B835 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1587030138; bh=bC0jvyEBshoJNcPFgdvlTq+ji8E1M1GiEwlb/BuuN+U=; h=From:To:Subject:Date:From; b=D7UdfKGzQzTvmxxE/ECL4tCCN0s+BvOVBB1nw+vWaEkcw8bsog1QnaUeeheaUbjVD r6/di+GD9LOxyQM0Tlw4R5CMz/phT3JSfULL+ERQE3r5WXrsRa6lJpogz0Bqq+PH9s 6oHGhGuniNv/sBHnY3LPJaMf8ye9KO0gv7qpUMQk= From: "allison.karlitskaya at redhat dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) Date: Thu, 16 Apr 2020 09:42:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 8.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: allison.karlitskaya at redhat dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Apr 2020 09:42:18 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94615 Bug ID: 94615 Summary: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: allison.karlitskaya at redhat dot com Target Milestone: --- Consider this code: { struct lastlog entry; strncpy (entry.ll_host, rhost, sizeof entry.ll_host); /* fill other fields */ /* write record to the lastlog */ } strncpy() turns out to be exactly what you want in this case because the ll_host field in struct lastlog is only *optionally* nul terminated, in the case of the hostname being shorter than the field width. If the hostname is equal to or longer than the field length then you should write the whole th= ing in, without including a nul. Consequently, any reader code needs to be more careful about checking for both conditions (which it ought to be, in any ca= se). GCC 8 gives this warning, though: /usr/include/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound 256 equals destination size [-Wstringop-truncation] ... which on its face seems kinda funny, because the bound *should* equal t= he destination size. Particularly in this case. Perhaps there is some attribute or other hint that can be added to the stru= ct in question that the character array is intended to be only optionally nul terminated, and in that case, we could skip the warning? Otherwise, it wou= ld be nice if there were a way to avoid this warning that didn't involve a mes= s of #if and #pragma.=