Fix xstrncpy usage, also fixes etags. From Nelson
authorSteve Youngs <steve@sxemacs.org>
Sun, 1 Jul 2012 04:23:35 +0000 (14:23 +1000)
committerSteve Youngs <steve@sxemacs.org>
Sun, 1 Jul 2012 04:23:35 +0000 (14:23 +1000)
* merges:
  Fix usage of xstrncpy
  Simple comment reflow to 80 cols
  Fix etags xstrncpy usage, and some other niceties

lib-src/etags.c
lib-src/fakemail.c
lib-src/gnuserv.c
lib-src/ootags.c
lib-src/pop.c
src/dumper.c
src/emacs.c
src/extents.h
src/sysdep.h
src/ui/redisplay.c

index 6823750..5271c6e 100644 (file)
@@ -399,10 +399,6 @@ static char *skip_spaces __P((char *));
 static char *skip_non_spaces __P((char *));
 static char *savenstr __P((char *, int));
 static char *savestr __P((char *));
-static char *etags_strchr __P((const char *, int));
-static char *etags_strrchr __P((const char *, int));
-static int etags_strcasecmp __P((const char *, const char *));
-static int etags_strncasecmp __P((const char *, const char *, int));
 static char *etags_getcwd __P((void));
 static char *relative_filename __P((char *, char *));
 static char *absolute_filename __P((char *, char *));
@@ -414,6 +410,112 @@ static void linebuffer_setlen __P((linebuffer *, int));
 static PTR xmalloc __P((unsigned int));
 static PTR xrealloc __P((char *, unsigned int));
 
+
+\f
+#if HAVE_STRRCHR
+#define etags_strrchr strrchr
+#else
+/*
+ * Return the ptr in sp at which the character c last
+ * appears; NULL if not found
+ *
+ * Identical to POSIX strrchr, included for portability.
+ */
+static char *
+etags_strrchr (sp, c)
+register const char *sp;
+register int c;
+{
+       register const char *r;
+
+       r = NULL;
+       do
+       {
+               if (*sp == c)
+                       r = sp;
+       } while (*sp++);
+       return (char *)r;
+}
+#endif
+
+#if HAVE_STRCHR
+#define etags_strchr strchr
+#else
+/*
+ * Return the ptr in sp at which the character c first
+ * appears; NULL if not found
+ *
+ * Identical to POSIX strchr, included for portability.
+ */
+static char *
+etags_strchr (sp, c)
+register const char *sp;
+register int c;
+{
+       do
+       {
+               if (*sp == c)
+                       return (char *)sp;
+       } while (*sp++);
+       return NULL;
+}
+#endif
+
+#if HAVE_STRCASECMP
+#define etags_strcasecmp strcasecmp
+#else
+/*
+ * Compare two strings, ignoring case for alphabetic characters.
+ *
+ * Same as BSD's strcasecmp, included for portability.
+ */
+static int
+etags_strcasecmp (s1, s2)
+register const char *s1;
+register const char *s2;
+{
+       while (*s1 != '\0'
+              && (ISALPHA (*s1) && ISALPHA (*s2)
+                  ? lowcase (*s1) == lowcase (*s2)
+                  : *s1 == *s2))
+               s1++, s2++;
+
+       return (ISALPHA (*s1) && ISALPHA (*s2)
+               ? lowcase (*s1) - lowcase (*s2)
+               : *s1 - *s2);
+}
+#endif
+
+#if HAVE_STRCASECMP
+#define etags_strncasecmp strncasecmp
+#else
+/*
+ * Compare two strings, ignoring case for alphabetic characters.
+ * Stop after a given number of characters
+ *
+ * Same as BSD's strncasecmp, included for portability.
+ */
+static int
+etags_strncasecmp (s1, s2, n)
+register const char *s1;
+register const char *s2;
+register int n;
+{
+       while (*s1 != '\0' && n-- > 0
+              && (ISALPHA (*s1) && ISALPHA (*s2)
+                  ? lowcase (*s1) == lowcase (*s2)
+                  : *s1 == *s2))
+               s1++, s2++;
+
+       if (n < 0)
+               return 0;
+       else
+               return (ISALPHA (*s1) && ISALPHA (*s2)
+                       ? lowcase (*s1) - lowcase (*s2)
+                       : *s1 - *s2);
+}
+#endif
+
 \f
 static char searchar = '/';    /* use /.../ searches */
 
