coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

removing coreutils' final strncpy uses


From: Jim Meyering
Subject: removing coreutils' final strncpy uses
Date: Sun, 15 Jul 2012 15:57:14 +0200

Remember the strncpy prohibition I added to "make syntax-check"
not too long ago?  I exempted the uses in pinky.c and who.c
because those programs don't really matter and their uses were
not obviously bad.  Plus, I just didn't feel like it.

Well, today I did, so removed them and, along the way, was surprised
to see that while not officially wrong, they were unwarranted:  they
would uselessly zero-pad out to the end of each destination buffer.
It felt good to remove them (and the exemption), making the
code more compact and more readable, to boot.

The 2nd commit below updates to the latest from gnulib.


>From f79263da4da048bf3a071e22530a0d2c02aff0df Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 15 Jul 2012 11:38:51 +0200
Subject: [PATCH 1/2] maint: remove unwarranted uses of strncpy

* src/pinky.c (print_entry): Remove unwarranted uses of strncpy.
Instead, use stpcpy and stpncpy.
* src/who.c (print_user): Likewise.
* cfg.mk: Remove strncpy exemptions.
---
 cfg.mk      |  3 ---
 src/pinky.c | 21 ++++++---------------
 src/who.c   | 19 +++++--------------
 3 files changed, 11 insertions(+), 32 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index 6620eaf..790238c 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -493,6 +493,3 @@ exclude_file_name_regexp--sc_prohibit_test_backticks = \
 # Exempt test.c, since it's nominally shared, and relatively static.
 exclude_file_name_regexp--sc_prohibit_operator_at_end_of_line = \
   ^src/(ptx|test|head)\.c$$
-
-# Exempt pinky and who: their uses of this function appear to be correct.
-exclude_file_name_regexp--sc_prohibit_strncpy = ^src/(pinky|who)\.c$$
diff --git a/src/pinky.c b/src/pinky.c
index 597bc56..c01b124 100644
--- a/src/pinky.c
+++ b/src/pinky.c
@@ -208,21 +208,14 @@ print_entry (const STRUCT_UTMP *utmp_ent)
 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)

   char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
+  char *p = line;

   /* Copy ut_line into LINE, prepending '/dev/' if ut_line is not
      already an absolute file name.  Some system may put the full,
      absolute file name in ut_line.  */
-  if (utmp_ent->ut_line[0] == '/')
-    {
-      strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
-      line[sizeof (utmp_ent->ut_line)] = '\0';
-    }
-  else
-    {
-      strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
-      strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line, sizeof 
utmp_ent->ut_line);
-      line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
-    }
+  if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
+    p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
+  stpncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));

   if (stat (line, &stats) == 0)
     {
@@ -242,8 +235,7 @@ print_entry (const STRUCT_UTMP *utmp_ent)
       struct passwd *pw;
       char name[UT_USER_SIZE + 1];

-      strncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
-      name[UT_USER_SIZE] = '\0';
+      stpncpy (name, UT_USER (utmp_ent), UT_USER_SIZE);
       pw = getpwnam (name);
       if (pw == NULL)
         /* TRANSLATORS: Real name is unknown; at most 19 characters. */
@@ -284,8 +276,7 @@ print_entry (const STRUCT_UTMP *utmp_ent)
       char *display = NULL;

       /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
-      strncpy (ut_host, utmp_ent->ut_host, (int) sizeof (utmp_ent->ut_host));
-      ut_host[sizeof (utmp_ent->ut_host)] = '\0';
+      stpncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));

       /* Look for an X display.  */
       display = strchr (ut_host, ':');
diff --git a/src/who.c b/src/who.c
index c875b1d..3ad8004 100644
--- a/src/who.c
+++ b/src/who.c
@@ -342,23 +342,15 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)

   char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
+  char *p = line;
   PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);

   /* Copy ut_line into LINE, prepending '/dev/' if ut_line is not
      already an absolute file name.  Some systems may put the full,
      absolute file name in ut_line.  */
-  if (utmp_ent->ut_line[0] == '/')
-    {
-      strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
-      line[sizeof (utmp_ent->ut_line)] = '\0';
-    }
-  else
-    {
-      strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
-      strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
-               sizeof (utmp_ent->ut_line));
-      line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
-    }
+  if ( ! IS_ABSOLUTE_FILE_NAME (utmp_ent->ut_line))
+    p = stpcpy (p, DEV_DIR_WITH_TRAILING_SLASH);
+  stpncpy (p, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));

   if (stat (line, &stats) == 0)
     {
@@ -384,8 +376,7 @@ print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
       char *display = NULL;

       /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
-      strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
-      ut_host[sizeof (utmp_ent->ut_host)] = '\0';
+      stpncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));

       /* Look for an X display.  */
       display = strchr (ut_host, ':');
--
1.7.11.2.194.g7bdb748


>From e380221333f4efd8d49b29b2690375c36eb90505 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 15 Jul 2012 13:50:36 +0200
Subject: [PATCH 2/2] build: update gnulib submodule to latest

---
 gnulib | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gnulib b/gnulib
index d8f90ad..696aa74 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit d8f90adf5f01512958b6da46bd5eea01294a434e
+Subproject commit 696aa74b38f8c109c21e4c01194faab53fb4cefc
--
1.7.11.2.194.g7bdb748



reply via email to

[Prev in Thread] Current Thread [Next in Thread]