public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
@ 2020-04-16  9:42 allison.karlitskaya at redhat dot com
  2020-04-16 10:12 ` [Bug c/94615] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: allison.karlitskaya at redhat dot com @ 2020-04-16  9:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

            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 thing
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 case).

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 the
destination size.  Particularly in this case.


Perhaps there is some attribute or other hint that can be added to the struct
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 would
be nice if there were a way to avoid this warning that didn't involve a mess of
#if and #pragma.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/94615] -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
  2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
@ 2020-04-16 10:12 ` rguenth at gcc dot gnu.org
  2020-04-16 10:17 ` fw at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-04-16 10:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Maybe use memcpy then, str* routines generally expect nul-termination

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/94615] -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
  2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
  2020-04-16 10:12 ` [Bug c/94615] " rguenth at gcc dot gnu.org
@ 2020-04-16 10:17 ` fw at gcc dot gnu.org
  2020-04-16 17:38 ` allison.karlitskaya at redhat dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: fw at gcc dot gnu.org @ 2020-04-16 10:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

Florian Weimer <fw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fw at gcc dot gnu.org

--- Comment #2 from Florian Weimer <fw at gcc dot gnu.org> ---
(In reply to Allison Karlitskaya from comment #0)
> 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
> the destination size.  Particularly in this case.

If this is true (of which I'm not yet sure), we are missing a nonstring
attribute in the glibc header. Either way, it's not a GCC bug, sorry.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/94615] -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
  2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
  2020-04-16 10:12 ` [Bug c/94615] " rguenth at gcc dot gnu.org
  2020-04-16 10:17 ` fw at gcc dot gnu.org
@ 2020-04-16 17:38 ` allison.karlitskaya at redhat dot com
  2020-04-16 17:44 ` allison.karlitskaya at redhat dot com
  2020-04-16 17:58 ` allison.karlitskaya at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: allison.karlitskaya at redhat dot com @ 2020-04-16 17:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

--- Comment #3 from Allison Karlitskaya <allison.karlitskaya at redhat dot com> ---
(In reply to Richard Biener from comment #1)
> Maybe use memcpy then, str* routines generally expect nul-termination

"str* routines generally expect nul-termination" doesn't really fly as an
argument, I'm afraid.  I'm using strncpy() because I've very carefully read the
manpage and determined that its standards-specified behaviour is exactly what I
want, and this behaviour specifically includes the possibility of no nul
termination.

(In reply to Florian Weimer from comment #2)
> If this is true (of which I'm not yet sure), we are missing a nonstring
> attribute in the glibc header. Either way, it's not a GCC bug, sorry.

That's really useful information.  I didn't know that this attribute already
exists.  Now just to convince the glibc people to add it...

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/94615] -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
  2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
                   ` (2 preceding siblings ...)
  2020-04-16 17:38 ` allison.karlitskaya at redhat dot com
@ 2020-04-16 17:44 ` allison.karlitskaya at redhat dot com
  2020-04-16 17:58 ` allison.karlitskaya at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: allison.karlitskaya at redhat dot com @ 2020-04-16 17:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

Allison Karlitskaya <allison.karlitskaya at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Allison Karlitskaya <allison.karlitskaya at redhat dot com> ---
Looks like they already added it for utmp, but not for lastlog:

https://sourceware.org/bugzilla/show_bug.cgi?id=24899

I'll file a new bug over there.

I guess this bug can probably be closed now.  Thanks for the pointer!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/94615] -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp)
  2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
                   ` (3 preceding siblings ...)
  2020-04-16 17:44 ` allison.karlitskaya at redhat dot com
@ 2020-04-16 17:58 ` allison.karlitskaya at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: allison.karlitskaya at redhat dot com @ 2020-04-16 17:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94615

Allison Karlitskaya <allison.karlitskaya at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=94626

--- Comment #5 from Allison Karlitskaya <allison.karlitskaya at redhat dot com> ---
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94626

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-04-16 17:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16  9:42 [Bug c/94615] New: -Wstringop-truncation warns on strncpy() with struct lastlog (or utmp) allison.karlitskaya at redhat dot com
2020-04-16 10:12 ` [Bug c/94615] " rguenth at gcc dot gnu.org
2020-04-16 10:17 ` fw at gcc dot gnu.org
2020-04-16 17:38 ` allison.karlitskaya at redhat dot com
2020-04-16 17:44 ` allison.karlitskaya at redhat dot com
2020-04-16 17:58 ` allison.karlitskaya at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).