@@ -2985,8 +3087,8 @@ bool *is_func_or_var;     /* OUT: function or variable found */
                {
                        fvdef = fvnone;
                        objdef = omethodtag;
-                       linebuffer_setlen (&token_name, len);
-                       xstrncpy (token_name.buffer, str, len);
+                       linebuffer_setlen (&token_name, len+1);
+                       xstrncpy (token_name.buffer, str, len+1);
                        token_name.buffer[len] = '\0';
                        return TRUE;
                }
@@ -3532,10 +3634,9 @@ FILE *inf;                       /* input file */
                                                                        off += 1;
                                                                        len -= 1;
                                                                }
-                                                               linebuffer_setlen (&token_name, len);
+                                                               linebuffer_setlen (&token_name, len+1);
                                                                xstrncpy (token_name.buffer,
-                                                                        newlb.buffer + off, len);
-                                                               token_name.buffer[len] = '\0';
+                                                                        newlb.buffer + off, len+1);
                                                                if (defun)
                                                                        while (--len >= 0)
                                                                                if (token_name.buffer[len] == '_')
@@ -3544,9 +3645,9 @@ FILE *inf;                        /* input file */
                                                        }
                                                        else
                                                        {
-                                                               linebuffer_setlen (&token_name, toklen);
+                                                               linebuffer_setlen (&token_name, toklen+1);
                                                                xstrncpy (token_name.buffer,
-                                                                        newlb.buffer + tokoff, toklen);
+                                                                        newlb.buffer + tokoff, toklen+1);
                                                                token_name.buffer[toklen] = '\0';
                                                                /* Name macros and members. */
                                                                token.named = (structdef == stagseen
@@ -5026,8 +5127,8 @@ FILE *inf;
                                continue;
 
                        /* Save all values for later tagging. */
-                       linebuffer_setlen (&tline, lb.len);
-                       xstrncpy(tline.buffer, lb.buffer, lb.len-1);
+                       linebuffer_setlen (&tline, lb.len+1);
+                       xstrncpy(tline.buffer, lb.buffer, lb.len+1);
                        save_lineno = lineno;
                        save_lcno = linecharno;
                        name = tline.buffer + (dbp - lb.buffer);
@@ -5505,8 +5606,8 @@ FILE * inf;
                                        else
                                                for (end = dbp; *end != '\0' && intoken (*end); end++)
                                                        continue;
-                                       linebuffer_setlen (&token_name, end - dbp);
-                                       xstrncpy (token_name.buffer, dbp, end - dbp);
+                                       linebuffer_setlen (&token_name, end - dbp+1);
+                                       xstrncpy (token_name.buffer, dbp, end - dbp+1);
                                        token_name.buffer[end - dbp] = '\0';
 
                                        dbp = end;
@@ -5607,8 +5708,7 @@ FILE *inf;
                        else if (len + 1 > allocated)
                                xrnew (last, len + 1, char);
                        allocated = len + 1;
-                       xstrncpy (last, cp, len);
-                       last[len] = '\0';
+                       xstrncpy (last, cp, len+1);
                }
        }
        free (last);
@@ -5785,8 +5885,7 @@ FILE *inf;
                        else if (len + 1 > allocated)
                                xrnew (last, len + 1, char);
                        allocated = len + 1;
-                       xstrncpy (last, cp, len);
-                       last[len] = '\0';
+                       xstrncpy (last, cp, allocated);
                }
        }
        free (last);
@@ -6173,18 +6272,22 @@ struct re_registers *regs;
 
        /* Allocate space and do the substitutions. */
        assert (size >= 0);
-       result = xnew (size + 1, char);
+       size_t avail = size + 1;
+       result = xnew (avail, char);
 
        for (t = result; *out != '\0'; out++)
                if (*out == '\\' && ISDIGIT (*++out))
                {
                        dig = *out - '0';
                        diglen = regs->end[dig] - regs->start[dig];
-                       xstrncpy (t, in + regs->start[dig], diglen);
+                       xstrncpy (t, in + regs->start[dig], avail);
                        t += diglen;
+                       avail -= diglen;
                }
