Browse code

Fixed memory leak in handling some multipart messages

git-svn: trunk@197

Nigel Horne authored on 2004/01/23 19:38:22
Showing 1 changed files
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.33  2004/01/23 10:38:22  nigelhorne
21
+ * Fixed memory leak in handling some multipart messages
22
+ *
20 23
  * Revision 1.32  2004/01/23 08:51:19  nigelhorne
21 24
  * Add detection of uuencoded viruses in single part multipart/mixed files
22 25
  *
... ...
@@ -87,7 +90,7 @@
87 87
  * Compilable under SCO; removed duplicate code with message.c
88 88
  *
89 89
  */
90
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.32 2004/01/23 08:51:19 nigelhorne Exp $";
90
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.33 2004/01/23 10:38:22 nigelhorne Exp $";
91 91
 
92 92
 #ifndef	CL_DEBUG
93 93
 /*#define	NDEBUG	/* map CLAMAV debug onto standard */
... ...
@@ -141,7 +144,7 @@ typedef enum    { FALSE = 0, TRUE = 1 } bool;
141 141
 
142 142
 static	message	*parseEmailHeaders(const message *m, const table_t *rfc821Table);
143 143
 static	int	parseEmailHeader(message *m, const char *line, const table_t *rfc821Table);
144
-static	int	parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, const char *dir, table_t *rfc821Table, table_t *subtypeTable);
144
+static	int	parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, const char *dir, table_t *rfc821Table, table_t *subtypeTable);
145 145
 static	int	boundaryStart(const char *line, const char *boundary);
146 146
 static	int	endOfMessage(const char *line, const char *boundary);
147 147
 static	int	initialiseTables(table_t **rfc821Table, table_t **subtypeTable);
... ...
@@ -481,13 +484,14 @@ parseEmailHeader(message *m, const char *line, const table_t *rfc821Table)
481 481
  *	2 for success, attachments not saved
482 482
  */
483 483
 static int	/* success or fail */
484
-parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, const char *dir, table_t *rfc821Table, table_t *subtypeTable)
484
+parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, const char *dir, table_t *rfc821Table, table_t *subtypeTable)
485 485
 {
486 486
 	message *messages[MAXALTERNATIVE];
487 487
 	int inhead, inMimeHead, i, rc, htmltextPart, multiparts = 0;
488 488
 	text *aText;
489 489
 	blob *blobList[MAX_ATTACHMENTS], **blobs;
490 490
 	const char *cptr;
491
+	message *mainMessage;
491 492
 
492 493
 	cli_dbgmsg("in parseEmailBody(nBlobs = %d)\n", nBlobs);
493 494
 
... ...
@@ -499,6 +503,7 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
499 499
 
500 500
 	aText = textIn;
501 501
 	blobs = blobsIn;
502
+	mainMessage = messageIn;
502 503
 
503 504
 	/* Anything left to be parsed? */
504 505
 	if(mainMessage && (messageGetBody(mainMessage) != NULL)) {
... ...
@@ -650,8 +655,11 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
650 650
 
651 651
 			free((char *)boundary);
652 652
 
653
-			if(multiparts == 0)
653
+			if(multiparts == 0) {
654
+				if(mainMessage && (mainMessage != messageIn))
655
+					messageDestroy(mainMessage);
654 656
 				return 2;	/* Nothing to do */
657
+			}
655 658
 
656 659
 			cli_dbgmsg("The message has %d parts\n", multiparts);
657 660
 			cli_dbgmsg("Find out the multipart type(%s)\n", mimeSubtype);
... ...
@@ -762,8 +770,11 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
762 762
 				 * common enough to implement, it is a simple
763 763
 				 * matter to add it
764 764
 				 */
765
-				if(aText)
765
+				if(aText) {
766
+					if(mainMessage && (mainMessage != messageIn))
767
+						messageDestroy(mainMessage);
766 768
 					mainMessage = NULL;
769
+				}
767 770
 
768 771
 				cli_dbgmsg("Mixed message with %d parts\n", multiparts);
769 772
 				for(i = 0; i < multiparts; i++) {
... ...
@@ -801,6 +812,8 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
801 801
 
802 802
 						break;
803 803
 					case NOMIME:
804
+						if(mainMessage && (mainMessage != messageIn))
805
+							messageDestroy(mainMessage);
804 806
 						mainMessage = NULL;
805 807
 						addToText = TRUE;
806 808
 						if(messageGetBody(aMessage) == NULL)
... ...
@@ -818,6 +831,8 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
818 818
 						else if((*dtype == '\0') || (strcasecmp(dtype, "inline") == 0)) {
819 819
 							const text *t_line = uuencodeBegin(aMessage);
820 820
 
821
+							if(mainMessage && (mainMessage != messageIn))
822
+								messageDestroy(mainMessage);
821 823
 							mainMessage = NULL;
822 824
 							if(t_line) {
823 825
 								cli_dbgmsg("Found uuencoded message in multipart/mixed text portion\n");
... ...
@@ -863,13 +878,19 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
863 863
 							body = parseEmailHeaders(aMessage, rfc821Table);
864 864
 							if(body) {
865 865
 								t = messageToText(body);
866
+
866 867
 								rc = parseEmailBody(body, blobs, nBlobs, t, dir, rfc821Table, subtypeTable);
868
+								cli_dbgmsg("Finished recursion\n");
867 869
 								textDestroy(t);
870
+								if(mainMessage && (mainMessage != messageIn))
871
+									messageDestroy(mainMessage);
868 872
 
869 873
 								mainMessage = body;
870 874
 							}
871 875
 						} else {
872 876
 							rc = parseEmailBody(NULL, blobs, nBlobs, NULL, dir, rfc821Table, subtypeTable);
877
+							if(mainMessage && (mainMessage != messageIn))
878
+								messageDestroy(mainMessage);
873 879
 							mainMessage = NULL;
874 880
 						}
875 881
 						continue;
... ...
@@ -985,6 +1006,9 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
985 985
 			if(blobs && (blobsIn == NULL))
986 986
 				puts("arraydestroy");
987 987
 
988
+			if(mainMessage && (mainMessage != messageIn))
989
+				messageDestroy(mainMessage);
990
+
988 991
 			if(aText && (textIn == NULL))
989 992
 				textDestroy(aText);
990 993
 
... ...
@@ -1039,6 +1063,7 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
1039 1039
 				m = body;
1040 1040
 
1041 1041
 				messageClean(m);
1042
+
1042 1043
 				if(messageGetBody(m))
1043 1044
 					rc = parseEmailBody(m, NULL, 0, NULL, dir, rfc821Table, subtypeTable);
1044 1045
 
... ...
@@ -1058,6 +1083,8 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
1058 1058
 			else
1059 1059
 				cli_warnmsg("Unsupported message format `%s'\n", mimeSubtype);
1060 1060
 
1061
+			if(mainMessage && (mainMessage != messageIn))
1062
+				messageDestroy(mainMessage);
1061 1063
 			return 0;
1062 1064
 
1063 1065
 		case APPLICATION:
... ...
@@ -1187,6 +1214,9 @@ parseEmailBody(message *mainMessage, blob **blobsIn, int nBlobs, text *textIn, c
1187 1187
 	if(blobs && (blobsIn == NULL))
1188 1188
 		blobArrayDestroy(blobs, nBlobs);
1189 1189
 
1190
+	if(mainMessage && (mainMessage != messageIn))
1191
+		messageDestroy(mainMessage);
1192
+
1190 1193
 	cli_dbgmsg("parseEmailBody() returning %d\n", rc);
1191 1194
 
1192 1195
 	return rc;