emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] /srv/bzr/emacs/trunk r109003: Simplify by avoiding confusi


From: Paul Eggert
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r109003: Simplify by avoiding confusing use of strncpy etc.
Date: Tue, 10 Jul 2012 14:48:34 -0700
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 109003
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Tue 2012-07-10 14:48:34 -0700
message:
  Simplify by avoiding confusing use of strncpy etc.
modified:
  lib-src/ChangeLog
  lib-src/etags.c
  lib-src/make-docfile.c
  lib-src/movemail.c
  lib-src/pop.c
  src/ChangeLog
  src/doc.c
  src/fileio.c
  src/frame.c
  src/gtkutil.c
  src/keyboard.c
  src/lread.c
  src/nsmenu.m
  src/nsterm.m
  src/process.c
  src/regex.c
  src/s/gnu-linux.h
  src/s/sol2-6.h
  src/s/unixware.h
  src/sysdep.c
  src/widget.c
  src/xdisp.c
=== modified file 'lib-src/ChangeLog'
--- a/lib-src/ChangeLog 2012-07-09 16:38:45 +0000
+++ b/lib-src/ChangeLog 2012-07-10 21:48:34 +0000
@@ -1,3 +1,22 @@
+2012-07-10  Paul Eggert  <address@hidden>
+
+       Simplify by avoiding confusing use of strncpy etc.
+       * etags.c (write_classname, C_entries):
+       Use sprintf rather than strncpy or strncat.
+       * etags.c (consider_token, C_entries, HTML_labels, Prolog_functions)
+       (Erlang_functions, substitute, readline_internal, savenstr):
+       * movemail.c (mail_spool_name):
+       Use memcpy rather than strncpy or strncat when either will do.
+       * make-docfile.c (write_c_args):
+       Use memcmp rather than strncmp when either will do.
+       * movemail.c (pop_retr):
+       * pop.c (pop_stat, pop_list, pop_multi_first, pop_last)
+       (socket_connection, pop_getline, sendline, getok):
+       Use snprintf rather than strncpy or strncat.
+       * movemail.c (concat): Remove; no longer needed.
+       (xmalloc): Define only if needed, now that concat has gone away.
+       Return void *.  All uses changed.
+
 2012-07-09  Paul Eggert  <address@hidden>
 
        Add GCC-style 'const' attribute to functions that can use it.

=== modified file 'lib-src/etags.c'
--- a/lib-src/etags.c   2012-07-09 16:38:45 +0000
+++ b/lib-src/etags.c   2012-07-10 21:48:34 +0000
@@ -2642,17 +2642,11 @@
     }
   for (i = 1; i < cstack.nl; i++)
     {
-      char *s;
-      int slen;
-
-      s = cstack.cname[i];
+      char *s = cstack.cname[i];
       if (s == NULL)
        continue;
-      slen = strlen (s);
-      len += slen + qlen;
-      linebuffer_setlen (cn, len);
-      strncat (cn->buffer, qualifier, qlen);
-      strncat (cn->buffer, s, slen);
+      linebuffer_setlen (cn, len + qlen + strlen (s));
+      len += sprintf (cn->buffer + len, "%s%s", qualifier, s);
     }
 }
 
@@ -2867,7 +2861,7 @@
           fvdef = fvnone;
           objdef = omethodtag;
           linebuffer_setlen (&token_name, len);
-          strncpy (token_name.buffer, str, len);
+          memcpy (token_name.buffer, str, len);
           token_name.buffer[len] = '\0';
           return TRUE;
         }
@@ -2879,10 +2873,11 @@
      case omethodparm:
        if (parlev == 0)
         {
+          int oldlen = token_name.len;
           fvdef = fvnone;
           objdef = omethodtag;
-          linebuffer_setlen (&token_name, token_name.len + len);
-          strncat (token_name.buffer, str, len);
+          linebuffer_setlen (&token_name, oldlen + len);
+          memcpy (token_name.buffer + oldlen, str, len);
           return TRUE;
         }
        return FALSE;
@@ -3311,12 +3306,12 @@
                              && nestlev > 0 && definedef == dnone)
                            /* in struct body */
                            {
+                             int len;
                               write_classname (&token_name, qualifier);
-                             linebuffer_setlen (&token_name,
-                                                token_name.len+qlen+toklen);
-                             strcat (token_name.buffer, qualifier);
-                             strncat (token_name.buffer,
-                                      newlb.buffer + tokoff, toklen);
+                             len = token_name.len;
+                             linebuffer_setlen (&token_name, len+qlen+toklen);
+                             sprintf (token_name.buffer + len, "%s%.*s",
+                                      qualifier, toklen, newlb.buffer + 
tokoff);
                              token.named = TRUE;
                            }
                          else if (objdef == ocatseen)
