From c66f2b6fa5e87101385fd7814eca0f19db49a878 Mon Sep 17 00:00:00 2001 From: "gavin%gavinsharp.com" Date: Wed, 16 Apr 2008 20:06:21 +0000 Subject: [PATCH] Bug 413874: tidy up string usage in mailnews, patch by Emre Birol , r=bienvenu, sr=dveditz git-svn-id: svn://10.0.0.236/trunk@250322 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/mailnews/mime/src/comi18n.cpp | 25 +++++++---- mozilla/mailnews/mime/src/mimedrft.cpp | 9 ++-- mozilla/mailnews/mime/src/mimeeobj.cpp | 7 +-- mozilla/mailnews/mime/src/mimefilt.cpp | 11 +++-- mozilla/mailnews/mime/src/mimei.cpp | 44 ++++++++++--------- mozilla/mailnews/mime/src/mimemoz2.cpp | 10 ++--- mozilla/mailnews/mime/src/mimemsg.cpp | 2 +- mozilla/mailnews/mime/src/mimethtm.cpp | 5 ++- mozilla/mailnews/mime/src/mimetric.cpp | 32 ++++++++++---- mozilla/mailnews/mime/src/mimeunty.cpp | 37 ++++++++-------- .../mailnews/mime/src/nsMsgHeaderParser.cpp | 36 ++++++++++----- 11 files changed, 130 insertions(+), 88 deletions(-) diff --git a/mozilla/mailnews/mime/src/comi18n.cpp b/mozilla/mailnews/mime/src/comi18n.cpp index 454c1340f50..89234750455 100644 --- a/mozilla/mailnews/mime/src/comi18n.cpp +++ b/mozilla/mailnews/mime/src/comi18n.cpp @@ -203,7 +203,7 @@ PRInt32 generate_encodedwords(char *pUTF8, const char *charset, char method, cha char encodedword_head[kMAX_CSNAME+4+1]; nsCAutoString _charset; char *pUTF8Head = nsnull, cUTF8Tmp = 0; - PRInt32 olen = 0, offset, linelen = output_carryoverlen, convlen = 0; + PRInt32 olen = 0, obufsize = outlen, offset, linelen = output_carryoverlen, convlen = 0; PRInt32 encodedword_headlen = 0, encodedword_taillen = foldingonly ? 0 : 2; // "?=" nsresult rv; @@ -324,18 +324,20 @@ PRInt32 generate_encodedwords(char *pUTF8, const char *charset, char method, cha } else { /* no folding needed, let's fall thru */ - strcpy(o, encodedword_head); + PL_strncpyz(o, encodedword_head, obufsize); olen += encodedword_headlen; linelen += encodedword_headlen; + obufsize -= encodedword_headlen; o += encodedword_headlen; if (!foldingonly) *pUCS2 = 0; } } else { - strcpy(o, "\r\n "); + PL_strncpyz(o, "\r\n ", obufsize); olen += 3; o += 3; + obufsize -= 3; linelen = 1; } @@ -344,9 +346,10 @@ PRInt32 generate_encodedwords(char *pUTF8, const char *charset, char method, cha */ while ((foldingonly ? *pUTF8 : *pUCS2) && (olen < outlen)) { - strcpy(o, encodedword_head); + PL_strncpyz(o, encodedword_head, obufsize); olen += encodedword_headlen; linelen += encodedword_headlen; + obufsize -= encodedword_headlen; o += encodedword_headlen; olen += encodedword_taillen; if (foldingonly) @@ -393,8 +396,9 @@ PRInt32 generate_encodedwords(char *pUTF8, const char *charset, char method, cha process_lastline: PRInt32 enclen; if (foldingonly) { - strcpy(o, pUTF8Head); + PL_strncpyz(o, pUTF8Head, obufsize); enclen = strlen(o); + obufsize -= enclen; o += enclen; *pUTF8 = cUTF8Tmp; } @@ -405,12 +409,15 @@ process_lastline: enclen = intlmime_encode_q((const unsigned char *)ibuf, strlen(ibuf), o); PR_Free(ibuf); o += enclen; - strcpy(o, "?="); + obufsize -= enclen; + PL_strncpyz(o, "?=", obufsize); } o += encodedword_taillen; + obufsize -= encodedword_taillen; olen += enclen; if (foldingonly ? *pUTF8 : *pUCS2) { /* not last line */ - strcpy(o, "\r\n "); + PL_strncpyz(o, "\r\n ", obufsize); + obufsize -= 3; o += 3; olen += 3; linelen = 1; @@ -676,7 +683,7 @@ char * apply_rfc2047_encoding(const char *_src, PRBool structured, const char *c break; } if (list->next) { - strcpy(outputtail, ", "); + PL_strncpyz(outputtail, ", ", outputlen); cursor += 2; outputtail += 2; outputlen -= 2; @@ -705,7 +712,7 @@ char * apply_rfc2047_encoding(const char *_src, PRBool structured, const char *c if (cursor + skiplen + overhead < foldlen) { char tmp = *(spacepos + 1); *(spacepos + 1) = '\0'; - strcpy(output, src); + PL_strncpyz(output, src, outputlen); output += skiplen; outputlen -= skiplen; cursor += skiplen; diff --git a/mozilla/mailnews/mime/src/mimedrft.cpp b/mozilla/mailnews/mime/src/mimedrft.cpp index 7ca42b309de..3e56a8fc015 100644 --- a/mozilla/mailnews/mime/src/mimedrft.cpp +++ b/mozilla/mailnews/mime/src/mimedrft.cpp @@ -1507,13 +1507,14 @@ mime_parse_stream_complete (nsMIMESession *stream) bodyLen = strlen(body); } - char* newbody = (char *)PR_MALLOC (bodyLen + 12); //+11 chars for
 & 
