I'd really rather not promote the use of strlcpy in GNU code, as it is
contrary to GNU style. Plus, I'm not a fan of __warn_unused_result__; in
my experience it's too often more trouble than it's worth, and I expect
this trouble would occur with strlcpy if we started using it.
That being said, I take your point that I should not have used strncpy
to pacify GCC in that test case: I plead guilty to being lazy because
the test-case code already has arbitrary limits.
Anyway, strlcpy is overkill here, as snprintf does the job just as well
here, and is already standard and supported by Gnulib. So I propose the
attached simpler patch instead.
>From 2513ff5a2d0cb11edbca47d5ad6b1b5ae0c690fa Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 27 Sep 2017 16:16:43 -0700
Subject: [PATCH] duplocale-tests: use snprintf, not strncpy
* modules/duplocale-tests (Depends-on): Add snprintf.
* tests/test-duplocale.c (get_locale_dependent_values):
Use snprintf instead of strncpy to avoid overrunning
the fixed-size output buffer.
---
ChangeLog | 8 ++++++++
modules/duplocale-tests | 1 +
tests/test-duplocale.c | 4 ++--
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 8a9bbe625..7aa6c03d7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2017-09-27 Paul Eggert <egg...@cs.ucla.edu>
+
+ duplocale-tests: use snprintf, not strncpy
+ * modules/duplocale-tests (Depends-on): Add snprintf.
+ * tests/test-duplocale.c (get_locale_dependent_values):
+ Use snprintf instead of strncpy to avoid overrunning
+ the fixed-size output buffer.
+
2017-09-26 Bruno Haible <br...@clisp.org>
uniname/uniname-tests: Tighten code.
diff --git a/modules/duplocale-tests b/modules/duplocale-tests
index baa9a6310..98f85b448 100644
--- a/modules/duplocale-tests
+++ b/modules/duplocale-tests
@@ -5,6 +5,7 @@ tests/macros.h
Depends-on:
langinfo
+snprintf
configure.ac:
AC_CHECK_FUNCS_ONCE([duplocale uselocale strfmon_l snprintf_l nl_langinfo_l])
diff --git a/tests/test-duplocale.c b/tests/test-duplocale.c
index f48fedf6a..c812f0b82 100644
--- a/tests/test-duplocale.c
+++ b/tests/test-duplocale.c
@@ -42,14 +42,14 @@ struct locale_dependent_values
static void
get_locale_dependent_values (struct locale_dependent_values *result)
{
+ size_t ntime = sizeof result->time;
strfmon (result->monetary, sizeof (result->monetary),
"%n", 123.75);
/* result->monetary is usually "$123.75" */
snprintf (result->numeric, sizeof (result->numeric),
"%g", 3.5);
/* result->numeric is usually "3,5" */
- strncpy (result->time, nl_langinfo (MON_1), sizeof result->time - 1);
- result->time[sizeof result->time - 1] = '\0';
+ ASSERT (snprintf (result->time, ntime, "%s", nl_langinfo (MON_1)) < ntime);
/* result->time is usually "janvier" */
}
--
2.13.5