@@ -3324,11 +3319,8 @@
                            {
                              int len = strlen (objtag) + 2 + toklen;
                              linebuffer_setlen (&token_name, len);
-                             strcpy (token_name.buffer, objtag);
-                             strcat (token_name.buffer, "(");
-                             strncat (token_name.buffer,
-                                      newlb.buffer + tokoff, toklen);
-                             strcat (token_name.buffer, ")");
+                             sprintf (token_name.buffer, "%s(%.*s)",
+                                      objtag, toklen, newlb.buffer + tokoff);
                              token.named = TRUE;
                            }
                          else if (objdef == omethodtag
@@ -3352,8 +3344,8 @@
                                  len -= 1;
                                }
                              linebuffer_setlen (&token_name, len);
-                             strncpy (token_name.buffer,
-                                      newlb.buffer + off, len);
+                             memcpy (token_name.buffer,
+                                     newlb.buffer + off, len);
                              token_name.buffer[len] = '\0';
                              if (defun)
                                while (--len >= 0)
@@ -3364,8 +3356,8 @@
                          else
                            {
                              linebuffer_setlen (&token_name, toklen);
-                             strncpy (token_name.buffer,
-                                      newlb.buffer + tokoff, toklen);
+                             memcpy (token_name.buffer,
+                                     newlb.buffer + tokoff, toklen);
                              token_name.buffer[toklen] = '\0';
                              /* Name macros and members. */
                              token.named = (structdef == stagseen
@@ -5161,7 +5153,7 @@
                  for (end = dbp; *end != '\0' && intoken (*end); end++)
                    continue;
                linebuffer_setlen (&token_name, end - dbp);
-               strncpy (token_name.buffer, dbp, end - dbp);
+               memcpy (token_name.buffer, dbp, end - dbp);
                token_name.buffer[end - dbp] = '\0';
 
                dbp = end;
@@ -5261,7 +5253,7 @@
          else if (len + 1 > allocated)
            xrnew (last, len + 1, char);
          allocated = len + 1;
-         strncpy (last, cp, len);
+         memcpy (last, cp, len);
          last[len] = '\0';
        }
     }
@@ -5434,7 +5426,7 @@
          else if (len + 1 > allocated)
            xrnew (last, len + 1, char);
          allocated = len + 1;
-         strncpy (last, cp, len);
+         memcpy (last, cp, len);
          last[len] = '\0';
        }
     }
@@ -5817,7 +5809,7 @@
       {
        dig = *out - '0';
        diglen = regs->end[dig] - regs->start[dig];
-       strncpy (t, in + regs->start[dig], diglen);
+       memcpy (t, in + regs->start[dig], diglen);
        t += diglen;
       }
     else
@@ -6040,7 +6032,7 @@
          filebuf.size *= 2;
          xrnew (filebuf.buffer, filebuf.size, char);
        }
-      strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len);
+      memcpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len);
       filebuf.len += lbp->len;
       filebuf.buffer[filebuf.len++] = '\n';
       filebuf.buffer[filebuf.len] = '\0';
@@ -6263,7 +6255,7 @@
   register char *dp;
 
   dp = xnew (len + 1, char);
-  strncpy (dp, cp, len);
+  memcpy (dp, cp, len);
   dp[len] = '\0';
   return dp;
 }

=== modified file 'lib-src/make-docfile.c'
--- a/lib-src/make-docfile.c    2012-07-06 19:50:17 +0000
+++ b/lib-src/make-docfile.c    2012-07-10 21:48:34 +0000
@@ -541,7 +541,7 @@
 
          /* In C code, `default' is a reserved word, so we spell it
             `defalt'; demangle that here.  */
-         if (ident_length == 6 && strncmp (ident_start, "defalt", 6) == 0)
+         if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0)
            fprintf (out, "DEFAULT");
          else
            while (ident_length-- > 0)

=== modified file 'lib-src/movemail.c'
--- a/lib-src/movemail.c        2012-06-24 17:39:14 +0000
+++ b/lib-src/movemail.c        2012-07-10 21:48:34 +0000
@@ -141,8 +141,9 @@
 static void error (const char *s1, const char *s2, const char *s3);
 static _Noreturn void pfatal_with_name (char *name);
 static _Noreturn void pfatal_and_delete (char *name);
-static char *concat (const char *s1, const char *s2, const char *s3);
-static long *xmalloc (unsigned int size);
+#ifdef MAIL_USE_MAILLOCK
+static void *xmalloc (size_t size);
+#endif
 #ifdef MAIL_USE_POP
 static int popmail (char *mailbox, char *outfile, int preserve, char 
*password, int reverse_order);
 static int pop_retr (popserver server, int msgno, FILE *arg);
@@ -301,7 +302,7 @@
           inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
           inname_dirlen--)
        continue;