-               else
+               else {
                        *t++ = *out;
+                       avail --;
+               }
        *t = '\0';
 
        assert (t <= result + size);
@@ -6400,13 +6503,12 @@ register FILE *stream;
        if (need_filebuf                /* we need filebuf for multi-line regexps */
            && chars_deleted > 0)       /* not at EOF */
        {
-               while (filebuf.size <= filebuf.len + lbp->len + 1) /* +1 for \n */
-               {
-                       /* Expand filebuf. */
+               size_t need = filebuf.len + lbp->len + 1; /* +1 for \n */
+               while (filebuf.size <= need ) 
                        filebuf.size *= 2;
-                       xrnew (filebuf.buffer, filebuf.size, char);
-               }
-               xstrncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len);
+               /* Expand filebuf. */
+               xrnew (filebuf.buffer, filebuf.size, char);
+               xstrncpy (filebuf.buffer + filebuf.len, lbp->buffer, filebuf.size - filebuf.len);
                filebuf.len += lbp->len;
                filebuf.buffer[filebuf.len++] = '\n';
                filebuf.buffer[filebuf.len] = '\0';
@@ -6637,98 +6739,11 @@ int len;
        register char *dp;
 
        dp = xnew (len + 1, char);
-       xstrncpy (dp, cp, len);
+       xstrncpy (dp, cp, len+1);
        dp[len] = '\0';
        return dp;
 }
 
-/*
- * Return the ptr in sp at which the character c last
- * appears; NULL if not found
- *
- * Identical to POSIX strrchr, included for portability.
- */
-static char *
-etags_strrchr (sp, c)
-register const char *sp;
-register int c;
-{
-       register const char *r;
-
-       r = NULL;
-       do
-       {
-               if (*sp == c)
-                       r = sp;
-       } while (*sp++);
-       return (char *)r;
-}
-
-/*
- * Return the ptr in sp at which the character c first
- * appears; NULL if not found
- *
- * Identical to POSIX strchr, included for portability.
- */
-static char *
-etags_strchr (sp, c)
-register const char *sp;
-register int c;
-{
-       do
-       {
-               if (*sp == c)
-                       return (char *)sp;
-       } while (*sp++);
-       return NULL;
-}
-
-/*
- * Compare two strings, ignoring case for alphabetic characters.
- *
- * Same as BSD's strcasecmp, included for portability.
- */
-static int
-etags_strcasecmp (s1, s2)
-register const char *s1;
-register const char *s2;
-{
-       while (*s1 != '\0'
-              && (ISALPHA (*s1) && ISALPHA (*s2)
-                  ? lowcase (*s1) == lowcase (*s2)
-                  : *s1 == *s2))
-               s1++, s2++;
-
-       return (ISALPHA (*s1) && ISALPHA (*s2)
-               ? lowcase (*s1) - lowcase (*s2)
-               : *s1 - *s2);
-}
-
-/*
- * Compare two strings, ignoring case for alphabetic characters.
- * Stop after a given number of characters
- *
- * Same as BSD's strncasecmp, included for portability.
- */
-static int
-etags_strncasecmp (s1, s2, n)
-register const char *s1;
-register const char *s2;
-register int n;
-{
-       while (*s1 != '\0' && n-- > 0
-              && (ISALPHA (*s1) && ISALPHA (*s2)
-                  ? lowcase (*s1) == lowcase (*s2)
-                  : *s1 == *s2))
-               s1++, s2++;
-
-       if (n < 0)
-               return 0;
-       else
-               return (ISALPHA (*s1) && ISALPHA (*s2)
-                       ? lowcase (*s1) - lowcase (*s2)
-                       : *s1 - *s2);
-}
 
 /* Skip spaces (end of string is not space), return new pointer. */
 static char *
@@ -6999,10 +7014,8 @@ linebuffer *lbp;
 int toksize;
 {
        while (lbp->size <= toksize)
-       {
                lbp->size *= 2;
-               xrnew (lbp->buffer, lbp->size, char);
-       }
+       xrnew (lbp->buffer, lbp->size, char);
        lbp->len = toksize;
 }
 
index 9da9121..525e9e6 100644 (file)
@@ -317,24 +317,24 @@ static line_list make_file_preface(void)
        temp = cuserid((char *)NULL);
        /* the_user */
        the_user_len = strlen(temp);
