[Commits] vcard-extension.c 1.8 1.9

miras at claws-mail.org miras at claws-mail.org
Mon Jan 28 02:22:10 CET 2013


Update of /home/claws-mail/contacts/extensions/vcard/src
In directory srv:/tmp/cvs-serv27205/extensions/vcard/src

Modified Files:
	vcard-extension.c 
Log Message:
2013-01-28 [mir]	0.6.0cvs92

	* extensions/vcard/src/vcard-extension.c
	    Fix some memory allocation errors. 

Index: vcard-extension.c
===================================================================
RCS file: /home/claws-mail/contacts/extensions/vcard/src/vcard-extension.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- vcard-extension.c	27 Jan 2013 23:54:41 -0000	1.8
+++ vcard-extension.c	28 Jan 2013 01:22:08 -0000	1.9
@@ -104,7 +104,12 @@
 
 static NormalizeFile* buffer_resize(NormalizeFile* buffer, long size) {
 	if (buffer->buffer) {
-		buffer->buffer = g_renew(gchar, buffer->buffer, size);
+		/*buffer->buffer = g_renew(gchar, buffer->buffer, size);*/
+		gchar* tmp = g_new0(gchar, size);
+		tmp = g_memdup(buffer->buffer, buffer->size);
+		g_free(buffer->buffer);
+		buffer->buffer = g_memdup(tmp, size);
+		g_free(tmp);
 	}
 	else {
 		buffer->buffer = g_new0(gchar, size);
@@ -114,10 +119,11 @@
 	return buffer;
 }
 
+#define bufsize 1024
 static NormalizeFile* normalize_file(const MainWindow* mainwindow, gchar* filename) {
 	NormalizeFile* file_buffer = NULL;
 	size_t numread;
-	gchar buf[BUFSIZ];
+	gchar* buf = NULL;
 	int i;
 	int size = 0;
 	long destpos = 0;
@@ -128,17 +134,29 @@
 		return NULL;
 	}
 	file_buffer = g_new0(NormalizeFile, 1);
-	while ((numread = fread(&buf, BUFSIZ, 1, fp)) != 0) {
+	do {
+		g_free(buf);
+		buf = g_new0(gchar, bufsize);
+		numread = fread(buf, bufsize, 1, fp);
+		if (ferror(fp)) {
+			show_message(mainwindow->window, GTK_UTIL_MESSAGE_ERROR,
+				strerror(errno));
+			g_free(file_buffer->buffer);
+			g_free(file_buffer);
+			fclose(fp);
+			return NULL;
+		}
 		size++;
-		file_buffer = buffer_resize(file_buffer, size * BUFSIZ);
+		file_buffer = buffer_resize(file_buffer, size * bufsize);
 		if (! file_buffer->buffer) {
 			show_message(mainwindow->window, GTK_UTIL_MESSAGE_ERROR,
 				_("Insufficient memory"));
 			g_free(file_buffer->buffer);
 			g_free(file_buffer);
+			fclose(fp);
 			return NULL;
 		}
-		for (i = 0; i < BUFSIZ && buf[i]; i++) {
+		for (i = 0; i < bufsize && buf[i]; i++) {
 			if (destpos > 0 && file_buffer->buffer[destpos - 1] == '\n' && buf[i] == ' ') {
 				destpos--;
 			}
@@ -146,12 +164,14 @@
 				file_buffer->buffer[destpos++] = buf[i];
 			}
 		}
-	}
+	} while (numread != 0);
+	g_free(buf);
 	fclose(fp);
-	if ((size * BUFSIZ) % destpos) {
-		file_buffer = buffer_resize(file_buffer, destpos + 1);
+	if (destpos && (size * bufsize) % destpos == 0) {
+		file_buffer = buffer_resize(file_buffer, ++destpos);
 	}
-	file_buffer->buffer[++destpos] = 0;
+	file_buffer->buffer[destpos] = 0;
+	file_buffer->size = destpos;
 	
 	return file_buffer;
 }



More information about the Commits mailing list