-      tempname = (char *) xmalloc (inname_dirlen + sizeof "EXXXXXX");
+      tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX");
 
       while (1)
        {
@@ -583,8 +584,8 @@
   if (stat (MAILDIR, &stat1) < 0)
     return NULL;
 
-  indir = (char *) xmalloc (fname - inname + 1);
-  strncpy (indir, inname, fname - inname);
+  indir = xmalloc (fname - inname + 1);
+  memcpy (indir, inname, fname - inname);
   indir[fname-inname] = '\0';
 
 
@@ -644,32 +645,18 @@
   fatal ("%s for %s", s, name);
 }
 
-/* Return a newly-allocated string whose contents concatenate those of s1, s2, 
s3.  */
-
-static char *
-concat (const char *s1, const char *s2, const char *s3)
-{
-  size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
-  char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
-
-  strcpy (result, s1);
-  strcpy (result + len1, s2);
-  strcpy (result + len1 + len2, s3);
-  *(result + len1 + len2 + len3) = 0;
-
-  return result;
-}
-
+#ifdef MAIL_USE_MAILLOCK
 /* Like malloc but get fatal error if memory is exhausted.  */
 
-static long *
-xmalloc (unsigned int size)
+static void *
+xmalloc (size_t size)
 {
-  long *result = (long *) malloc (size);
+  void *result = malloc (size);
   if (!result)
     fatal ("virtual memory exhausted", 0, 0);
   return result;
 }
+#endif
 
 /* This is the guts of the interface to the Post Office Protocol.  */
 
@@ -851,10 +838,7 @@
 
   if (pop_retrieve_first (server, msgno, &line))
     {
-      char *msg = concat ("Error from POP server: ", pop_error, "");
-      strncpy (Errmsg, msg, sizeof (Errmsg));
-      Errmsg[sizeof (Errmsg)-1] = '\0';
-      free (msg);
+      snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error);
       return (NOTOK);
     }
 
@@ -873,10 +857,7 @@
 
   if (ret)
     {
-      char *msg = concat ("Error from POP server: ", pop_error, "");
-      strncpy (Errmsg, msg, sizeof (Errmsg));
-      Errmsg[sizeof (Errmsg)-1] = '\0';
-      free (msg);
+      snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error);
       return (NOTOK);
     }
 