tags + PRUint32 newbodylen = bodyLen + 12; //+11 chars for
 & 
tags + char* newbody = (char *)PR_MALLOC (newbodylen); if (newbody) { *newbody = 0; - PL_strcat(newbody, "
");
-                PL_strcat(newbody, body);
-                PL_strcat(newbody, "
"); + PL_strcatn(newbody, newbodylen, "
");
+                PL_strcatn(newbody, newbodylen, body);
+                PL_strcatn(newbody, newbodylen, "
"); PR_Free(body); body = newbody; } diff --git a/mozilla/mailnews/mime/src/mimeeobj.cpp b/mozilla/mailnews/mime/src/mimeeobj.cpp index 988d73566c7..2502d357df1 100644 --- a/mozilla/mailnews/mime/src/mimeeobj.cpp +++ b/mozilla/mailnews/mime/src/mimeeobj.cpp @@ -160,7 +160,8 @@ MimeExternalObject_parse_begin (MimeObject *obj) else { const char *p = "Part "; - char *s = (char *)PR_MALLOC(strlen(p) + strlen(id) + 1); + PRUint32 slen = strlen(p) + strlen(id) + 1; + char *s = (char *)PR_MALLOC(slen); if (!s) { PR_Free(id); @@ -170,8 +171,8 @@ MimeExternalObject_parse_begin (MimeObject *obj) // we have a valid id if (id) id_name = mime_find_suggested_name_of_part(id, obj); - PL_strcpy(s, p); - PL_strcat(s, id); + PL_strncpyz(s, p, slen); + PL_strcatn(s, slen, id); PR_Free(id); id = s; } diff --git a/mozilla/mailnews/mime/src/mimefilt.cpp b/mozilla/mailnews/mime/src/mimefilt.cpp index 324289f9cf2..c7fab026026 100644 --- a/mozilla/mailnews/mime/src/mimefilt.cpp +++ b/mozilla/mailnews/mime/src/mimefilt.cpp @@ -185,14 +185,13 @@ test_image_make_image_html(void *image_data) "an inlined image would have gone here for
"); const char *suffix = "

"; #endif - char *buf; - buf = (char *) PR_MALLOC (strlen (prefix) + strlen (suffix) + - strlen (url) + 20); + PRUint32 buflen = strlen (prefix) + strlen (suffix) + strlen (url) + 20; + char *buf = (char *) PR_MALLOC (buflen); if (!buf) return 0; *buf = 0; - PL_strcat (buf, prefix); - PL_strcat (buf, url); - PL_strcat (buf, suffix); + PL_strcatn (buf, buflen, prefix); + PL_strcatn (buf, buflen, url); + PL_strcatn (buf, buflen, suffix); return buf; } diff --git a/mozilla/mailnews/mime/src/mimei.cpp b/mozilla/mailnews/mime/src/mimei.cpp index 136ec49c7bc..3c8a17a4735 100644 --- a/mozilla/mailnews/mime/src/mimei.cpp +++ b/mozilla/mailnews/mime/src/mimei.cpp @@ -1087,15 +1087,16 @@ mime_part_address(MimeObject *obj) return strdup(buf); else { - char *s = (char *)PR_MALLOC(strlen(higher) + strlen(buf) + 3); + PRUint32 slen = strlen(higher) + strlen(buf) + 3; + char *s = (char *)PR_MALLOC(slen); if (!s) { PR_Free(higher); return 0; /* MIME_OUT_OF_MEMORY */ } - PL_strcpy(s, higher); - PL_strcat(s, "."); - PL_strcat(s, buf); + PL_strncpyz(s, higher, slen); + PL_strcatn(s, slen, "."); + PL_strcatn(s, slen, buf); PR_Free(higher); return s; } @@ -1229,7 +1230,8 @@ mime_set_url_part(const char *url, const char *part, PRBool append_p) } } - result = (char *) PR_MALLOC(strlen(url) + strlen(part) + 10); + PRUint32 resultlen = strlen(url) + strlen(part) + 10; + result = (char *) PR_MALLOC(resultlen); if (!result) return 0; if (part_begin) @@ -1248,17 +1250,17 @@ mime_set_url_part(const char *url, const char *part, PRBool append_p) } else { - PL_strcpy(result, url); + PL_strncpyz(result, url, resultlen); if (got_q) - PL_strcat(result, "&part="); + PL_strcatn(result, resultlen, "&part="); else - PL_strcat(result, "?part="); + PL_strcatn(result, resultlen, "?part="); } - PL_strcat(result, part); + PL_strcatn(result, resultlen, part); if (part_end && *part_end) - PL_strcat(result, part_end); + PL_strcatn(result, resultlen, part_end); /* Semi-broken kludge to omit a trailing "?part=0". */ { @@ -1287,15 +1289,15 @@ mime_set_url_imap_part(const char *url, const char *imappart, const char *libmim *whereCurrent = 0; } - result = (char *) PR_MALLOC(strlen(url) + strlen(imappart) + strlen(libmimepart) + 17); + PRUint32 resultLen = strlen(url) + strlen(imappart) + strlen(libmimepart) + 17; + result = (char *) PR_MALLOC(resultLen); if (!result) return 0; - PL_strcpy(result, url); - PL_strcat(result, "/;section="); - PL_strcat(result, imappart); - PL_strcat(result, "?part="); - PL_strcat(result, libmimepart); - result[strlen(result)] = 0; + PL_strncpyz(result, url, resultLen); + PL_strcatn(result, resultLen, "/;section="); + PL_strcatn(result, resultLen, imappart); + PL_strcatn(result, resultLen, "?part="); + PL_strcatn(result, resultLen, libmimepart); if (whereCurrent) *whereCurrent = '/'; @@ -1605,11 +1607,11 @@ mime_parse_url_options(const char *url, MimeDisplayOptions *options) else if (strcmp(options->part_to_load, "1")) /* not 1 */ { const char *prefix = "1."; - char *s = (char *) PR_MALLOC(strlen(options->part_to_load) + - strlen(prefix) + 1); + PRUint32 slen = strlen(options->part_to_load) + strlen(prefix) + 1; + char *s = (char *) PR_MALLOC(slen); if (!s) return MIME_OUT_OF_MEMORY; - PL_strcpy(s, prefix); - PL_strcat(s, options->part_to_load); + PL_strncpyz(s, prefix, slen); + PL_strcatn(s, slen, options->part_to_load); PR_Free(options->part_to_load); options->part_to_load = s; } diff --git a/mozilla/mailnews/mime/src/mimemoz2.cpp b/mozilla/mailnews/mime/src/mimemoz2.cpp index 513e3af42f4..fb56e8baec9 100644 --- a/mozilla/mailnews/mime/src/mimemoz2.cpp +++ b/mozilla/mailnews/mime/src/mimemoz2.cpp @@ -1209,15 +1209,15 @@ mime_image_make_image_html(void *image_closure) else url = mid->url; - buf = (char *) PR_MALLOC (strlen(prefix) + strlen(suffix) + - strlen(url) + 20) ; + PRUint32 buflen = strlen(prefix) + strlen(suffix) + strlen(url) + 20; + buf = (char *) PR_MALLOC (buflen); if (!buf) return 0; *buf = 0; - PL_strcat (buf, prefix); - PL_strcat (buf, url); - PL_strcat (buf, suffix); + PL_strcatn (buf, buflen, prefix); + PL_strcatn (buf, buflen, url); + PL_strcatn (buf, buflen, suffix); return buf; } diff --git a/mozilla/mailnews/mime/src/mimemsg.cpp b/mozilla/mailnews/mime/src/mimemsg.cpp index 729b734f4fb..859b56f6373 100644 --- a/mozilla/mailnews/mime/src/mimemsg.cpp +++ b/mozilla/mailnews/mime/src/mimemsg.cpp @@ -244,7 +244,7 @@ MimeMessage_parse_line (const char *aLine, PRInt32 aLength, MimeObject *obj) char *s = (char *)PR_MALLOC(length + MSG_LINEBREAK_LEN + 1); if (!s) return MIME_OUT_OF_MEMORY; memcpy(s, line, length); - PL_strcpy(s + length, MSG_LINEBREAK); + PL_strncpyz(s + length, MSG_LINEBREAK, MSG_LINEBREAK_LEN); status = kid->clazz->parse_buffer (s, length + MSG_LINEBREAK_LEN, kid); PR_Free(s); return status; diff --git a/mozilla/mailnews/mime/src/mimethtm.cpp b/mozilla/mailnews/mime/src/mimethtm.cpp index 1cbd307bd67..992dc30b595 100644 --- a/mozilla/mailnews/mime/src/mimethtm.cpp +++ b/mozilla/mailnews/mime/src/mimethtm.cpp @@ -116,7 +116,8 @@ MimeInlineTextHTML_parse_begin (MimeObject *obj) if (base_hdr) { - char *buf = (char *) PR_MALLOC(strlen(base_hdr) + 20); + PRUint32 buflen = strlen(base_hdr) + 20; + char *buf = (char *) PR_MALLOC(buflen); const char *in; char *out; if (!buf) @@ -129,7 +130,7 @@ MimeInlineTextHTML_parse_begin (MimeObject *obj) mail header to be wrapped reasonably. Creators are supposed to insert whitespace every 40 characters or less. */ - PL_strcpy(buf, "= line + length) /* blank line */ { - PL_strcpy (*obufferP, "
"); + PL_strncpyz (*obufferP, "
", *obuffer_sizeP); return output_fn (*obufferP, strlen(*obufferP), closure); } } + PRUint32 outlen = (PRUint32) *obuffer_sizeP; out = *obufferP; *out = 0; @@ -132,6 +133,7 @@ MimeRichtextConvert (const char *line, PRInt32 length, last_end = line; this_start = last_end; this_end = this_start; + PRUint32 addedlen = 0; while (this_end < data_end) { /* Skip forward to next special character. */ @@ -161,24 +163,34 @@ MimeRichtextConvert (const char *line, PRInt32 length, memcpy (out, last_end, this_start - last_end); out += this_start - last_end; *out = 0; + outlen -= (this_start - last_end); } if (this_start >= data_end) break; else if (*this_start == '&') { - PL_strcpy (out, "&"); out += strlen (out); + PL_strncpyz (out, "&", outlen); + addedlen = strlen(out); + outlen -= addedlen; + out += addedlen; } else if (*this_start == '>') { - PL_strcpy (out, ">"); out += strlen (out); + PL_strncpyz (out, ">", outlen); + addedlen = strlen(out); + outlen -= addedlen; + out += addedlen; } else if (enriched_p && this_start < data_end + 1 && this_start[0] == '<' && this_start[1] == '<') { - PL_strcpy (out, "<"); out += strlen (out); + PL_strncpyz (out, "<", outlen); + addedlen = strlen(out); + outlen -= addedlen; + out += addedlen; } else if (this_start != this_end) { @@ -312,13 +324,17 @@ MimeRichtextConvert (const char *line, PRInt32 length, if (this_start[1] == '/') { - if (tag_close) PL_strcpy (out, tag_close); - out += strlen (out); + if (tag_close) PL_strncpyz (out, tag_close, outlen); + addedlen = strlen (out); + outlen -= addedlen; + out += addedlen; } else { - if (tag_open) PL_strcpy (out, tag_open); - out += strlen (out); + if (tag_open) PL_strncpyz (out, tag_open, outlen); + addedlen = strlen (out); + outlen -= addedlen; + out += addedlen; } } diff --git a/mozilla/mailnews/mime/src/mimeunty.cpp b/mozilla/mailnews/mime/src/mimeunty.cpp index a7f04d50a2b..09c19e8b4ea 100644 --- a/mozilla/mailnews/mime/src/mimeunty.cpp +++ b/mozilla/mailnews/mime/src/mimeunty.cpp @@ -319,47 +319,48 @@ MimeUntypedText_open_subpart (MimeObject *obj, uty->open_hdrs = MimeHeaders_new(); if (!uty->open_hdrs) return MIME_OUT_OF_MEMORY; - h = (char *) PR_MALLOC(strlen(type) + - (enc ? strlen(enc) : 0) + - (desc ? strlen(desc) : 0) + - (name ? strlen(name) : 0) + - 100); + PRUint32 hlen = strlen(type) + + (enc ? strlen(enc) : 0) + + (desc ? strlen(desc) : 0) + + (name ? strlen(name) : 0) + + 100; + h = (char *) PR_MALLOC(hlen); if (!h) return MIME_OUT_OF_MEMORY; - PL_strcpy(h, HEADER_CONTENT_TYPE ": "); - PL_strcat(h, type); - PL_strcat(h, MSG_LINEBREAK); + PL_strncpyz(h, HEADER_CONTENT_TYPE ": ", hlen); + PL_strcatn(h, hlen, type); + PL_strcatn(h, hlen, MSG_LINEBREAK); status = MimeHeaders_parse_line(h, strlen(h), uty->open_hdrs); if (status < 0) goto FAIL; if (enc) { - PL_strcpy(h, HEADER_CONTENT_TRANSFER_ENCODING ": "); - PL_strcat(h, enc); - PL_strcat(h, MSG_LINEBREAK); + PL_strncpyz(h, HEADER_CONTENT_TRANSFER_ENCODING ": ", hlen); + PL_strcatn(h, hlen, enc); + PL_strcatn(h, hlen, MSG_LINEBREAK); status = MimeHeaders_parse_line(h, strlen(h), uty->open_hdrs); if (status < 0) goto FAIL; } if (desc) { - PL_strcpy(h, HEADER_CONTENT_DESCRIPTION ": "); - PL_strcat(h, desc); - PL_strcat(h, MSG_LINEBREAK); + PL_strncpyz(h, HEADER_CONTENT_DESCRIPTION ": ", hlen); + PL_strcatn(h, hlen, desc); + PL_strcatn(h, hlen, MSG_LINEBREAK); status = MimeHeaders_parse_line(h, strlen(h), uty->open_hdrs); if (status < 0) goto FAIL; } if (name) { - PL_strcpy(h, HEADER_CONTENT_DISPOSITION ": inline; filename=\""); - PL_strcat(h, name); - PL_strcat(h, "\"" MSG_LINEBREAK); + PL_strncpyz(h, HEADER_CONTENT_DISPOSITION ": inline; filename=\"", hlen); + PL_strcatn(h, hlen, name); + PL_strcatn(h, hlen, "\"" MSG_LINEBREAK); status = MimeHeaders_parse_line(h, strlen(h), uty->open_hdrs); if (status < 0) goto FAIL; } /* push out a blank line. */ - PL_strcpy(h, MSG_LINEBREAK); + PL_strncpyz(h, MSG_LINEBREAK, hlen); status = MimeHeaders_parse_line(h, strlen(h), uty->open_hdrs); if (status < 0) goto FAIL; diff --git a/mozilla/mailnews/mime/src/nsMsgHeaderParser.cpp b/mozilla/mailnews/mime/src/nsMsgHeaderParser.cpp index 37aabc5f988..45c539c662b 100644 --- a/mozilla/mailnews/mime/src/nsMsgHeaderParser.cpp +++ b/mozilla/mailnews/mime/src/nsMsgHeaderParser.cpp @@ -1422,6 +1422,7 @@ msg_remove_duplicate_addresses(const char *addrs, const char *other_addrs, char **a_array1 = 0, **a_array2 = 0, **a_array3 = 0; char **n_array1 = 0, **n_array3 = 0; int i, j; + PRUint32 addedlen = 0; count1 = msg_parse_Header_addresses(addrs, &names1, &addrs1); if (count1 < 0) goto FAIL; @@ -1531,8 +1532,9 @@ msg_remove_duplicate_addresses(const char *addrs, const char *other_addrs, if (count3 > count1) break; } } - - output = (char *)PR_Malloc(size3 + 1); + + PRUint32 outlen = size3 + 1; + output = (char *)PR_Malloc(outlen); if (!output) goto FAIL; *output = 0; @@ -1540,15 +1542,19 @@ msg_remove_duplicate_addresses(const char *addrs, const char *other_addrs, s2 = output; for (i = 0; i < count3; i++) { - PL_strcpy(out, a_array3[i]); - out += strlen(out); + PL_strncpyz(out, a_array3[i], outlen); + addedlen = strlen(out); + outlen -= addedlen; + out += addedlen; *out++ = 0; } s1 = out; for (i = 0; i < count3; i++) { - PL_strcpy(out, n_array3[i]); - out += strlen(out); + PL_strncpyz(out, n_array3[i], outlen); + addedlen = strlen(out); + outlen -= addedlen; + out += addedlen; *out++ = 0; } result = msg_format_Header_addresses(s1, s2, count3, PR_FALSE); @@ -1580,26 +1586,34 @@ msg_make_full_address(const char* name, const char* addr) int nl = name ? strlen (name) : 0; int al = addr ? strlen (addr) : 0; char *buf, *s; + PRUint32 buflen, slen; int L; if (al == 0) return 0; - buf = (char *)PR_Malloc((nl * 2) + (al * 2) + 20); + + buflen = (nl * 2) + (al * 2) + 25; + buf = (char *)PR_Malloc(buflen); if (!buf) return 0; if (nl > 0) { - PL_strcpy(buf, name); + PL_strncpyz(buf, name, buflen); L = msg_quote_phrase_or_addr(buf, nl, PR_FALSE); s = buf + L; - *s++ = ' '; - *s++ = '<'; + slen = buflen - L; + if ( slen > 2 ) { + *s++ = ' '; + *s++ = '<'; + slen -= 2; // for ' ' and '<' + } } else { s = buf; + slen = buflen; } - PL_strcpy(s, addr); + PL_strncpyz(s, addr, slen); L = msg_quote_phrase_or_addr(s, al, PR_TRUE); s += L; if (nl > 0)