-       the_user = alloc_string(the_user_len + 1);
-       xstrncpy(the_user, the_user_len, temp);
+       the_user = alloc_string(the_user_len);
+       xstrncpy(the_user, temp, the_user_len+1);
        /* alloc the_string */
        the_string_len = 3 + prefix_length + the_user_len + date_length;
        the_string = alloc_string(the_string_len);
-       temp_len = the_string_len;
+       temp_len = the_string_len+1;
        temp = the_string;
-       xstrncpy(temp, temp_len, FROM_PREFIX);
+       xstrncpy(temp, FROM_PREFIX, temp_len);
 
        temp = &temp[prefix_length];
        *temp++ = ' ';
        temp_len -= prefix_length + 1;
-       xstrncpy(temp, temp_len, the_user);
+       xstrncpy(temp, the_user, temp_len);
 
        temp = &temp[the_user_len];
        *temp++ = ' ';
        temp_len -= the_user_len + 1;
-       xstrncpy(temp, temp_len, the_date);
+       xstrncpy(temp, the_date, temp_len);
 
        result = new_list();
        result->string = the_string;
index 08649b7..05a33e9 100644 (file)
@@ -193,9 +193,7 @@ handle_ipc_request(struct msgbuf *msgp)
        }
        /* if */
        msgctl(ipc_qid, IPC_STAT, &msg_st);
-       xstrncpy(buf, msgp->mtext, len);
-       /* terminate buf */
-       buf[len] = '\0';
+       xstrncpy(buf, msgp->mtext, sizeof(buf));
 
        printf("%d %s", ipc_qid, buf);
        fflush(stdout);
index 116bd3f..a167988 100644 (file)
@@ -306,8 +306,6 @@ char *skip_spaces PP((char *cp));
 char *skip_non_spaces PP((char *cp));
 char *savenstr PP((char *cp, int len));
 char *savestr PP((char *cp));
-char *etags_strchr PP((char *sp, int c));
-char *etags_strrchr PP((char *sp, int c));
 char *etags_getcwd PP((void));
 char *relative_filename PP((char *file, char *dir));
 char *absolute_filename PP((char *file, char *dir));
@@ -318,6 +316,55 @@ void grow_linebuffer PP((linebuffer * lbp, int toksize));
 long *xmalloc PP((unsigned int size));
 long *xrealloc PP((char *ptr, unsigned int size));
 \f
+#if HAVE_STRRCHR
+#define etags_strrchr strrchr
+#else
+/*
+ * Return the ptr in sp at which the character c last
+ * appears; NULL if not found
+ *
+ * Identical to POSIX strrchr, included for portability.
+ */
+static char *
+etags_strrchr (sp, c)
+register const char *sp;
+register int c;
+{
+       register const char *r;
+
+       r = NULL;
+       do
+       {
+               if (*sp == c)
+                       r = sp;
+       } while (*sp++);
+       return (char *)r;
+}
+#endif
+
+#if HAVE_STRCHR
+#define etags_strchr strchr
+#else
+/*
+ * Return the ptr in sp at which the character c first
+ * appears; NULL if not found
+ *
+ * Identical to POSIX strchr, included for portability.
+ */
+static char *
+etags_strchr (sp, c)
+register const char *sp;
+register int c;
+{
+       do
+       {
+               if (*sp == c)
+                       return (char *)sp;
+       } while (*sp++);
+       return NULL;
+}
+#endif
+\f
 char searchar = '/';           /* use /.../ searches */
 
 char *tagfile;                 /* output file */
@@ -2395,7 +2442,7 @@ bool *is_func_or_var;             /* OUT: function or variable found */
                        objdef = omethodtag;
                        methodlen = len;
                        grow_linebuffer(&token_name, methodlen + 1);
-                       xstrncpy(token_name.buffer, str, len);
+                       xstrncpy(token_name.buffer, str, methodlen+1);
                        token_name.buffer[methodlen] = '\0';
                        token_name.len = methodlen;
                        return TRUE;
@@ -2843,11 +2890,7 @@ FILE *inf;                       /* input file */
                                                                     newlb.
                                                                     buffer +
                                                                     tokoff,