=== modified file 'lib-src/pop.c'
--- a/lib-src/pop.c     2012-06-26 01:05:39 +0000
+++ b/lib-src/pop.c     2012-07-10 21:48:34 +0000
@@ -340,10 +340,7 @@
   if (strncmp (fromserver, "+OK ", 4))
     {
       if (0 == strncmp (fromserver, "-ERR", 4))
-       {
-         strncpy (pop_error, fromserver, ERROR_MAX);
-         pop_error[ERROR_MAX-1] = '\0';
-       }
+       snprintf (pop_error, ERROR_MAX, "%s", fromserver);
       else
        {
          strcpy (pop_error,
@@ -444,10 +441,7 @@
       if (strncmp (fromserver, "+OK ", 4))
        {
          if (! strncmp (fromserver, "-ERR", 4))
-           {
-             strncpy (pop_error, fromserver, ERROR_MAX);
-             pop_error[ERROR_MAX-1] = '\0';
-           }
+           snprintf (pop_error, ERROR_MAX, "%s", fromserver);
          else
            {
              strcpy (pop_error,
@@ -686,8 +680,7 @@
 
   if (0 == strncmp (*response, "-ERR", 4))
     {
-      strncpy (pop_error, *response, ERROR_MAX);
-      pop_error[ERROR_MAX-1] = '\0';
+      snprintf (pop_error, ERROR_MAX, "%s", *response);
       return (-1);
     }
   else if (0 == strncmp (*response, "+OK", 3))
@@ -860,8 +853,7 @@
 
   if (! strncmp (fromserver, "-ERR", 4))
     {
-      strncpy (pop_error, fromserver, ERROR_MAX);
-      pop_error[ERROR_MAX-1] = '\0';
+      snprintf (pop_error, ERROR_MAX, "%s", fromserver);
       return (-1);
     }
   else if (strncmp (fromserver, "+OK ", 4))
@@ -1061,9 +1053,8 @@
   sock = socket (PF_INET, SOCK_STREAM, 0);
   if (sock < 0)
     {
-      strcpy (pop_error, POP_SOCKET_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (POP_SOCKET_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s",
+               POP_SOCKET_ERROR, strerror (errno));
       return (-1);
 
     }
@@ -1139,9 +1130,7 @@
   if (! connect_ok)
     {
       CLOSESOCKET (sock);
-      strcpy (pop_error, CONNECT_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (CONNECT_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s", CONNECT_ERROR, strerror (errno));
       return (-1);
 
     }
@@ -1159,9 +1148,8 @@
            krb5_auth_con_free (kcontext, auth_context);
          if (kcontext)
            krb5_free_context (kcontext);
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, error_message (rem),
-                  ERROR_MAX - sizeof (KRB_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s",
+                   KRB_ERROR, error_message (rem));
          CLOSESOCKET (sock);
          return (-1);
        }
@@ -1199,30 +1187,19 @@
       krb5_free_principal (kcontext, server);
       if (rem)
        {
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, error_message (rem),
-                  ERROR_MAX - sizeof (KRB_ERROR));
+         int pop_error_len = snprintf (pop_error, ERROR_MAX, "%s%s",
+                                       KRB_ERROR, error_message (rem));
 #if defined HAVE_KRB5_ERROR_TEXT
          if (err_ret && err_ret->text.length)
            {
-             strncat (pop_error, " [server says '",
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, err_ret->text.data,
-                      min (ERROR_MAX - strlen (pop_error) - 1,
-                           err_ret->text.length));
-             strncat (pop_error, "']",
-                      ERROR_MAX - strlen (pop_error) - 1);
+             int errlen = err_ret->text.length;
+             snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len,
+                       " [server says '.*%s']", errlen, err_ret->text.data);
            }
 #elif defined HAVE_KRB5_ERROR_E_TEXT
-         if (err_ret && err_ret->e_text && strlen (*err_ret->e_text))
-           {
-             strncat (pop_error, " [server says '",
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, *err_ret->e_text,
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, "']",
-                      ERROR_MAX - strlen (pop_error) - 1);
-           }
+         if (err_ret && err_ret->e_text && **err_ret->e_text)
+           snprintf (pop_error + pop_error_len, ERRMAX - pop_error_len,
+                     " [server says '%s']", *err_ret->e_text);
 #endif
          if (err_ret)
            krb5_free_error (kcontext, err_ret);
@@ -1243,9 +1220,7 @@
       free ((char *) ticket);
       if (rem != KSUCCESS)
        {
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, krb_err_txt[rem],
-                  ERROR_MAX - sizeof (KRB_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s", KRB_ERROR, krb_err_txt[rem]);
          CLOSESOCKET (sock);
          return (-1);
        }
@@ -1350,9 +1325,8 @@
                  server->buffer_size - server->data - 1, 0);
       if (ret < 0)
        {
-         strcpy (pop_error, GETLINE_ERROR);
-         strncat (pop_error, strerror (errno),
-                  ERROR_MAX - sizeof (GETLINE_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s",
+                   GETLINE_ERROR, strerror (errno));
          pop_trash (server);
          return (-1);
        }
@@ -1436,9 +1410,7 @@
   if (ret < 0)
     {
       pop_trash (server);
-      strcpy (pop_error, SENDLINE_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (SENDLINE_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s", SENDLINE_ERROR, strerror 
(errno));
       return (ret);
     }
 
@@ -1500,8 +1472,7 @@
     return (0);
   else if (! strncmp (fromline, "-ERR", 4))
     {
-      strncpy (pop_error, fromline, ERROR_MAX);
-      pop_error[ERROR_MAX-1] = '\0';
+      snprintf (pop_error, ERROR_MAX, "%s", fromline);
       return (-1);
     }
   else

=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2012-07-10 19:04:14 +0000
+++ b/src/ChangeLog     2012-07-10 21:48:34 +0000
@@ -1,3 +1,37 @@
+2012-07-10  Paul Eggert  <address@hidden>
+
+       Simplify by avoiding confusing use of strncpy etc.
+       * doc.c (Fsnarf_documentation):
+       * fileio.c (Ffile_name_directory, Fsubstitute_in_file_name):
+       * frame.c (Fmake_terminal_frame):
+       * gtkutil.c (get_utf8_string):
+       * lread.c (openp):
+       * nsmenu.m (ns_update_menubar):
+       * regex.c (regerror):
+       Prefer memcpy to strncpy and strncat when either will do.
+       * fileio.c (Fsubstitute_in_file_name):
+       * keyboard.c (MULTI_LETTER_MOD, parse_modifiers_uncached)
+       (menu_separator_name_p):
+       * nsmenu.m (ns_update_menubar):
+       Prefer memcmp to strncmp when either will do.
+       * nsterm.m: Include <ftoastr.h>.
+       (ns_get_color):
+       * s/gnu-linux.h, s/sol2-6.h, s/unixware.h (PTY_TTY_NAME_SPRINTF):
+       Prefer snprintf to strncpy.
+       * nsterm.m (ns_term_init):
+       * widget.c (set_frame_size) [0]: Prefer xstrdup to xmalloc + strncpy.
+       * nsterm.m (ns_term_init):
+       Avoid the need for strncpy, by using build_string or
+       make_unibyte_string directly.  Use dtoastr, not snprintf.
+       * process.c (Fmake_network_process): Diagnose service names that
+       are too long, rather than silently truncating them or creating
+       non-null-terminated names.
+       (Fnetwork_interface_info): Likewise, for interface names.
+       * sysdep.c (system_process_attributes) [GNU_LINUX]:
+       Prefer sprintf to strncat.
+       * xdisp.c (debug_method_add) [GLYPH_DEBUG]:
+       Prefer vsnprintf to vsprintf + strncpy.
+
 2012-07-10  Glenn Morris  <address@hidden>
 
        * dispnew.c (PENDING_OUTPUT_COUNT) [!__GNU_LIBRARY__]:

=== modified file 'src/doc.c'
--- a/src/doc.c 2012-07-10 16:53:26 +0000
+++ b/src/doc.c 2012-07-10 21:48:34 +0000
@@ -645,7 +645,7 @@
                 {
                   ptrdiff_t len = end - p - 2;
                   char *fromfile = alloca (len + 1);
-                  strncpy (fromfile, &p[2], len);
+                  memcpy (fromfile, &p[2], len);
                   fromfile[len] = 0;
                   if (fromfile[len-1] == 'c')
                     fromfile[len-1] = 'o';

=== modified file 'src/fileio.c'
--- a/src/fileio.c      2012-07-10 08:43:46 +0000
+++ b/src/fileio.c      2012-07-10 21:48:34 +0000
@@ -365,7 +365,7 @@
 
       if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':')
        {
-         strncpy (res, beg, 2);
+         memcpy (res, beg, 2);
          beg += 2;
          r += 2;
        }
@@ -1648,7 +1648,7 @@
 
        /* Copy out the variable name.  */
        target = alloca (s - o + 1);
-       strncpy (target, o, s - o);
+       memcpy (target, o, s - o);
        target[s - o] = 0;
 #ifdef DOS_NT
        strupr (target); /* $home == $HOME etc.  */
@@ -1711,7 +1711,7 @@
 
        /* Copy out the variable name.  */
        target = alloca (s - o + 1);
-       strncpy (target, o, s - o);
+       memcpy (target, o, s - o);
        target[s - o] = 0;
 #ifdef DOS_NT
        strupr (target); /* $home == $HOME etc.  */
@@ -1732,13 +1732,13 @@
            orig = make_unibyte_string (o, orig_length);
            decoded = DECODE_FILE (orig);
            decoded_length = SBYTES (decoded);
-           strncpy (x, SSDATA (decoded), decoded_length);
+           memcpy (x, SDATA (decoded), decoded_length);
            x += decoded_length;
 
            /* If environment variable needed decoding, return value
               needs to be multibyte.  */
            if (decoded_length != orig_length
-               || strncmp (SSDATA (decoded), o, orig_length))
+               || memcmp (SDATA (decoded), o, orig_length))
              multibyte = 1;
          }
       }

=== modified file 'src/frame.c'
--- a/src/frame.c       2012-07-10 16:53:26 +0000
+++ b/src/frame.c       2012-07-10 21:48:34 +0000
@@ -646,7 +646,7 @@
       if (!NILP (tty))
         {
           name = alloca (SBYTES (tty) + 1);
-          strncpy (name, SSDATA (tty), SBYTES (tty));
+          memcpy (name, SSDATA (tty), SBYTES (tty));
           name[SBYTES (tty)] = 0;
         }
 
@@ -657,7 +657,7 @@
       if (!NILP (tty_type))
         {
           type = alloca (SBYTES (tty_type) + 1);
-          strncpy (type, SSDATA (tty_type), SBYTES (tty_type));
+          memcpy (type, SSDATA (tty_type), SBYTES (tty_type));
           type[SBYTES (tty_type)] = 0;
         }
 

=== modified file 'src/gtkutil.c'
--- a/src/gtkutil.c     2012-07-06 15:02:29 +0000
+++ b/src/gtkutil.c     2012-07-10 21:48:34 +0000
@@ -529,7 +529,7 @@
                                        &bytes_written, &err))
              && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
         {
-          strncpy (up, (char *)p, bytes_written);
+          memcpy (up, p, bytes_written);
           sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
           up += bytes_written+4;
           p += bytes_written+1;

=== modified file 'src/keyboard.c'
--- a/src/keyboard.c    2012-07-10 08:43:46 +0000
+++ b/src/keyboard.c    2012-07-10 21:48:34 +0000
@@ -6134,7 +6134,7 @@
 
 #define MULTI_LETTER_MOD(BIT, NAME, LEN)                       \
          if (i + LEN + 1 <= SBYTES (name)                      \
-             && ! strncmp (SSDATA (name) + i, NAME, LEN))      \
+             && ! memcmp (SDATA (name) + i, NAME, LEN))        \
            {                                                   \
              this_mod_end = i + LEN;                           \
              this_mod = BIT;                                   \
@@ -6172,13 +6172,13 @@
   if (! (modifiers & (down_modifier | drag_modifier
                      | double_modifier | triple_modifier))
       && i + 7 == SBYTES (name)
-      && strncmp (SSDATA (name) + i, "mouse-", 6) == 0
+      && memcmp (SDATA (name) + i, "mouse-", 6) == 0
       && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
     modifiers |= click_modifier;
 
   if (! (modifiers & (double_modifier | triple_modifier))
       && i + 6 < SBYTES (name)
-      && strncmp (SSDATA (name) + i, "wheel-", 6) == 0)
+      && memcmp (SDATA (name) + i, "wheel-", 6) == 0)
     modifiers |= click_modifier;
 
   if (modifier_end)
@@ -6630,7 +6630,7 @@
 
 #define MULTI_LETTER_MOD(BIT, NAME, LEN)               \
       if (LEN == SBYTES (name)                         \
-         && ! strncmp (SSDATA (name), NAME, LEN))      \
+         && ! memcmp (SDATA (name), NAME, LEN))        \
        return BIT;
 
     case 'A':
@@ -7418,7 +7418,7 @@
   if (!label)
     return 0;
   else if (strlen (label) > 3
-          && strncmp (label, "--", 2) == 0
+          && memcmp (label, "--", 2) == 0
           && label[2] != '-')
     {
       int i;

=== modified file 'src/lread.c'
--- a/src/lread.c       2012-07-10 08:43:46 +0000
+++ b/src/lread.c       2012-07-10 21:48:34 +0000
@@ -1495,26 +1495,14 @@
 
          /* Concatenate path element/specified name with the suffix.
             If the directory starts with /:, remove that.  */
-         if (SCHARS (filename) > 2
-             && SREF (filename, 0) == '/'
-             && SREF (filename, 1) == ':')
-           {
-             fnlen = SBYTES (filename) - 2;
-             strncpy (fn, SSDATA (filename) + 2, fnlen);
-             fn[fnlen] = '\0';
-           }
-         else
-           {
-             fnlen = SBYTES (filename);
-             strncpy (fn, SSDATA (filename), fnlen);
-             fn[fnlen] = '\0';
-           }
-
-         if (lsuffix != 0)  /* Bug happens on CCI if lsuffix is 0.  */
-           {
-             strncat (fn, SSDATA (XCAR (tail)), lsuffix);
-             fnlen += lsuffix;
-           }
+         int prefixlen = ((SCHARS (filename) > 2
+                           && SREF (filename, 0) == '/'
+                           && SREF (filename, 1) == ':')
+                          ? 2 : 0);
+         fnlen = SBYTES (filename) - prefixlen;
+         memcpy (fn, SDATA (filename) + prefixlen, fnlen);
+         memcpy (fn + fnlen, SDATA (XCAR (tail)), lsuffix + 1);
+         fnlen += lsuffix;
          /* Check that the file exists and is not a directory.  */
          /* We used to only check for handlers on non-absolute file names:
                if (absolute)

=== modified file 'src/nsmenu.m'
--- a/src/nsmenu.m      2012-07-05 18:35:48 +0000
+++ b/src/nsmenu.m      2012-07-10 21:48:34 +0000
@@ -426,7 +426,8 @@
                   break;
               else
                 continue;
-              if (strncmp (previous_strings[i], SDATA (string), 10))
+              if (memcmp (previous_strings[i], SDATA (string),
+                         min (10, SBYTES (string) + 1)))
                 break;
             }
 
@@ -447,7 +448,8 @@
            break;
 
           if (n < 100)
-            strncpy (previous_strings[i/4], SDATA (string), 10);
+           memcpy (previous_strings[i/4], min (10, SBYTES (string) + 1),
+                   SDATA (string));
 
          wv = xmalloc_widget_value ();
          wv->name = SSDATA (string);

=== modified file 'src/nsterm.m'
--- a/src/nsterm.m      2012-07-10 01:25:07 +0000
+++ b/src/nsterm.m      2012-07-10 21:48:34 +0000
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <setjmp.h>
 #include <c-strcase.h>
+#include <ftoastr.h>
 
 #include "lisp.h"
 #include "blockinput.h"
@@ -1442,21 +1443,16 @@
       [scanner scanFloat: &b];
     }
   else if (!strncmp(name, "rgb:", 4))  /* A newer X11 format -- rgb:r/g/b */
-    {
-      strncpy (hex, name + 4, 19);
-      hex[19] = '\0';
-      scaling = (strlen(hex) - 2) / 3;
-    }
+    scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
   else if (name[0] == '#')        /* An old X11 format; convert to newer */
     {
       int len = (strlen(name) - 1);
       int start = (len % 3 == 0) ? 1 : len / 4 + 1;
       int i;
       scaling = strlen(name+start) / 3;
-      for (i=0; i<3; i++) {
-        strncpy(hex + i * (scaling + 1), name + start + i * scaling, scaling);
-        hex[(i+1) * (scaling + 1) - 1] = '/';
-      }
+      for (i = 0; i < 3; i++)
+       snprintf (hex + i * (scaling + 1), "%.*s/", scaling,
+                 name + start + i * scaling);
       hex[3 * (scaling + 1) - 1] = '\0';
     }
 
@@ -4107,10 +4103,7 @@
                                 ns_display_name_list);
   dpyinfo->name_list_element = XCAR (ns_display_name_list);
 
-  /* Set the name of the terminal. */
-  terminal->name = xmalloc (SBYTES (display_name) + 1);
-  strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
-  terminal->name[SBYTES (display_name)] = 0;
+  terminal->name = xstrdup (SSDATA (display_name));
 
   UNBLOCK_INPUT;
 
@@ -4167,14 +4160,14 @@
   }
 
   {
-    char c[128];
 #ifdef NS_IMPL_GNUSTEP
-    strncpy (c, gnustep_base_version, sizeof (c));
+    Vwindow_system_version = build_string (gnustep_base_version);
 #else
     /*PSnextrelease (128, c); */
-    snprintf (c, sizeof (c), "%g", NSAppKitVersionNumber);
+    char c[DBL_BUFSIZE_BOUND];
+    int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber);
+    Vwindow_system_version = make_unibyte_string (c, len);
 #endif
-    Vwindow_system_version = build_string (c);
   }
 
   delete_keyboard_wait_descriptor (0);

=== modified file 'src/process.c'
--- a/src/process.c     2012-07-09 21:12:08 +0000
+++ b/src/process.c     2012-07-10 21:48:34 +0000
@@ -3013,7 +3013,9 @@
       CHECK_STRING (service);
       memset (&address_un, 0, sizeof address_un);
       address_un.sun_family = AF_LOCAL;
-      strncpy (address_un.sun_path, SSDATA (service), sizeof 
address_un.sun_path);
+      if (sizeof address_un.sun_path <= SBYTES (service))
+       error ("Service name too long");
+      strcpy (address_un.sun_path, SSDATA (service));
       ai.ai_addr = (struct sockaddr *) &address_un;
       ai.ai_addrlen = sizeof address_un;
       goto open_socket;
@@ -3717,8 +3719,9 @@
 
   CHECK_STRING (ifname);
 
-  memset (rq.ifr_name, 0, sizeof rq.ifr_name);
-  strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name));
+  if (sizeof rq.ifr_name <= SBYTES (ifname))
+    error ("interface name too long");
+  strcpy (rq.ifr_name, SSDATA (ifname));
 
   s = socket (AF_INET, SOCK_STREAM, 0);
   if (s < 0)

