diff options
| author | Paul Eggert | 2012-07-10 14:48:34 -0700 |
|---|---|---|
| committer | Paul Eggert | 2012-07-10 14:48:34 -0700 |
| commit | e99a530f8cdca3ccd9e739cd092ed9865d12fe89 (patch) | |
| tree | c2bef9f80ff9910be17757a83f61caed02146d0a | |
| parent | c59592b32f5b5808c12720bfd37ea73b473fa1db (diff) | |
| download | emacs-e99a530f8cdca3ccd9e739cd092ed9865d12fe89.tar.gz emacs-e99a530f8cdca3ccd9e739cd092ed9865d12fe89.zip | |
Simplify by avoiding confusing use of strncpy etc.
| -rw-r--r-- | lib-src/ChangeLog | 19 | ||||
| -rw-r--r-- | lib-src/etags.c | 56 | ||||
| -rw-r--r-- | lib-src/make-docfile.c | 2 | ||||
| -rw-r--r-- | lib-src/movemail.c | 45 | ||||
| -rw-r--r-- | lib-src/pop.c | 73 | ||||
| -rw-r--r-- | src/ChangeLog | 34 | ||||
| -rw-r--r-- | src/doc.c | 2 | ||||
| -rw-r--r-- | src/fileio.c | 10 | ||||
| -rw-r--r-- | src/frame.c | 4 | ||||
| -rw-r--r-- | src/gtkutil.c | 2 | ||||
| -rw-r--r-- | src/keyboard.c | 10 | ||||
| -rw-r--r-- | src/lread.c | 28 | ||||
| -rw-r--r-- | src/nsmenu.m | 6 | ||||
| -rw-r--r-- | src/nsterm.m | 27 | ||||
| -rw-r--r-- | src/process.c | 9 | ||||
| -rw-r--r-- | src/regex.c | 2 | ||||
| -rw-r--r-- | src/s/gnu-linux.h | 3 | ||||
| -rw-r--r-- | src/s/sol2-6.h | 3 | ||||
| -rw-r--r-- | src/s/unixware.h | 3 | ||||
| -rw-r--r-- | src/sysdep.c | 18 | ||||
| -rw-r--r-- | src/widget.c | 14 | ||||
| -rw-r--r-- | src/xdisp.c | 10 |
22 files changed, 170 insertions, 210 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index bc7c5d7114d..449985966a4 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog | |||
| @@ -1,3 +1,22 @@ | |||
| 1 | 2012-07-10 Paul Eggert <eggert@cs.ucla.edu> | ||
| 2 | |||
| 3 | Simplify by avoiding confusing use of strncpy etc. | ||
| 4 | * etags.c (write_classname, C_entries): | ||
| 5 | Use sprintf rather than strncpy or strncat. | ||
| 6 | * etags.c (consider_token, C_entries, HTML_labels, Prolog_functions) | ||
| 7 | (Erlang_functions, substitute, readline_internal, savenstr): | ||
| 8 | * movemail.c (mail_spool_name): | ||
| 9 | Use memcpy rather than strncpy or strncat when either will do. | ||
| 10 | * make-docfile.c (write_c_args): | ||
| 11 | Use memcmp rather than strncmp when either will do. | ||
| 12 | * movemail.c (pop_retr): | ||
| 13 | * pop.c (pop_stat, pop_list, pop_multi_first, pop_last) | ||
| 14 | (socket_connection, pop_getline, sendline, getok): | ||
| 15 | Use snprintf rather than strncpy or strncat. | ||
| 16 | * movemail.c (concat): Remove; no longer needed. | ||
| 17 | (xmalloc): Define only if needed, now that concat has gone away. | ||
| 18 | Return void *. All uses changed. | ||
| 19 | |||
| 1 | 2012-07-09 Paul Eggert <eggert@cs.ucla.edu> | 20 | 2012-07-09 Paul Eggert <eggert@cs.ucla.edu> |
| 2 | 21 | ||
| 3 | Add GCC-style 'const' attribute to functions that can use it. | 22 | Add GCC-style 'const' attribute to functions that can use it. |
diff --git a/lib-src/etags.c b/lib-src/etags.c index 7141811239f..69200b790fb 100644 --- a/lib-src/etags.c +++ b/lib-src/etags.c | |||
| @@ -2642,17 +2642,11 @@ write_classname (linebuffer *cn, const char *qualifier) | |||
| 2642 | } | 2642 | } |
| 2643 | for (i = 1; i < cstack.nl; i++) | 2643 | for (i = 1; i < cstack.nl; i++) |
| 2644 | { | 2644 | { |
| 2645 | char *s; | 2645 | char *s = cstack.cname[i]; |
| 2646 | int slen; | ||
| 2647 | |||
| 2648 | s = cstack.cname[i]; | ||
| 2649 | if (s == NULL) | 2646 | if (s == NULL) |
| 2650 | continue; | 2647 | continue; |
| 2651 | slen = strlen (s); | 2648 | linebuffer_setlen (cn, len + qlen + strlen (s)); |
| 2652 | len += slen + qlen; | 2649 | len += sprintf (cn->buffer + len, "%s%s", qualifier, s); |
| 2653 | linebuffer_setlen (cn, len); | ||
| 2654 | strncat (cn->buffer, qualifier, qlen); | ||
| 2655 | strncat (cn->buffer, s, slen); | ||
| 2656 | } | 2650 | } |
| 2657 | } | 2651 | } |
| 2658 | 2652 | ||
| @@ -2867,7 +2861,7 @@ consider_token (register char *str, register int len, register int c, int *c_ext | |||
| 2867 | fvdef = fvnone; | 2861 | fvdef = fvnone; |
| 2868 | objdef = omethodtag; | 2862 | objdef = omethodtag; |
| 2869 | linebuffer_setlen (&token_name, len); | 2863 | linebuffer_setlen (&token_name, len); |
| 2870 | strncpy (token_name.buffer, str, len); | 2864 | memcpy (token_name.buffer, str, len); |
| 2871 | token_name.buffer[len] = '\0'; | 2865 | token_name.buffer[len] = '\0'; |
| 2872 | return TRUE; | 2866 | return TRUE; |
| 2873 | } | 2867 | } |
| @@ -2879,10 +2873,11 @@ consider_token (register char *str, register int len, register int c, int *c_ext | |||
| 2879 | case omethodparm: | 2873 | case omethodparm: |
| 2880 | if (parlev == 0) | 2874 | if (parlev == 0) |
| 2881 | { | 2875 | { |
| 2876 | int oldlen = token_name.len; | ||
| 2882 | fvdef = fvnone; | 2877 | fvdef = fvnone; |
| 2883 | objdef = omethodtag; | 2878 | objdef = omethodtag; |
| 2884 | linebuffer_setlen (&token_name, token_name.len + len); | 2879 | linebuffer_setlen (&token_name, oldlen + len); |
| 2885 | strncat (token_name.buffer, str, len); | 2880 | memcpy (token_name.buffer + oldlen, str, len); |
| 2886 | return TRUE; | 2881 | return TRUE; |
| 2887 | } | 2882 | } |
| 2888 | return FALSE; | 2883 | return FALSE; |
| @@ -3311,12 +3306,12 @@ C_entries (int c_ext, FILE *inf) | |||
| 3311 | && nestlev > 0 && definedef == dnone) | 3306 | && nestlev > 0 && definedef == dnone) |
| 3312 | /* in struct body */ | 3307 | /* in struct body */ |
| 3313 | { | 3308 | { |
| 3309 | int len; | ||
| 3314 | write_classname (&token_name, qualifier); | 3310 | write_classname (&token_name, qualifier); |
| 3315 | linebuffer_setlen (&token_name, | 3311 | len = token_name.len; |
| 3316 | token_name.len+qlen+toklen); | 3312 | linebuffer_setlen (&token_name, len+qlen+toklen); |
| 3317 | strcat (token_name.buffer, qualifier); | 3313 | sprintf (token_name.buffer + len, "%s%.*s", |
| 3318 | strncat (token_name.buffer, | 3314 | qualifier, toklen, newlb.buffer + tokoff); |
| 3319 | newlb.buffer + tokoff, toklen); | ||
| 3320 | token.named = TRUE; | 3315 | token.named = TRUE; |
| 3321 | } | 3316 | } |
| 3322 | else if (objdef == ocatseen) | 3317 | else if (objdef == ocatseen) |
| @@ -3324,11 +3319,8 @@ C_entries (int c_ext, FILE *inf) | |||
| 3324 | { | 3319 | { |
| 3325 | int len = strlen (objtag) + 2 + toklen; | 3320 | int len = strlen (objtag) + 2 + toklen; |
| 3326 | linebuffer_setlen (&token_name, len); | 3321 | linebuffer_setlen (&token_name, len); |
| 3327 | strcpy (token_name.buffer, objtag); | 3322 | sprintf (token_name.buffer, "%s(%.*s)", |
| 3328 | strcat (token_name.buffer, "("); | 3323 | objtag, toklen, newlb.buffer + tokoff); |
| 3329 | strncat (token_name.buffer, | ||
| 3330 | newlb.buffer + tokoff, toklen); | ||
| 3331 | strcat (token_name.buffer, ")"); | ||
| 3332 | token.named = TRUE; | 3324 | token.named = TRUE; |
| 3333 | } | 3325 | } |
| 3334 | else if (objdef == omethodtag | 3326 | else if (objdef == omethodtag |
| @@ -3352,8 +3344,8 @@ C_entries (int c_ext, FILE *inf) | |||
| 3352 | len -= 1; | 3344 | len -= 1; |
| 3353 | } | 3345 | } |
| 3354 | linebuffer_setlen (&token_name, len); | 3346 | linebuffer_setlen (&token_name, len); |
| 3355 | strncpy (token_name.buffer, | 3347 | memcpy (token_name.buffer, |
| 3356 | newlb.buffer + off, len); | 3348 | newlb.buffer + off, len); |
| 3357 | token_name.buffer[len] = '\0'; | 3349 | token_name.buffer[len] = '\0'; |
| 3358 | if (defun) | 3350 | if (defun) |
| 3359 | while (--len >= 0) | 3351 | while (--len >= 0) |
| @@ -3364,8 +3356,8 @@ C_entries (int c_ext, FILE *inf) | |||
| 3364 | else | 3356 | else |
| 3365 | { | 3357 | { |
| 3366 | linebuffer_setlen (&token_name, toklen); | 3358 | linebuffer_setlen (&token_name, toklen); |
| 3367 | strncpy (token_name.buffer, | 3359 | memcpy (token_name.buffer, |
| 3368 | newlb.buffer + tokoff, toklen); | 3360 | newlb.buffer + tokoff, toklen); |
| 3369 | token_name.buffer[toklen] = '\0'; | 3361 | token_name.buffer[toklen] = '\0'; |
| 3370 | /* Name macros and members. */ | 3362 | /* Name macros and members. */ |
| 3371 | token.named = (structdef == stagseen | 3363 | token.named = (structdef == stagseen |
| @@ -5161,7 +5153,7 @@ HTML_labels (FILE *inf) | |||
| 5161 | for (end = dbp; *end != '\0' && intoken (*end); end++) | 5153 | for (end = dbp; *end != '\0' && intoken (*end); end++) |
| 5162 | continue; | 5154 | continue; |
| 5163 | linebuffer_setlen (&token_name, end - dbp); | 5155 | linebuffer_setlen (&token_name, end - dbp); |
| 5164 | strncpy (token_name.buffer, dbp, end - dbp); | 5156 | memcpy (token_name.buffer, dbp, end - dbp); |
| 5165 | token_name.buffer[end - dbp] = '\0'; | 5157 | token_name.buffer[end - dbp] = '\0'; |
| 5166 | 5158 | ||
| 5167 | dbp = end; | 5159 | dbp = end; |
| @@ -5261,7 +5253,7 @@ Prolog_functions (FILE *inf) | |||
| 5261 | else if (len + 1 > allocated) | 5253 | else if (len + 1 > allocated) |
| 5262 | xrnew (last, len + 1, char); | 5254 | xrnew (last, len + 1, char); |
| 5263 | allocated = len + 1; | 5255 | allocated = len + 1; |
| 5264 | strncpy (last, cp, len); | 5256 | memcpy (last, cp, len); |
| 5265 | last[len] = '\0'; | 5257 | last[len] = '\0'; |
| 5266 | } | 5258 | } |
| 5267 | } | 5259 | } |
| @@ -5434,7 +5426,7 @@ Erlang_functions (FILE *inf) | |||
| 5434 | else if (len + 1 > allocated) | 5426 | else if (len + 1 > allocated) |
| 5435 | xrnew (last, len + 1, char); | 5427 | xrnew (last, len + 1, char); |
| 5436 | allocated = len + 1; | 5428 | allocated = len + 1; |
| 5437 | strncpy (last, cp, len); | 5429 | memcpy (last, cp, len); |
| 5438 | last[len] = '\0'; | 5430 | last[len] = '\0'; |
| 5439 | } | 5431 | } |
| 5440 | } | 5432 | } |
| @@ -5817,7 +5809,7 @@ substitute (char *in, char *out, struct re_registers *regs) | |||
| 5817 | { | 5809 | { |
| 5818 | dig = *out - '0'; | 5810 | dig = *out - '0'; |
| 5819 | diglen = regs->end[dig] - regs->start[dig]; | 5811 | diglen = regs->end[dig] - regs->start[dig]; |
| 5820 | strncpy (t, in + regs->start[dig], diglen); | 5812 | memcpy (t, in + regs->start[dig], diglen); |
| 5821 | t += diglen; | 5813 | t += diglen; |
| 5822 | } | 5814 | } |
| 5823 | else | 5815 | else |
| @@ -6040,7 +6032,7 @@ readline_internal (linebuffer *lbp, register FILE *stream) | |||
| 6040 | filebuf.size *= 2; | 6032 | filebuf.size *= 2; |
| 6041 | xrnew (filebuf.buffer, filebuf.size, char); | 6033 | xrnew (filebuf.buffer, filebuf.size, char); |
| 6042 | } | 6034 | } |
| 6043 | strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len); | 6035 | memcpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len); |
| 6044 | filebuf.len += lbp->len; | 6036 | filebuf.len += lbp->len; |
| 6045 | filebuf.buffer[filebuf.len++] = '\n'; | 6037 | filebuf.buffer[filebuf.len++] = '\n'; |
| 6046 | filebuf.buffer[filebuf.len] = '\0'; | 6038 | filebuf.buffer[filebuf.len] = '\0'; |
| @@ -6263,7 +6255,7 @@ savenstr (const char *cp, int len) | |||
| 6263 | register char *dp; | 6255 | register char *dp; |
| 6264 | 6256 | ||
| 6265 | dp = xnew (len + 1, char); | 6257 | dp = xnew (len + 1, char); |
| 6266 | strncpy (dp, cp, len); | 6258 | memcpy (dp, cp, len); |
| 6267 | dp[len] = '\0'; | 6259 | dp[len] = '\0'; |
| 6268 | return dp; | 6260 | return dp; |
| 6269 | } | 6261 | } |
diff --git a/lib-src/make-docfile.c b/lib-src/make-docfile.c index 99c43da97f5..bd87b5b6524 100644 --- a/lib-src/make-docfile.c +++ b/lib-src/make-docfile.c | |||
| @@ -541,7 +541,7 @@ write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) | |||
| 541 | 541 | ||
| 542 | /* In C code, `default' is a reserved word, so we spell it | 542 | /* In C code, `default' is a reserved word, so we spell it |
| 543 | `defalt'; demangle that here. */ | 543 | `defalt'; demangle that here. */ |
| 544 | if (ident_length == 6 && strncmp (ident_start, "defalt", 6) == 0) | 544 | if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0) |
| 545 | fprintf (out, "DEFAULT"); | 545 | fprintf (out, "DEFAULT"); |
| 546 | else | 546 | else |
| 547 | while (ident_length-- > 0) | 547 | while (ident_length-- > 0) |
diff --git a/lib-src/movemail.c b/lib-src/movemail.c index 3d994ec5a5e..b9a1be8a7f1 100644 --- a/lib-src/movemail.c +++ b/lib-src/movemail.c | |||
| @@ -141,8 +141,9 @@ static _Noreturn void fatal (const char *s1, const char *s2, const char *s3); | |||
| 141 | static void error (const char *s1, const char *s2, const char *s3); | 141 | static void error (const char *s1, const char *s2, const char *s3); |
| 142 | static _Noreturn void pfatal_with_name (char *name); | 142 | static _Noreturn void pfatal_with_name (char *name); |
| 143 | static _Noreturn void pfatal_and_delete (char *name); | 143 | static _Noreturn void pfatal_and_delete (char *name); |
| 144 | static char *concat (const char *s1, const char *s2, const char *s3); | 144 | #ifdef MAIL_USE_MAILLOCK |
| 145 | static long *xmalloc (unsigned int size); | 145 | static void *xmalloc (size_t size); |
| 146 | #endif | ||
| 146 | #ifdef MAIL_USE_POP | 147 | #ifdef MAIL_USE_POP |
| 147 | static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order); | 148 | static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order); |
| 148 | static int pop_retr (popserver server, int msgno, FILE *arg); | 149 | static int pop_retr (popserver server, int msgno, FILE *arg); |
| @@ -301,7 +302,7 @@ main (int argc, char **argv) | |||
| 301 | inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]); | 302 | inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]); |
| 302 | inname_dirlen--) | 303 | inname_dirlen--) |
| 303 | continue; | 304 | continue; |
| 304 | tempname = (char *) xmalloc (inname_dirlen + sizeof "EXXXXXX"); | 305 | tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX"); |
| 305 | 306 | ||
| 306 | while (1) | 307 | while (1) |
| 307 | { | 308 | { |
| @@ -583,8 +584,8 @@ mail_spool_name (char *inname) | |||
| 583 | if (stat (MAILDIR, &stat1) < 0) | 584 | if (stat (MAILDIR, &stat1) < 0) |
| 584 | return NULL; | 585 | return NULL; |
| 585 | 586 | ||
| 586 | indir = (char *) xmalloc (fname - inname + 1); | 587 | indir = xmalloc (fname - inname + 1); |
| 587 | strncpy (indir, inname, fname - inname); | 588 | memcpy (indir, inname, fname - inname); |
| 588 | indir[fname-inname] = '\0'; | 589 | indir[fname-inname] = '\0'; |
| 589 | 590 | ||
| 590 | 591 | ||
| @@ -644,32 +645,18 @@ pfatal_and_delete (char *name) | |||
| 644 | fatal ("%s for %s", s, name); | 645 | fatal ("%s for %s", s, name); |
| 645 | } | 646 | } |
| 646 | 647 | ||
| 647 | /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ | 648 | #ifdef MAIL_USE_MAILLOCK |
| 648 | |||
| 649 | static char * | ||
| 650 | concat (const char *s1, const char *s2, const char *s3) | ||
| 651 | { | ||
| 652 | size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | ||
| 653 | char *result = (char *) xmalloc (len1 + len2 + len3 + 1); | ||
| 654 | |||
| 655 | strcpy (result, s1); | ||
| 656 | strcpy (result + len1, s2); | ||
| 657 | strcpy (result + len1 + len2, s3); | ||
| 658 | *(result + len1 + len2 + len3) = 0; | ||
| 659 | |||
| 660 | return result; | ||
| 661 | } | ||
| 662 | |||
| 663 | /* Like malloc but get fatal error if memory is exhausted. */ | 649 | /* Like malloc but get fatal error if memory is exhausted. */ |
| 664 | 650 | ||
| 665 | static long * | 651 | static void * |
| 666 | xmalloc (unsigned int size) | 652 | xmalloc (size_t size) |
| 667 | { | 653 | { |
| 668 | long *result = (long *) malloc (size); | 654 | void *result = malloc (size); |
| 669 | if (!result) | 655 | if (!result) |
| 670 | fatal ("virtual memory exhausted", 0, 0); | 656 | fatal ("virtual memory exhausted", 0, 0); |
| 671 | return result; | 657 | return result; |
| 672 | } | 658 | } |
| 659 | #endif | ||
| 673 | 660 | ||
| 674 | /* This is the guts of the interface to the Post Office Protocol. */ | 661 | /* This is the guts of the interface to the Post Office Protocol. */ |
| 675 | 662 | ||
| @@ -851,10 +838,7 @@ pop_retr (popserver server, int msgno, FILE *arg) | |||
| 851 | 838 | ||
| 852 | if (pop_retrieve_first (server, msgno, &line)) | 839 | if (pop_retrieve_first (server, msgno, &line)) |
| 853 | { | 840 | { |
| 854 | char *msg = concat ("Error from POP server: ", pop_error, ""); | 841 | snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); |
| 855 | strncpy (Errmsg, msg, sizeof (Errmsg)); | ||
| 856 | Errmsg[sizeof (Errmsg)-1] = '\0'; | ||
| 857 | free (msg); | ||
| 858 | return (NOTOK); | 842 | return (NOTOK); |
| 859 | } | 843 | } |
| 860 | 844 | ||
| @@ -873,10 +857,7 @@ pop_retr (popserver server, int msgno, FILE *arg) | |||
| 873 | 857 | ||
| 874 | if (ret) | 858 | if (ret) |
| 875 | { | 859 | { |
| 876 | char *msg = concat ("Error from POP server: ", pop_error, ""); | 860 | snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); |
| 877 | strncpy (Errmsg, msg, sizeof (Errmsg)); | ||
| 878 | Errmsg[sizeof (Errmsg)-1] = '\0'; | ||
| 879 | free (msg); | ||
| 880 | return (NOTOK); | 861 | return (NOTOK); |
| 881 | } | 862 | } |
| 882 | 863 | ||
diff --git a/lib-src/pop.c b/lib-src/pop.c index b8ed65c3eac..74054e0e1b1 100644 --- a/lib-src/pop.c +++ b/lib-src/pop.c | |||
| @@ -340,10 +340,7 @@ pop_stat (popserver server, int *count, int *size) | |||
| 340 | if (strncmp (fromserver, "+OK ", 4)) | 340 | if (strncmp (fromserver, "+OK ", 4)) |
| 341 | { | 341 | { |
| 342 | if (0 == strncmp (fromserver, "-ERR", 4)) | 342 | if (0 == strncmp (fromserver, "-ERR", 4)) |
| 343 | { | 343 | snprintf (pop_error, ERROR_MAX, "%s", fromserver); |
| 344 | strncpy (pop_error, fromserver, ERROR_MAX); | ||
| 345 | pop_error[ERROR_MAX-1] = '\0'; | ||
| 346 | } | ||
| 347 | else | 344 | else |
| 348 | { | 345 | { |
| 349 | strcpy (pop_error, | 346 | strcpy (pop_error, |
| @@ -444,10 +441,7 @@ pop_list (popserver server, int message, int **IDs, int **sizes) | |||
| 444 | if (strncmp (fromserver, "+OK ", 4)) | 441 | if (strncmp (fromserver, "+OK ", 4)) |
| 445 | { | 442 | { |
| 446 | if (! strncmp (fromserver, "-ERR", 4)) | 443 | if (! strncmp (fromserver, "-ERR", 4)) |
| 447 | { | 444 | snprintf (pop_error, ERROR_MAX, "%s", fromserver); |
| 448 | strncpy (pop_error, fromserver, ERROR_MAX); | ||
| 449 | pop_error[ERROR_MAX-1] = '\0'; | ||
| 450 | } | ||
| 451 | else | 445 | else |
| 452 | { | 446 | { |
| 453 | strcpy (pop_error, | 447 | strcpy (pop_error, |
| @@ -686,8 +680,7 @@ pop_multi_first (popserver server, const char *command, char **response) | |||
| 686 | 680 | ||
| 687 | if (0 == strncmp (*response, "-ERR", 4)) | 681 | if (0 == strncmp (*response, "-ERR", 4)) |
| 688 | { | 682 | { |
| 689 | strncpy (pop_error, *response, ERROR_MAX); | 683 | snprintf (pop_error, ERROR_MAX, "%s", *response); |
| 690 | pop_error[ERROR_MAX-1] = '\0'; | ||
| 691 | return (-1); | 684 | return (-1); |
| 692 | } | 685 | } |
| 693 | else if (0 == strncmp (*response, "+OK", 3)) | 686 | else if (0 == strncmp (*response, "+OK", 3)) |
| @@ -860,8 +853,7 @@ pop_last (popserver server) | |||
| 860 | 853 | ||
| 861 | if (! strncmp (fromserver, "-ERR", 4)) | 854 | if (! strncmp (fromserver, "-ERR", 4)) |
| 862 | { | 855 | { |
| 863 | strncpy (pop_error, fromserver, ERROR_MAX); | 856 | snprintf (pop_error, ERROR_MAX, "%s", fromserver); |
| 864 | pop_error[ERROR_MAX-1] = '\0'; | ||
| 865 | return (-1); | 857 | return (-1); |
| 866 | } | 858 | } |
| 867 | else if (strncmp (fromserver, "+OK ", 4)) | 859 | else if (strncmp (fromserver, "+OK ", 4)) |
| @@ -1061,9 +1053,8 @@ socket_connection (char *host, int flags) | |||
| 1061 | sock = socket (PF_INET, SOCK_STREAM, 0); | 1053 | sock = socket (PF_INET, SOCK_STREAM, 0); |
| 1062 | if (sock < 0) | 1054 | if (sock < 0) |
| 1063 | { | 1055 | { |
| 1064 | strcpy (pop_error, POP_SOCKET_ERROR); | 1056 | snprintf (pop_error, ERROR_MAX, "%s%s", |
| 1065 | strncat (pop_error, strerror (errno), | 1057 | POP_SOCKET_ERROR, strerror (errno)); |
| 1066 | ERROR_MAX - sizeof (POP_SOCKET_ERROR)); | ||
| 1067 | return (-1); | 1058 | return (-1); |
| 1068 | 1059 | ||
| 1069 | } | 1060 | } |
| @@ -1139,9 +1130,7 @@ socket_connection (char *host, int flags) | |||
| 1139 | if (! connect_ok) | 1130 | if (! connect_ok) |
| 1140 | { | 1131 | { |
| 1141 | CLOSESOCKET (sock); | 1132 | CLOSESOCKET (sock); |
| 1142 | strcpy (pop_error, CONNECT_ERROR); | 1133 | snprintf (pop_error, ERROR_MAX, "%s%s", CONNECT_ERROR, strerror (errno)); |
| 1143 | strncat (pop_error, strerror (errno), | ||
| 1144 | ERROR_MAX - sizeof (CONNECT_ERROR)); | ||
| 1145 | return (-1); | 1134 | return (-1); |
| 1146 | 1135 | ||
| 1147 | } | 1136 | } |
| @@ -1159,9 +1148,8 @@ socket_connection (char *host, int flags) | |||
| 1159 | krb5_auth_con_free (kcontext, auth_context); | 1148 | krb5_auth_con_free (kcontext, auth_context); |
| 1160 | if (kcontext) | 1149 | if (kcontext) |
| 1161 | krb5_free_context (kcontext); | 1150 | krb5_free_context (kcontext); |
| 1162 | strcpy (pop_error, KRB_ERROR); | 1151 | snprintf (pop_error, ERROR_MAX, "%s%s", |
| 1163 | strncat (pop_error, error_message (rem), | 1152 | KRB_ERROR, error_message (rem)); |
| 1164 | ERROR_MAX - sizeof (KRB_ERROR)); | ||
| 1165 | CLOSESOCKET (sock); | 1153 | CLOSESOCKET (sock); |
| 1166 | return (-1); | 1154 | return (-1); |
| 1167 | } | 1155 | } |
| @@ -1199,30 +1187,19 @@ socket_connection (char *host, int flags) | |||
| 1199 | krb5_free_principal (kcontext, server); | 1187 | krb5_free_principal (kcontext, server); |
| 1200 | if (rem) | 1188 | if (rem) |
| 1201 | { | 1189 | { |
| 1202 | strcpy (pop_error, KRB_ERROR); | 1190 | int pop_error_len = snprintf (pop_error, ERROR_MAX, "%s%s", |
| 1203 | strncat (pop_error, error_message (rem), | 1191 | KRB_ERROR, error_message (rem)); |
| 1204 | ERROR_MAX - sizeof (KRB_ERROR)); | ||
| 1205 | #if defined HAVE_KRB5_ERROR_TEXT | 1192 | #if defined HAVE_KRB5_ERROR_TEXT |
| 1206 | if (err_ret && err_ret->text.length) | 1193 | if (err_ret && err_ret->text.length) |
| 1207 | { | 1194 | { |
| 1208 | strncat (pop_error, " [server says '", | 1195 | int errlen = err_ret->text.length; |
| 1209 | ERROR_MAX - strlen (pop_error) - 1); | 1196 | snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len, |
| 1210 | strncat (pop_error, err_ret->text.data, | 1197 | " [server says '.*%s']", errlen, err_ret->text.data); |
| 1211 | min (ERROR_MAX - strlen (pop_error) - 1, | ||
| 1212 | err_ret->text.length)); | ||
| 1213 | strncat (pop_error, "']", | ||
| 1214 | ERROR_MAX - strlen (pop_error) - 1); | ||
| 1215 | } | 1198 | } |
| 1216 | #elif defined HAVE_KRB5_ERROR_E_TEXT | 1199 | #elif defined HAVE_KRB5_ERROR_E_TEXT |
| 1217 | if (err_ret && err_ret->e_text && strlen (*err_ret->e_text)) | 1200 | if (err_ret && err_ret->e_text && **err_ret->e_text) |
| 1218 | { | 1201 | snprintf (pop_error + pop_error_len, ERRMAX - pop_error_len, |
| 1219 | strncat (pop_error, " [server says '", | 1202 | " [server says '%s']", *err_ret->e_text); |
| 1220 | ERROR_MAX - strlen (pop_error) - 1); | ||
| 1221 | strncat (pop_error, *err_ret->e_text, | ||
| 1222 | ERROR_MAX - strlen (pop_error) - 1); | ||
| 1223 | strncat (pop_error, "']", | ||
| 1224 | ERROR_MAX - strlen (pop_error) - 1); | ||
| 1225 | } | ||
| 1226 | #endif | 1203 | #endif |
| 1227 | if (err_ret) | 1204 | if (err_ret) |
| 1228 | krb5_free_error (kcontext, err_ret); | 1205 | krb5_free_error (kcontext, err_ret); |
| @@ -1243,9 +1220,7 @@ socket_connection (char *host, int flags) | |||
| 1243 | free ((char *) ticket); | 1220 | free ((char *) ticket); |
| 1244 | if (rem != KSUCCESS) | 1221 | if (rem != KSUCCESS) |
| 1245 | { | 1222 | { |
| 1246 | strcpy (pop_error, KRB_ERROR); | 1223 | snprintf (pop_error, ERROR_MAX, "%s%s", KRB_ERROR, krb_err_txt[rem]); |
| 1247 | strncat (pop_error, krb_err_txt[rem], | ||
| 1248 | ERROR_MAX - sizeof (KRB_ERROR)); | ||
| 1249 | CLOSESOCKET (sock); | 1224 | CLOSESOCKET (sock); |
| 1250 | return (-1); | 1225 | return (-1); |
| 1251 | } | 1226 | } |
| @@ -1350,9 +1325,8 @@ pop_getline (popserver server, char **line) | |||
| 1350 | server->buffer_size - server->data - 1, 0); | 1325 | server->buffer_size - server->data - 1, 0); |
| 1351 | if (ret < 0) | 1326 | if (ret < 0) |
| 1352 | { | 1327 | { |
| 1353 | strcpy (pop_error, GETLINE_ERROR); | 1328 | snprintf (pop_error, ERROR_MAX, "%s%s", |
| 1354 | strncat (pop_error, strerror (errno), | 1329 | GETLINE_ERROR, strerror (errno)); |
| 1355 | ERROR_MAX - sizeof (GETLINE_ERROR)); | ||
| 1356 | pop_trash (server); | 1330 | pop_trash (server); |
| 1357 | return (-1); | 1331 | return (-1); |
| 1358 | } | 1332 | } |
| @@ -1436,9 +1410,7 @@ sendline (popserver server, const char *line) | |||
| 1436 | if (ret < 0) | 1410 | if (ret < 0) |
| 1437 | { | 1411 | { |
| 1438 | pop_trash (server); | 1412 | pop_trash (server); |
| 1439 | strcpy (pop_error, SENDLINE_ERROR); | 1413 | snprintf (pop_error, ERROR_MAX, "%s%s", SENDLINE_ERROR, strerror (errno)); |
| 1440 | strncat (pop_error, strerror (errno), | ||
| 1441 | ERROR_MAX - sizeof (SENDLINE_ERROR)); | ||
| 1442 | return (ret); | 1414 | return (ret); |
| 1443 | } | 1415 | } |
| 1444 | 1416 | ||
| @@ -1500,8 +1472,7 @@ getok (popserver server) | |||
| 1500 | return (0); | 1472 | return (0); |
| 1501 | else if (! strncmp (fromline, "-ERR", 4)) | 1473 | else if (! strncmp (fromline, "-ERR", 4)) |
| 1502 | { | 1474 | { |
| 1503 | strncpy (pop_error, fromline, ERROR_MAX); | 1475 | snprintf (pop_error, ERROR_MAX, "%s", fromline); |
| 1504 | pop_error[ERROR_MAX-1] = '\0'; | ||
| 1505 | return (-1); | 1476 | return (-1); |
| 1506 | } | 1477 | } |
| 1507 | else | 1478 | else |
diff --git a/src/ChangeLog b/src/ChangeLog index 84703fcdc2f..af0fc3a8e38 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -1,3 +1,37 @@ | |||
| 1 | 2012-07-10 Paul Eggert <eggert@cs.ucla.edu> | ||
| 2 | |||
| 3 | Simplify by avoiding confusing use of strncpy etc. | ||
| 4 | * doc.c (Fsnarf_documentation): | ||
| 5 | * fileio.c (Ffile_name_directory, Fsubstitute_in_file_name): | ||
| 6 | * frame.c (Fmake_terminal_frame): | ||
| 7 | * gtkutil.c (get_utf8_string): | ||
| 8 | * lread.c (openp): | ||
| 9 | * nsmenu.m (ns_update_menubar): | ||
| 10 | * regex.c (regerror): | ||
| 11 | Prefer memcpy to strncpy and strncat when either will do. | ||
| 12 | * fileio.c (Fsubstitute_in_file_name): | ||
| 13 | * keyboard.c (MULTI_LETTER_MOD, parse_modifiers_uncached) | ||
| 14 | (menu_separator_name_p): | ||
| 15 | * nsmenu.m (ns_update_menubar): | ||
| 16 | Prefer memcmp to strncmp when either will do. | ||
| 17 | * nsterm.m: Include <ftoastr.h>. | ||
| 18 | (ns_get_color): | ||
| 19 | * s/gnu-linux.h, s/sol2-6.h, s/unixware.h (PTY_TTY_NAME_SPRINTF): | ||
| 20 | Prefer snprintf to strncpy. | ||
| 21 | * nsterm.m (ns_term_init): | ||
| 22 | * widget.c (set_frame_size) [0]: Prefer xstrdup to xmalloc + strncpy. | ||
| 23 | * nsterm.m (ns_term_init): | ||
| 24 | Avoid the need for strncpy, by using build_string or | ||
| 25 | make_unibyte_string directly. Use dtoastr, not snprintf. | ||
| 26 | * process.c (Fmake_network_process): Diagnose service names that | ||
| 27 | are too long, rather than silently truncating them or creating | ||
| 28 | non-null-terminated names. | ||
| 29 | (Fnetwork_interface_info): Likewise, for interface names. | ||
| 30 | * sysdep.c (system_process_attributes) [GNU_LINUX]: | ||
| 31 | Prefer sprintf to strncat. | ||
| 32 | * xdisp.c (debug_method_add) [GLYPH_DEBUG]: | ||
| 33 | Prefer vsnprintf to vsprintf + strncpy. | ||
| 34 | |||
| 1 | 2012-07-10 Glenn Morris <rgm@gnu.org> | 35 | 2012-07-10 Glenn Morris <rgm@gnu.org> |
| 2 | 36 | ||
| 3 | * dispnew.c (PENDING_OUTPUT_COUNT) [!__GNU_LIBRARY__]: | 37 | * dispnew.c (PENDING_OUTPUT_COUNT) [!__GNU_LIBRARY__]: |
| @@ -645,7 +645,7 @@ the same file name is found in the `doc-directory'. */) | |||
| 645 | { | 645 | { |
| 646 | ptrdiff_t len = end - p - 2; | 646 | ptrdiff_t len = end - p - 2; |
| 647 | char *fromfile = alloca (len + 1); | 647 | char *fromfile = alloca (len + 1); |
| 648 | strncpy (fromfile, &p[2], len); | 648 | memcpy (fromfile, &p[2], len); |
| 649 | fromfile[len] = 0; | 649 | fromfile[len] = 0; |
| 650 | if (fromfile[len-1] == 'c') | 650 | if (fromfile[len-1] == 'c') |
| 651 | fromfile[len-1] = 'o'; | 651 | fromfile[len-1] = 'o'; |
diff --git a/src/fileio.c b/src/fileio.c index 5d4ee1bde08..eccb1fbb559 100644 --- a/src/fileio.c +++ b/src/fileio.c | |||
| @@ -365,7 +365,7 @@ Given a Unix syntax file name, returns a string ending in slash. */) | |||
| 365 | 365 | ||
| 366 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') | 366 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') |
| 367 | { | 367 | { |
| 368 | strncpy (res, beg, 2); | 368 | memcpy (res, beg, 2); |
| 369 | beg += 2; | 369 | beg += 2; |
| 370 | r += 2; | 370 | r += 2; |
| 371 | } | 371 | } |
| @@ -1648,7 +1648,7 @@ those `/' is discarded. */) | |||
| 1648 | 1648 | ||
| 1649 | /* Copy out the variable name. */ | 1649 | /* Copy out the variable name. */ |
| 1650 | target = alloca (s - o + 1); | 1650 | target = alloca (s - o + 1); |
| 1651 | strncpy (target, o, s - o); | 1651 | memcpy (target, o, s - o); |
| 1652 | target[s - o] = 0; | 1652 | target[s - o] = 0; |
| 1653 | #ifdef DOS_NT | 1653 | #ifdef DOS_NT |
| 1654 | strupr (target); /* $home == $HOME etc. */ | 1654 | strupr (target); /* $home == $HOME etc. */ |
| @@ -1711,7 +1711,7 @@ those `/' is discarded. */) | |||
| 1711 | 1711 | ||
| 1712 | /* Copy out the variable name. */ | 1712 | /* Copy out the variable name. */ |
| 1713 | target = alloca (s - o + 1); | 1713 | target = alloca (s - o + 1); |
| 1714 | strncpy (target, o, s - o); | 1714 | memcpy (target, o, s - o); |
| 1715 | target[s - o] = 0; | 1715 | target[s - o] = 0; |
| 1716 | #ifdef DOS_NT | 1716 | #ifdef DOS_NT |
| 1717 | strupr (target); /* $home == $HOME etc. */ | 1717 | strupr (target); /* $home == $HOME etc. */ |
| @@ -1732,13 +1732,13 @@ those `/' is discarded. */) | |||
| 1732 | orig = make_unibyte_string (o, orig_length); | 1732 | orig = make_unibyte_string (o, orig_length); |
| 1733 | decoded = DECODE_FILE (orig); | 1733 | decoded = DECODE_FILE (orig); |
| 1734 | decoded_length = SBYTES (decoded); | 1734 | decoded_length = SBYTES (decoded); |
| 1735 | strncpy (x, SSDATA (decoded), decoded_length); | 1735 | memcpy (x, SDATA (decoded), decoded_length); |
| 1736 | x += decoded_length; | 1736 | x += decoded_length; |
| 1737 | 1737 | ||
| 1738 | /* If environment variable needed decoding, return value | 1738 | /* If environment variable needed decoding, return value |
| 1739 | needs to be multibyte. */ | 1739 | needs to be multibyte. */ |
| 1740 | if (decoded_length != orig_length | 1740 | if (decoded_length != orig_length |
| 1741 | || strncmp (SSDATA (decoded), o, orig_length)) | 1741 | || memcmp (SDATA (decoded), o, orig_length)) |
| 1742 | multibyte = 1; | 1742 | multibyte = 1; |
| 1743 | } | 1743 | } |
| 1744 | } | 1744 | } |
diff --git a/src/frame.c b/src/frame.c index 3a3f5526035..8d7981777bf 100644 --- a/src/frame.c +++ b/src/frame.c | |||
| @@ -646,7 +646,7 @@ affects all frames on the same terminal device. */) | |||
| 646 | if (!NILP (tty)) | 646 | if (!NILP (tty)) |
| 647 | { | 647 | { |
| 648 | name = alloca (SBYTES (tty) + 1); | 648 | name = alloca (SBYTES (tty) + 1); |
| 649 | strncpy (name, SSDATA (tty), SBYTES (tty)); | 649 | memcpy (name, SSDATA (tty), SBYTES (tty)); |
| 650 | name[SBYTES (tty)] = 0; | 650 | name[SBYTES (tty)] = 0; |
| 651 | } | 651 | } |
| 652 | 652 | ||
| @@ -657,7 +657,7 @@ affects all frames on the same terminal device. */) | |||
| 657 | if (!NILP (tty_type)) | 657 | if (!NILP (tty_type)) |
| 658 | { | 658 | { |
| 659 | type = alloca (SBYTES (tty_type) + 1); | 659 | type = alloca (SBYTES (tty_type) + 1); |
| 660 | strncpy (type, SSDATA (tty_type), SBYTES (tty_type)); | 660 | memcpy (type, SSDATA (tty_type), SBYTES (tty_type)); |
| 661 | type[SBYTES (tty_type)] = 0; | 661 | type[SBYTES (tty_type)] = 0; |
| 662 | } | 662 | } |
| 663 | 663 | ||
diff --git a/src/gtkutil.c b/src/gtkutil.c index f4551b4b9f7..290ecef9216 100644 --- a/src/gtkutil.c +++ b/src/gtkutil.c | |||
| @@ -529,7 +529,7 @@ get_utf8_string (const char *str) | |||
| 529 | &bytes_written, &err)) | 529 | &bytes_written, &err)) |
| 530 | && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE) | 530 | && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE) |
| 531 | { | 531 | { |
| 532 | strncpy (up, (char *)p, bytes_written); | 532 | memcpy (up, p, bytes_written); |
| 533 | sprintf (up + bytes_written, "\\%03o", p[bytes_written]); | 533 | sprintf (up + bytes_written, "\\%03o", p[bytes_written]); |
| 534 | up += bytes_written+4; | 534 | up += bytes_written+4; |
| 535 | p += bytes_written+1; | 535 | p += bytes_written+1; |
diff --git a/src/keyboard.c b/src/keyboard.c index 71c8d869ece..b34d3c470a5 100644 --- a/src/keyboard.c +++ b/src/keyboard.c | |||
| @@ -6134,7 +6134,7 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end) | |||
| 6134 | 6134 | ||
| 6135 | #define MULTI_LETTER_MOD(BIT, NAME, LEN) \ | 6135 | #define MULTI_LETTER_MOD(BIT, NAME, LEN) \ |
| 6136 | if (i + LEN + 1 <= SBYTES (name) \ | 6136 | if (i + LEN + 1 <= SBYTES (name) \ |
| 6137 | && ! strncmp (SSDATA (name) + i, NAME, LEN)) \ | 6137 | && ! memcmp (SDATA (name) + i, NAME, LEN)) \ |
| 6138 | { \ | 6138 | { \ |
| 6139 | this_mod_end = i + LEN; \ | 6139 | this_mod_end = i + LEN; \ |
| 6140 | this_mod = BIT; \ | 6140 | this_mod = BIT; \ |
| @@ -6172,13 +6172,13 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end) | |||
| 6172 | if (! (modifiers & (down_modifier | drag_modifier | 6172 | if (! (modifiers & (down_modifier | drag_modifier |
| 6173 | | double_modifier | triple_modifier)) | 6173 | | double_modifier | triple_modifier)) |
| 6174 | && i + 7 == SBYTES (name) | 6174 | && i + 7 == SBYTES (name) |
| 6175 | && strncmp (SSDATA (name) + i, "mouse-", 6) == 0 | 6175 | && memcmp (SDATA (name) + i, "mouse-", 6) == 0 |
| 6176 | && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9')) | 6176 | && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9')) |
| 6177 | modifiers |= click_modifier; | 6177 | modifiers |= click_modifier; |
| 6178 | 6178 | ||
| 6179 | if (! (modifiers & (double_modifier | triple_modifier)) | 6179 | if (! (modifiers & (double_modifier | triple_modifier)) |
| 6180 | && i + 6 < SBYTES (name) | 6180 | && i + 6 < SBYTES (name) |
| 6181 | && strncmp (SSDATA (name) + i, "wheel-", 6) == 0) | 6181 | && memcmp (SDATA (name) + i, "wheel-", 6) == 0) |
| 6182 | modifiers |= click_modifier; | 6182 | modifiers |= click_modifier; |
| 6183 | 6183 | ||
| 6184 | if (modifier_end) | 6184 | if (modifier_end) |
| @@ -6630,7 +6630,7 @@ parse_solitary_modifier (Lisp_Object symbol) | |||
| 6630 | 6630 | ||
| 6631 | #define MULTI_LETTER_MOD(BIT, NAME, LEN) \ | 6631 | #define MULTI_LETTER_MOD(BIT, NAME, LEN) \ |
| 6632 | if (LEN == SBYTES (name) \ | 6632 | if (LEN == SBYTES (name) \ |
| 6633 | && ! strncmp (SSDATA (name), NAME, LEN)) \ | 6633 | && ! memcmp (SDATA (name), NAME, LEN)) \ |
| 6634 | return BIT; | 6634 | return BIT; |
| 6635 | 6635 | ||
| 6636 | case 'A': | 6636 | case 'A': |
| @@ -7418,7 +7418,7 @@ menu_separator_name_p (const char *label) | |||
| 7418 | if (!label) | 7418 | if (!label) |
| 7419 | return 0; | 7419 | return 0; |
| 7420 | else if (strlen (label) > 3 | 7420 | else if (strlen (label) > 3 |
| 7421 | && strncmp (label, "--", 2) == 0 | 7421 | && memcmp (label, "--", 2) == 0 |
| 7422 | && label[2] != '-') | 7422 | && label[2] != '-') |
| 7423 | { | 7423 | { |
| 7424 | int i; | 7424 | int i; |
diff --git a/src/lread.c b/src/lread.c index bd85e44093e..640414b3e91 100644 --- a/src/lread.c +++ b/src/lread.c | |||
| @@ -1495,26 +1495,14 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *sto | |||
| 1495 | 1495 | ||
| 1496 | /* Concatenate path element/specified name with the suffix. | 1496 | /* Concatenate path element/specified name with the suffix. |
| 1497 | If the directory starts with /:, remove that. */ | 1497 | If the directory starts with /:, remove that. */ |
| 1498 | if (SCHARS (filename) > 2 | 1498 | int prefixlen = ((SCHARS (filename) > 2 |
| 1499 | && SREF (filename, 0) == '/' | 1499 | && SREF (filename, 0) == '/' |
| 1500 | && SREF (filename, 1) == ':') | 1500 | && SREF (filename, 1) == ':') |
| 1501 | { | 1501 | ? 2 : 0); |
| 1502 | fnlen = SBYTES (filename) - 2; | 1502 | fnlen = SBYTES (filename) - prefixlen; |
| 1503 | strncpy (fn, SSDATA (filename) + 2, fnlen); | 1503 | memcpy (fn, SDATA (filename) + prefixlen, fnlen); |
| 1504 | fn[fnlen] = '\0'; | 1504 | memcpy (fn + fnlen, SDATA (XCAR (tail)), lsuffix + 1); |
| 1505 | } | 1505 | fnlen += lsuffix; |
| 1506 | else | ||
| 1507 | { | ||
| 1508 | fnlen = SBYTES (filename); | ||
| 1509 | strncpy (fn, SSDATA (filename), fnlen); | ||
| 1510 | fn[fnlen] = '\0'; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | if (lsuffix != 0) /* Bug happens on CCI if lsuffix is 0. */ | ||
| 1514 | { | ||
| 1515 | strncat (fn, SSDATA (XCAR (tail)), lsuffix); | ||
| 1516 | fnlen += lsuffix; | ||
| 1517 | } | ||
| 1518 | /* Check that the file exists and is not a directory. */ | 1506 | /* Check that the file exists and is not a directory. */ |
| 1519 | /* We used to only check for handlers on non-absolute file names: | 1507 | /* We used to only check for handlers on non-absolute file names: |
| 1520 | if (absolute) | 1508 | if (absolute) |
diff --git a/src/nsmenu.m b/src/nsmenu.m index cccecffb427..2cd626e131a 100644 --- a/src/nsmenu.m +++ b/src/nsmenu.m | |||
| @@ -426,7 +426,8 @@ ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu) | |||
| 426 | break; | 426 | break; |
| 427 | else | 427 | else |
| 428 | continue; | 428 | continue; |
| 429 | if (strncmp (previous_strings[i], SDATA (string), 10)) | 429 | if (memcmp (previous_strings[i], SDATA (string), |
| 430 | min (10, SBYTES (string) + 1))) | ||
| 430 | break; | 431 | break; |
| 431 | } | 432 | } |
| 432 | 433 | ||
| @@ -447,7 +448,8 @@ ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu) | |||
| 447 | break; | 448 | break; |
| 448 | 449 | ||
| 449 | if (n < 100) | 450 | if (n < 100) |
| 450 | strncpy (previous_strings[i/4], SDATA (string), 10); | 451 | memcpy (previous_strings[i/4], min (10, SBYTES (string) + 1), |
| 452 | SDATA (string)); | ||
| 451 | 453 | ||
| 452 | wv = xmalloc_widget_value (); | 454 | wv = xmalloc_widget_value (); |
| 453 | wv->name = SSDATA (string); | 455 | wv->name = SSDATA (string); |
diff --git a/src/nsterm.m b/src/nsterm.m index fde02f3ec9b..0745efc35bd 100644 --- a/src/nsterm.m +++ b/src/nsterm.m | |||
| @@ -37,6 +37,7 @@ GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu) | |||
| 37 | #include <unistd.h> | 37 | #include <unistd.h> |
| 38 | #include <setjmp.h> | 38 | #include <setjmp.h> |
| 39 | #include <c-strcase.h> | 39 | #include <c-strcase.h> |
| 40 | #include <ftoastr.h> | ||
| 40 | 41 | ||
| 41 | #include "lisp.h" | 42 | #include "lisp.h" |
| 42 | #include "blockinput.h" | 43 | #include "blockinput.h" |
| @@ -1442,21 +1443,16 @@ ns_get_color (const char *name, NSColor **col) | |||
| 1442 | [scanner scanFloat: &b]; | 1443 | [scanner scanFloat: &b]; |
| 1443 | } | 1444 | } |
| 1444 | else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */ | 1445 | else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */ |
| 1445 | { | 1446 | scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3; |
| 1446 | strncpy (hex, name + 4, 19); | ||
| 1447 | hex[19] = '\0'; | ||
| 1448 | scaling = (strlen(hex) - 2) / 3; | ||
| 1449 | } | ||
| 1450 | else if (name[0] == '#') /* An old X11 format; convert to newer */ | 1447 | else if (name[0] == '#') /* An old X11 format; convert to newer */ |
| 1451 | { | 1448 | { |
| 1452 | int len = (strlen(name) - 1); | 1449 | int len = (strlen(name) - 1); |
| 1453 | int start = (len % 3 == 0) ? 1 : len / 4 + 1; | 1450 | int start = (len % 3 == 0) ? 1 : len / 4 + 1; |
| 1454 | int i; | 1451 | int i; |
| 1455 | scaling = strlen(name+start) / 3; | 1452 | scaling = strlen(name+start) / 3; |
| 1456 | for (i=0; i<3; i++) { | 1453 | for (i = 0; i < 3; i++) |
| 1457 | strncpy(hex + i * (scaling + 1), name + start + i * scaling, scaling); | 1454 | snprintf (hex + i * (scaling + 1), "%.*s/", scaling, |
| 1458 | hex[(i+1) * (scaling + 1) - 1] = '/'; | 1455 | name + start + i * scaling); |
| 1459 | } | ||
| 1460 | hex[3 * (scaling + 1) - 1] = '\0'; | 1456 | hex[3 * (scaling + 1) - 1] = '\0'; |
| 1461 | } | 1457 | } |
| 1462 | 1458 | ||
| @@ -4107,10 +4103,7 @@ ns_term_init (Lisp_Object display_name) | |||
| 4107 | ns_display_name_list); | 4103 | ns_display_name_list); |
| 4108 | dpyinfo->name_list_element = XCAR (ns_display_name_list); | 4104 | dpyinfo->name_list_element = XCAR (ns_display_name_list); |
| 4109 | 4105 | ||
| 4110 | /* Set the name of the terminal. */ | 4106 | terminal->name = xstrdup (SSDATA (display_name)); |
| 4111 | terminal->name = xmalloc (SBYTES (display_name) + 1); | ||
| 4112 | strncpy (terminal->name, SDATA (display_name), SBYTES (display_name)); | ||
| 4113 | terminal->name[SBYTES (display_name)] = 0; | ||
| 4114 | 4107 | ||
| 4115 | UNBLOCK_INPUT; | 4108 | UNBLOCK_INPUT; |
| 4116 | 4109 | ||
| @@ -4167,14 +4160,14 @@ ns_term_init (Lisp_Object display_name) | |||
| 4167 | } | 4160 | } |
| 4168 | 4161 | ||
| 4169 | { | 4162 | { |
| 4170 | char c[128]; | ||
| 4171 | #ifdef NS_IMPL_GNUSTEP | 4163 | #ifdef NS_IMPL_GNUSTEP |
| 4172 | strncpy (c, gnustep_base_version, sizeof (c)); | 4164 | Vwindow_system_version = build_string (gnustep_base_version); |
| 4173 | #else | 4165 | #else |
| 4174 | /*PSnextrelease (128, c); */ | 4166 | /*PSnextrelease (128, c); */ |
| 4175 | snprintf (c, sizeof (c), "%g", NSAppKitVersionNumber); | 4167 | char c[DBL_BUFSIZE_BOUND]; |
| 4168 | int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber); | ||
| 4169 | Vwindow_system_version = make_unibyte_string (c, len); | ||
| 4176 | #endif | 4170 | #endif |
| 4177 | Vwindow_system_version = build_string (c); | ||
| 4178 | } | 4171 | } |
| 4179 | 4172 | ||
| 4180 | delete_keyboard_wait_descriptor (0); | 4173 | delete_keyboard_wait_descriptor (0); |
diff --git a/src/process.c b/src/process.c index b8c3a18b33d..79100eb7a2c 100644 --- a/src/process.c +++ b/src/process.c | |||
| @@ -3013,7 +3013,9 @@ usage: (make-network-process &rest ARGS) */) | |||
| 3013 | CHECK_STRING (service); | 3013 | CHECK_STRING (service); |
| 3014 | memset (&address_un, 0, sizeof address_un); | 3014 | memset (&address_un, 0, sizeof address_un); |
| 3015 | address_un.sun_family = AF_LOCAL; | 3015 | address_un.sun_family = AF_LOCAL; |
| 3016 | strncpy (address_un.sun_path, SSDATA (service), sizeof address_un.sun_path); | 3016 | if (sizeof address_un.sun_path <= SBYTES (service)) |
| 3017 | error ("Service name too long"); | ||
| 3018 | strcpy (address_un.sun_path, SSDATA (service)); | ||
| 3017 | ai.ai_addr = (struct sockaddr *) &address_un; | 3019 | ai.ai_addr = (struct sockaddr *) &address_un; |
| 3018 | ai.ai_addrlen = sizeof address_un; | 3020 | ai.ai_addrlen = sizeof address_un; |
| 3019 | goto open_socket; | 3021 | goto open_socket; |
| @@ -3717,8 +3719,9 @@ FLAGS is the current flags of the interface. */) | |||
| 3717 | 3719 | ||
| 3718 | CHECK_STRING (ifname); | 3720 | CHECK_STRING (ifname); |
| 3719 | 3721 | ||
| 3720 | memset (rq.ifr_name, 0, sizeof rq.ifr_name); | 3722 | if (sizeof rq.ifr_name <= SBYTES (ifname)) |
| 3721 | strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name)); | 3723 | error ("interface name too long"); |
| 3724 | strcpy (rq.ifr_name, SSDATA (ifname)); | ||
| 3722 | 3725 | ||
| 3723 | s = socket (AF_INET, SOCK_STREAM, 0); | 3726 | s = socket (AF_INET, SOCK_STREAM, 0); |
| 3724 | if (s < 0) | 3727 | if (s < 0) |
diff --git a/src/regex.c b/src/regex.c index 751006d57ba..4bf119402ac 100644 --- a/src/regex.c +++ b/src/regex.c | |||
| @@ -6644,7 +6644,7 @@ regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size) | |||
| 6644 | { | 6644 | { |
| 6645 | if (msg_size > errbuf_size) | 6645 | if (msg_size > errbuf_size) |
| 6646 | { | 6646 | { |
| 6647 | strncpy (errbuf, msg, errbuf_size - 1); | 6647 | memcpy (errbuf, msg, errbuf_size - 1); |
| 6648 | errbuf[errbuf_size - 1] = 0; | 6648 | errbuf[errbuf_size - 1] = 0; |
| 6649 | } | 6649 | } |
| 6650 | else | 6650 | else |
diff --git a/src/s/gnu-linux.h b/src/s/gnu-linux.h index e3d43249d81..6f45ee00b78 100644 --- a/src/s/gnu-linux.h +++ b/src/s/gnu-linux.h | |||
| @@ -63,8 +63,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 63 | close (fd); \ | 63 | close (fd); \ |
| 64 | return -1; \ | 64 | return -1; \ |
| 65 | } \ | 65 | } \ |
| 66 | strncpy (pty_name, ptyname, sizeof (pty_name)); \ | 66 | snprintf (pty_name, sizeof pty_name, "%s", ptyname); \ |
| 67 | pty_name[sizeof (pty_name) - 1] = 0; \ | ||
| 68 | sigunblock (sigmask (SIGCHLD)); \ | 67 | sigunblock (sigmask (SIGCHLD)); \ |
| 69 | } | 68 | } |
| 70 | 69 | ||
diff --git a/src/s/sol2-6.h b/src/s/sol2-6.h index bb7a9859b7d..ec45748e03a 100644 --- a/src/s/sol2-6.h +++ b/src/s/sol2-6.h | |||
| @@ -54,8 +54,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 54 | { emacs_close (fd); return -1; } \ | 54 | { emacs_close (fd); return -1; } \ |
| 55 | if (!(ptyname = ptsname (fd))) \ | 55 | if (!(ptyname = ptsname (fd))) \ |
| 56 | { emacs_close (fd); return -1; } \ | 56 | { emacs_close (fd); return -1; } \ |
| 57 | strncpy (pty_name, ptyname, sizeof (pty_name)); \ | 57 | snprintf (pty_name, sizeof pty_name, "%s", ptyname); \ |
| 58 | pty_name[sizeof (pty_name) - 1] = 0; \ | ||
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | #define GC_SETJMP_WORKS 1 | 60 | #define GC_SETJMP_WORKS 1 |
diff --git a/src/s/unixware.h b/src/s/unixware.h index e9ebb63f30a..5bda987ffe1 100644 --- a/src/s/unixware.h +++ b/src/s/unixware.h | |||
| @@ -40,8 +40,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 40 | fatal("could not unlock slave pty"); \ | 40 | fatal("could not unlock slave pty"); \ |
| 41 | if (!(ptyname = ptsname(fd))) \ | 41 | if (!(ptyname = ptsname(fd))) \ |
| 42 | fatal ("could not enable slave pty"); \ | 42 | fatal ("could not enable slave pty"); \ |
| 43 | strncpy(pty_name, ptyname, sizeof(pty_name)); \ | 43 | snprintf (pty_name, sizeof pty_name, "%s", ptyname); \ |
| 44 | pty_name[sizeof(pty_name) - 1] = 0; \ | ||
| 45 | } | 44 | } |
| 46 | 45 | ||
| 47 | /* Conservative garbage collection has not been tested, so for now | 46 | /* Conservative garbage collection has not been tested, so for now |
diff --git a/src/sysdep.c b/src/sysdep.c index 0639b72285a..ed926760414 100644 --- a/src/sysdep.c +++ b/src/sysdep.c | |||
| @@ -2744,9 +2744,11 @@ system_process_attributes (Lisp_Object pid) | |||
| 2744 | char procbuf[1025], *p, *q; | 2744 | char procbuf[1025], *p, *q; |
| 2745 | int fd; | 2745 | int fd; |
| 2746 | ssize_t nread; | 2746 | ssize_t nread; |
| 2747 | const char *cmd = NULL; | 2747 | static char const default_cmd[] = "???"; |
| 2748 | const char *cmd = default_cmd; | ||
| 2749 | int cmdsize = sizeof default_cmd - 1; | ||
| 2748 | char *cmdline = NULL; | 2750 | char *cmdline = NULL; |
| 2749 | ptrdiff_t cmdsize = 0, cmdline_size; | 2751 | ptrdiff_t cmdline_size; |
| 2750 | unsigned char c; | 2752 | unsigned char c; |
| 2751 | printmax_t proc_id; | 2753 | printmax_t proc_id; |
| 2752 | int ppid, pgrp, sess, tty, tpgid, thcount; | 2754 | int ppid, pgrp, sess, tty, tpgid, thcount; |
| @@ -2808,11 +2810,6 @@ system_process_attributes (Lisp_Object pid) | |||
| 2808 | } | 2810 | } |
| 2809 | else | 2811 | else |
| 2810 | q = NULL; | 2812 | q = NULL; |
| 2811 | if (cmd == NULL) | ||
| 2812 | { | ||
| 2813 | cmd = "???"; | ||
| 2814 | cmdsize = 3; | ||
| 2815 | } | ||
| 2816 | /* Command name is encoded in locale-coding-system; decode it. */ | 2813 | /* Command name is encoded in locale-coding-system; decode it. */ |
| 2817 | cmd_str = make_unibyte_string (cmd, cmdsize); | 2814 | cmd_str = make_unibyte_string (cmd, cmdsize); |
| 2818 | decoded_cmd = code_convert_string_norecord (cmd_str, | 2815 | decoded_cmd = code_convert_string_norecord (cmd_str, |
| @@ -2950,14 +2947,9 @@ system_process_attributes (Lisp_Object pid) | |||
| 2950 | } | 2947 | } |
| 2951 | if (!cmdline_size) | 2948 | if (!cmdline_size) |
| 2952 | { | 2949 | { |
| 2953 | if (!cmd) | ||
| 2954 | cmd = "???"; | ||
| 2955 | if (!cmdsize) | ||
| 2956 | cmdsize = strlen (cmd); | ||
| 2957 | cmdline_size = cmdsize + 2; | 2950 | cmdline_size = cmdsize + 2; |
| 2958 | cmdline = xmalloc (cmdline_size + 1); | 2951 | cmdline = xmalloc (cmdline_size + 1); |
| 2959 | strcpy (cmdline, "["); | 2952 | sprintf (cmdline, "[%.*s]", cmdsize, cmd); |
| 2960 | strcat (strncat (cmdline, cmd, cmdsize), "]"); | ||
| 2961 | } | 2953 | } |
| 2962 | emacs_close (fd); | 2954 | emacs_close (fd); |
| 2963 | /* Command line is encoded in locale-coding-system; decode it. */ | 2955 | /* Command line is encoded in locale-coding-system; decode it. */ |
diff --git a/src/widget.c b/src/widget.c index e219c200e00..b94c30f4e9c 100644 --- a/src/widget.c +++ b/src/widget.c | |||
| @@ -429,25 +429,15 @@ set_frame_size (EmacsFrame ew) | |||
| 429 | { | 429 | { |
| 430 | /* the tricky things with the sign is to make sure that | 430 | /* the tricky things with the sign is to make sure that |
| 431 | -0 is printed -0. */ | 431 | -0 is printed -0. */ |
| 432 | int len; | ||
| 433 | char *tem; | ||
| 434 | sprintf (shell_position, "=%c%d%c%d", | 432 | sprintf (shell_position, "=%c%d%c%d", |
| 435 | flags & XNegative ? '-' : '+', x < 0 ? -x : x, | 433 | flags & XNegative ? '-' : '+', x < 0 ? -x : x, |
| 436 | flags & YNegative ? '-' : '+', y < 0 ? -y : y); | 434 | flags & YNegative ? '-' : '+', y < 0 ? -y : y); |
| 437 | len = strlen (shell_position) + 1; | 435 | XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL); |
| 438 | tem = xmalloc (len); | ||
| 439 | strncpy (tem, shell_position, len); | ||
| 440 | XtVaSetValues (wmshell, XtNgeometry, tem, NULL); | ||
| 441 | } | 436 | } |
| 442 | else if (flags & (WidthValue | HeightValue)) | 437 | else if (flags & (WidthValue | HeightValue)) |
| 443 | { | 438 | { |
| 444 | int len; | ||
| 445 | char *tem; | ||
| 446 | sprintf (shell_position, "=%dx%d", pixel_width, pixel_height); | 439 | sprintf (shell_position, "=%dx%d", pixel_width, pixel_height); |
| 447 | len = strlen (shell_position) + 1; | 440 | XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL); |
| 448 | tem = xmalloc (len); | ||
| 449 | strncpy (tem, shell_position, len); | ||
| 450 | XtVaSetValues (wmshell, XtNgeometry, tem, NULL); | ||
| 451 | } | 441 | } |
| 452 | 442 | ||
| 453 | /* If the geometry spec we're using has W/H components, mark the size | 443 | /* If the geometry spec we're using has W/H components, mark the size |
diff --git a/src/xdisp.c b/src/xdisp.c index 1732e3dfe56..4c9f3fda0a8 100644 --- a/src/xdisp.c +++ b/src/xdisp.c | |||
| @@ -12492,23 +12492,21 @@ static void debug_method_add (struct window *, char const *, ...) | |||
| 12492 | static void | 12492 | static void |
| 12493 | debug_method_add (struct window *w, char const *fmt, ...) | 12493 | debug_method_add (struct window *w, char const *fmt, ...) |
| 12494 | { | 12494 | { |
| 12495 | char buffer[512]; | ||
| 12496 | char *method = w->desired_matrix->method; | 12495 | char *method = w->desired_matrix->method; |
| 12497 | int len = strlen (method); | 12496 | int len = strlen (method); |
| 12498 | int size = sizeof w->desired_matrix->method; | 12497 | int size = sizeof w->desired_matrix->method; |
| 12499 | int remaining = size - len - 1; | 12498 | int remaining = size - len - 1; |
| 12500 | va_list ap; | 12499 | va_list ap; |
| 12501 | 12500 | ||
| 12502 | va_start (ap, fmt); | ||
| 12503 | vsprintf (buffer, fmt, ap); | ||
| 12504 | va_end (ap); | ||
| 12505 | if (len && remaining) | 12501 | if (len && remaining) |
| 12506 | { | 12502 | { |
| 12507 | method[len] = '|'; | 12503 | method[len] = '|'; |
| 12508 | --remaining, ++len; | 12504 | --remaining, ++len; |
| 12509 | } | 12505 | } |
| 12510 | 12506 | ||
| 12511 | strncpy (method + len, buffer, remaining); | 12507 | va_start (ap, fmt); |
| 12508 | vsnprintf (method + len, remaining + 1, fmt, ap); | ||
| 12509 | va_end (ap); | ||
| 12512 | 12510 | ||
| 12513 | if (trace_redisplay_p) | 12511 | if (trace_redisplay_p) |
| 12514 | fprintf (stderr, "%p (%s): %s\n", | 12512 | fprintf (stderr, "%p (%s): %s\n", |
| @@ -12517,7 +12515,7 @@ debug_method_add (struct window *w, char const *fmt, ...) | |||
| 12517 | && STRINGP (BVAR (XBUFFER (w->buffer), name))) | 12515 | && STRINGP (BVAR (XBUFFER (w->buffer), name))) |
| 12518 | ? SSDATA (BVAR (XBUFFER (w->buffer), name)) | 12516 | ? SSDATA (BVAR (XBUFFER (w->buffer), name)) |
| 12519 | : "no buffer"), | 12517 | : "no buffer"), |
| 12520 | buffer); | 12518 | method + len); |
| 12521 | } | 12519 | } |
| 12522 | 12520 | ||
| 12523 | #endif /* GLYPH_DEBUG */ | 12521 | #endif /* GLYPH_DEBUG */ |