-                                                                    toklen);
-                                                               token_name.
-                                                                   buffer
-                                                                   [toklen] =
-                                                                   '\0';
+                                                                    toklen + 1);
                                                                token_name.len =
                                                                    toklen;
                                                                /* Name macros. */
@@ -3911,7 +3954,7 @@ FILE *inf;
 
                        /* save all values for later tagging */
                        grow_linebuffer(&tline, lb.len + 1);
-                       xstrncpy(tline.buffer, lb.buffer, lb.len);
+                       xstrncpy(tline.buffer, lb.buffer, lb.len + 1);
                        save_lineno = lineno;
                        save_lcno = linecharno;
 
@@ -4300,8 +4343,7 @@ FILE *inf;
                        else if (len + 1 > allocated)
                                last = xrnew(last, len + 1, char);
                        allocated = len + 1;
-                       xstrncpy(last, cp, len);
-                       last[len] = '\0';
+                       xstrncpy(last, cp, allocated);
                }
        }
        free(last);
@@ -4455,8 +4497,7 @@ FILE *inf;
                        else if (len + 1 > allocated)
                                last = xrnew(last, len + 1, char);
                        allocated = len + 1;
-                       xstrncpy(last, cp, len);
-                       last[len] = '\0';
+                       xstrncpy(last, cp, allocated);
                }
        }
        free(last);
@@ -4747,17 +4788,21 @@ struct re_registers *regs;
                        size -= 1;
 
        /* Allocate space and do the substitutions. */
-       result = xnew(size + 1, char);
+       size_t avail = size + 1;
+       result = xnew(avail, char);
 
        for (t = result; *out != '\0'; out++)
                if (*out == '\\' && isdigit(*++out)) {
                        /* Using "dig2" satisfies my debugger.  Bleah. */
                        dig = *out - '0';
                        diglen = regs->end[dig] - regs->start[dig];
-                       xstrncpy(t, in + regs->start[dig], diglen);
+                       xstrncpy(t, in + regs->start[dig], avail);
                        t += diglen;
-               } else
+                       avail -= diglen;
+               } else {
                        *t++ = *out;
+                       avail --;
+               }
        *t = '\0';
 
        if (DEBUG && (t > result + size || t - result != strlen(result)))
@@ -4925,48 +4970,11 @@ int len;
        register char *dp;
 
        dp = xnew(len + 1, char);
-       xstrncpy(dp, cp, len);
+       xstrncpy(dp, cp, len+1);
        dp[len] = '\0';
        return dp;
 }
 
-/*
- * Return the ptr in sp at which the character c last
- * appears; NULL if not found
- *
- * Identical to System V strrchr, included for portability.
- */
-char *etags_strrchr(sp, c)
-register char *sp;
-register int c;
-{
-       register char *r;
-
-       r = NULL;
-       do {
-               if (*sp == c)
-                       r = sp;
-       } while (*sp++);
-       return r;
-}
-
-/*
- * Return the ptr in sp at which the character c first
- * appears; NULL if not found
- *
- * Identical to System V strchr, included for portability.
- */
-char *etags_strchr(sp, c)
-register char *sp;
-register int c;
-{
-       do {
-               if (*sp == c)
-                       return sp;
-       } while (*sp++);
-       return NULL;
-}
-
 /* Skip spaces, return new pointer. */
 char *skip_spaces(cp)
 char *cp;
index 931c9b0..f0ec06a 100644 (file)
@@ -354,9 +354,9 @@ pop_stat (server, count, size)
                if (0 == strncmp (fromserver, "-ERR", 4)) {
                        xstrncpy (pop_error, fromserver, ERROR_MAX);
                } else {
-                       strcpy (pop_error,
+                       xstrncpy (pop_error,
                                "Unexpected response from POP "
-                               "server in pop_stat");
+                                "server in pop_stat", ERROR_MAX);
                        pop_trash (server);
                }
                return (-1);