=== modified file 'src/regex.c'
--- a/src/regex.c       2012-07-05 18:35:48 +0000
+++ b/src/regex.c       2012-07-10 21:48:34 +0000
@@ -6644,7 +6644,7 @@
     {
       if (msg_size > errbuf_size)
        {
-         strncpy (errbuf, msg, errbuf_size - 1);
+         memcpy (errbuf, msg, errbuf_size - 1);
          errbuf[errbuf_size - 1] = 0;
        }
       else

=== modified file 'src/s/gnu-linux.h'
--- a/src/s/gnu-linux.h 2012-06-12 19:03:32 +0000
+++ b/src/s/gnu-linux.h 2012-07-10 21:48:34 +0000
@@ -63,8 +63,7 @@
        close (fd);                                     \
        return -1;                                      \
       }                                                        \
-    strncpy (pty_name, ptyname, sizeof (pty_name));    \
-    pty_name[sizeof (pty_name) - 1] = 0;               \
+    snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
     sigunblock (sigmask (SIGCHLD));                    \
   }
 

=== modified file 'src/s/sol2-6.h'
--- a/src/s/sol2-6.h    2012-04-14 06:18:49 +0000
+++ b/src/s/sol2-6.h    2012-07-10 21:48:34 +0000
@@ -54,8 +54,7 @@
       { emacs_close (fd); return -1; }         \
     if (!(ptyname = ptsname (fd)))             \
       { emacs_close (fd); return -1; }         \
