bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* Possible testing case of snprintf.
@ 2020-02-09 14:44 Mats Erik Andersson
  2020-02-09 15:24 ` Tim Rühsen
  0 siblings, 1 reply; 20+ messages in thread
From: Mats Erik Andersson @ 2020-02-09 14:44 UTC (permalink / raw)
  To: bug-gnulib

Hello there!

This note has its origin in a report received at bug-inetutils.
The following test code for snprintf() is a simplyfied detection
I have implemented as a warning-only test in Gnu Inetutils.
My point is that Linux/glibc and kfreebsd/glibc triggers this
warning, but OpenSolaris, OpenIndiana, FreeBSD, OpenBSD, NetBSD,
and DragonflyBSD do not! Reading the replacement code for the
Gnulib module snprintf, neither would your function, should it
undergo the test. In conclusion, this is a case where the native
glibc function snprintf() behaves worse than does your replacement.

  #define MESSAGE       "try a fool"
  #define WRONG_MESSAGE "fool"

  char msg[sizeof (MESSAGE)] = "try a ";

  snprintf (msg, sizeof (msg), "%s%s", msg, WRONG_MESSAGE);

  if (!strcmp (msg, WRONG_MESSAGE))
    printf ("Warning! snprintf got confused!\n");

Observe that `msg' is target, as well as source. POSIX mentions
nothing about such a use case, but glibc will produce "fool",
whereas all BSD unices as well as OpenSolaris descendants will
produce "try a fool". Tacitly, POSIX would probably cry out
a statement like "Undefined"!

It is my opinion that this discrepancies should at least be documented
in 'snprintf.texi', were you not to take matter so far as to include
this as some sort of test, after due elaboration.

Gnu Inetutils uses legacy code from BSD4.4, where the related use
is present and did not cause troubles, but as the recent report
submitted to us, the code does produce portability issues when
brought to glibc. It has taken a long time to discover this state
of affaires, but now it has surfaced!

On behalf of Gnu Inetutils,

  Mats Erik Andersson.


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

end of thread, other threads:[~2020-02-24 19:18 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-09 14:44 Possible testing case of snprintf Mats Erik Andersson
2020-02-09 15:24 ` Tim Rühsen
2020-02-10  3:02   ` restrict Bruno Haible
2020-02-10  8:39     ` restrict Jeffrey Walton
2020-02-10 10:11     ` restrict Tim Rühsen
2020-02-16 21:44     ` restrict Paul Eggert
2020-02-16 23:49       ` restrict Bruno Haible
2020-02-17  0:14         ` restrict Bruno Haible
2020-02-17  2:46           ` restrict Paul Eggert
2020-02-17  3:53             ` restrict Bruno Haible
2020-02-17  9:51               ` restrict Tim Rühsen
2020-02-17 21:03                 ` restrict Bruno Haible
2020-02-17 18:03         ` restrict Paul Eggert
2020-02-17 20:55           ` restrict Bruno Haible
2020-02-17 22:28             ` restrict Paul Eggert
2020-02-17 21:19     ` restrict - summary Bruno Haible
2020-02-22 22:51       ` Bruno Haible
2020-02-24 19:17         ` Eric Blake
2020-02-23 12:39       ` Bruno Haible
2020-02-23 13:30       ` Bruno Haible

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