Fix usage of xstrncpy
authorNelson Ferreira <nelson.ferreira@ieee.org>
Sun, 1 Jul 2012 03:03:17 +0000 (23:03 -0400)
committerNelson Ferreira <nelson.ferreira@ieee.org>
Sun, 1 Jul 2012 03:03:17 +0000 (23:03 -0400)
* lib-src/fakemail.c (make_file_preface): make sure the len
argument to xstrcpy is always the available buffer len

        * lib-src/ootags.c: Only define etags_strchr and etags_strrchr if
configure did not detect the right ones.

        * lib-src/ootags.c (consider_token): Ditto

        * lib-src/ootags.c (C_entries): Ditto

        * lib-src/ootags.c (Pascal_functions): Ditto

        * lib-src/ootags.c (Prolog_functions): Ditto

        * lib-src/ootags.c (Erlang_functions): Ditto

* lib-src/ootags.c (savenstr): Ditto.

        * lib-src/ootags.c (substitute): Properly account for remaining
buffer available.

* lib-src/gnuserv.c (handle_ipc_request): use buffer size instead
of message length for copy. also no need to \0 terminate with
xstrncpy

* lib-src/pop.c (pop_stat): Use xstrncpy here.

* src/dumper.c (pdump_load): No need for \0 termination here.

* src/emacs.c (make_docfile): use remaining size of buffer, not
len of source..

* src/sysdep.h (x__dirname): Use just len. xstrncpy will stop early.

* src/sysdep.h (xdirname): Oops, need +1 for the \0 terminator.

* src/ui/redisplay.c (add_bufbyte_string_runes): Account for the \0 terminator.

Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
lib-src/fakemail.c
lib-src/gnuserv.c
lib-src/ootags.c
lib-src/pop.c
src/dumper.c
src/emacs.c
src/sysdep.h
src/ui/redisplay.c

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