index 44b9d82..8e72ea4 100644 (file)
@@ -1380,7 +1380,6 @@ int pdump_load(const char *argv0)
                        }
                        if (remain > 0) {
                                xstrncpy(w, name, remain);
-                               w[remain]='\0';
                        }
 
                        /* Check that exe_path is executable and not a
index be33f63..58970a2 100644 (file)
@@ -821,9 +821,9 @@ make_docfile(int c, char **v)
        /* set up the program call */
        xstrncpy(mdocfile,
                 (char*)XSTRING_DATA(Vexec_directory),
-                XSTRING_LENGTH(Vexec_directory));
-       xstrncpy(mdocfile+XSTRING_LENGTH(Vexec_directory),
-                make_docfile_prog, countof(make_docfile_prog));
+                sizeof(mdocfile));
+       xstrncpy(mdocfile+edlen,
+                make_docfile_prog, sizeof(mdocfile)-edlen);
 
        /* find the --make-docfile option */
        for (p = v; *p; p++) {
index c25e324..6a096ee 100644 (file)
@@ -50,24 +50,29 @@ struct extent {
        struct {
                Lisp_Object face;
 
-               /* These flags are simply an optimization for common boolean properties
-                  which go onto the extent's property list.  Any of them would work if
-                  done in the normal way, but the space savings of doing these in this
-                  way is significant.  Note that if you add a flag, there are numerous
-                  places in extents.c that need to know about it.
-
-                  Another consideration is that some of these properties are accessed
-                  during redisplay, so it's good for access to them to be fast (a bit
-                  reference instead of a search down a plist).
-
-                  `begin_glyph_layout' and `end_glyph_layout' are unusual in that
-                  they have 4 states instead of 2.
-
-                  Other special extent properties are stored in an auxiliary
-                  structure that sits at the beginning of the plist.  The has_aux
-                  flag indicates whether this structure exists.  The has_parent
-                  flag is an optimization indicating whether the extent has a parent
-                  (this could also be determined by looking in the aux structure). */
+               /* These flags are simply an optimization for common
+                  boolean properties which go onto the extent's
+                  property list.  Any of them would work if done in
+                  the normal way, but the space savings of doing
+                  these in this way is significant.  Note that if you
+                  add a flag, there are numerous places in extents.c
+                  that need to know about it.
+
+                  Another consideration is that some of these
+                  properties are accessed during redisplay, so it's
+                  good for access to them to be fast (a bit reference
+                  instead of a search down a plist).
+
+                  `begin_glyph_layout' and `end_glyph_layout' are
+                  unusual in that they have 4 states instead of 2.
+
+                  Other special extent properties are stored in an
+                  auxiliary structure that sits at the beginning of
+                  the plist.  The has_aux flag indicates whether this
+                  structure exists.  The has_parent flag is an
+                  optimization indicating whether the extent has a
+                  parent (this could also be determined by looking in
+                  the aux structure). */
 
                 enum_field(glyph_layout) begin_glyph_layout:2;
                /*  2 text, margins, or whitespace */
index 4ee497d..62e2b6b 100644 (file)
@@ -241,7 +241,7 @@ x__dirname(char *restrict res, const char *file, size_t len)
        xstrncpy(res, file, len);
        /* if we were using side effects we woulda matched the above cond */
        result = dirname(res);
-       xstrncpy(res, result, xmin_size_t(len, xstrlen(result)));
+       xstrncpy(res, result, len);
        return;
 }
 #elif defined(HAVE_DIRNAME)
@@ -250,7 +250,7 @@ x__dirname(char *restrict res, const char *file, size_t len)
 {
        /* assumes res is malloc'd of size LEN */
        char *result = dirname(res);
-       xstrncpy(res, result, xmin_size_t(len, xstrlen(result)));
+       xstrncpy(res, result, len);
        return;
 }
 #endif
@@ -261,7 +261,7 @@ xdirname(const char *file)
 extern_inline char*
 xdirname(const char *file)
 {
-       size_t len = xstrlen(file);
+       size_t len = xstrlen(file)+1;
        char *res = xmalloc_atomic(len);
 
        x__dirname(res, file, len);
index 57b6317..1937f44 100644 (file)
@@ -1094,9 +1094,9 @@ static prop_block_dynarr *add_bufbyte_string_runes(pos_data * data,
 
                                pb.type = PROP_STRING;
                                pb.data.p_string.str =
-                                       xnew_atomic_array(Bufbyte, len);
+                                       xnew_atomic_array(Bufbyte, len+1);
                                xstrncpy((char *)pb.data.p_string.str,
-                                       (char *)pos, len);
+                                       (char *)pos, len+1);
                                pb.data.p_string.len = len;
 
                                Dynarr_add(prop, pb);