lf_to_crlf(): NUL-terminate msg_data::data

Through the rest of the file, the data member of struct msg_data is kept NUL-terminated, and that fact is relied upon in a couple of places. Change lf_to_crlf() to preserve this invariant. In fact, there are no execution paths in which lf_to_crlf() is called and then its data member is required to be NUL-terminated, but it is better to be consistent to prevent future confusion. Document the invariant in the struct msg_data definition. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Nov 25, 2012 at 12:08 UTC 32a8569ecf211cc2ba98333daa10b1a6e783ae87
1 file changed +6 -1
imap-send.c
+6 -1
@@ -69,8 +69,12 @@ struct store {
69 };
70
71 struct msg_data {
72 + /* NUL-terminated data: */
73 char *data;
74 +
75 + /* length of data (not including NUL): */
76 int len;
77 +
78 unsigned char flags;
79 };
80
@@ -1276,7 +1280,7 @@ static void lf_to_crlf(struct msg_data *msg)
1280 lfnum++;
1281 }
1282
1279 - new = xmalloc(msg->len + lfnum);
1283 + new = xmalloc(msg->len + lfnum + 1);
1284 if (msg->data[0] == '\n') {
1285 new[0] = '\r';
1286 new[1] = '\n';
@@ -1297,6 +1301,7 @@ static void lf_to_crlf(struct msg_data *msg)
1301 /* otherwise it already had CR before */
1302 new[j++] = '\n';
1303 }
1304 + new[j] = '\0';
1305 msg->len += lfnum;
1306 free(msg->data);
1307 msg->data = new;