-    strncpy (pty_name, ptyname, sizeof (pty_name)); \
-    pty_name[sizeof (pty_name) - 1] = 0;       \
+    snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
   }
 
 #define GC_SETJMP_WORKS 1

=== modified file 'src/s/unixware.h'
--- a/src/s/unixware.h  2012-07-10 07:37:17 +0000
+++ b/src/s/unixware.h  2012-07-10 21:48:34 +0000
@@ -40,8 +40,7 @@
       fatal("could not unlock slave pty");     \
     if (!(ptyname = ptsname(fd)))              \
       fatal ("could not enable slave pty");    \
-    strncpy(pty_name, ptyname, sizeof(pty_name)); \
-    pty_name[sizeof(pty_name) - 1] = 0;                \
+    snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
   }
 
 /* Conservative garbage collection has not been tested, so for now

=== modified file 'src/sysdep.c'
--- a/src/sysdep.c      2012-07-07 18:16:15 +0000
+++ b/src/sysdep.c      2012-07-10 21:48:34 +0000
@@ -2744,9 +2744,11 @@
   char procbuf[1025], *p, *q;
   int fd;
   ssize_t nread;
-  const char *cmd = NULL;
+  static char const default_cmd[] = "???";
+  const char *cmd = default_cmd;
+  int cmdsize = sizeof default_cmd - 1;
   char *cmdline = NULL;
-  ptrdiff_t cmdsize = 0, cmdline_size;
+  ptrdiff_t cmdline_size;
   unsigned char c;
   printmax_t proc_id;
   int ppid, pgrp, sess, tty, tpgid, thcount;
@@ -2808,11 +2810,6 @@
        }
       else
        q = NULL;
-      if (cmd == NULL)
-       {
-         cmd = "???";
-         cmdsize = 3;
-       }
       /* Command name is encoded in locale-coding-system; decode it.  */
       cmd_str = make_unibyte_string (cmd, cmdsize);
       decoded_cmd = code_convert_string_norecord (cmd_str,
@@ -2950,14 +2947,9 @@
        }
       if (!cmdline_size)
        {
-         if (!cmd)
-           cmd = "???";
-         if (!cmdsize)
-           cmdsize = strlen (cmd);
          cmdline_size = cmdsize + 2;
          cmdline = xmalloc (cmdline_size + 1);
-         strcpy (cmdline, "[");
-         strcat (strncat (cmdline, cmd, cmdsize), "]");
+         sprintf (cmdline, "[%.*s]", cmdsize, cmd);
        }
       emacs_close (fd);
       /* Command line is encoded in locale-coding-system; decode it.  */

=== modified file 'src/widget.c'
--- a/src/widget.c      2012-07-05 06:32:41 +0000
+++ b/src/widget.c      2012-07-10 21:48:34 +0000
@@ -429,25 +429,15 @@
       {
        /* the tricky things with the sign is to make sure that
           -0 is printed -0. */
-       int len;
-       char *tem;
        sprintf (shell_position, "=%c%d%c%d",
                 flags & XNegative ? '-' : '+', x < 0 ? -x : x,
                 flags & YNegative ? '-' : '+', y < 0 ? -y : y);
-       len = strlen (shell_position) + 1;
-       tem = xmalloc (len);
-       strncpy (tem, shell_position, len);
-       XtVaSetValues (wmshell, XtNgeometry, tem, NULL);
+       XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL);
       }
     else if (flags & (WidthValue | HeightValue))
       {
-       int len;
-       char *tem;
        sprintf (shell_position, "=%dx%d", pixel_width, pixel_height);
-       len = strlen (shell_position) + 1;
-       tem = xmalloc (len);
-       strncpy (tem, shell_position, len);
-       XtVaSetValues (wmshell, XtNgeometry, tem, NULL);
+       XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL);
       }
 
     /* If the geometry spec we're using has W/H components, mark the size

=== modified file 'src/xdisp.c'
--- a/src/xdisp.c       2012-07-10 08:43:46 +0000
+++ b/src/xdisp.c       2012-07-10 21:48:34 +0000
@@ -12492,23 +12492,21 @@
 static void
 debug_method_add (struct window *w, char const *fmt, ...)
 {
-  char buffer[512];
   char *method = w->desired_matrix->method;
   int len = strlen (method);
   int size = sizeof w->desired_matrix->method;
   int remaining = size - len - 1;
   va_list ap;
 
-  va_start (ap, fmt);
-  vsprintf (buffer, fmt, ap);
-  va_end (ap);
   if (len && remaining)
     {
       method[len] = '|';
       --remaining, ++len;
     }
 
-  strncpy (method + len, buffer, remaining);
+  va_start (ap, fmt);
+  vsnprintf (method + len, remaining + 1, fmt, ap);
+  va_end (ap);
 
   if (trace_redisplay_p)
     fprintf (stderr, "%p (%s): %s\n",
@@ -12517,7 +12515,7 @@
               && STRINGP (BVAR (XBUFFER (w->buffer), name)))
              ? SSDATA (BVAR (XBUFFER (w->buffer), name))
              : "no buffer"),
-            buffer);
+            method + len);
 }
 
 #endif /* GLYPH_DEBUG */


reply via email to

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