[PATCH] memory allocation bug in cfgreader.c

Guido Falsi <[email protected]> Wed, 14 Aug 2013 15:21:41 +0200
Newsgroups gmane.linux.drivers.gnokii
Message-ID <[email protected]>
Hi!

Mike Barnard Kwatampora <[email protected]>, A fellow FreeBSD 
user, reported me crashes when running gnokii as a user on his system.

He also was very helpful in debugging the problem.

After some digging I noticed that the realloc calls int eh function 
get_locations() have a wrong second argument, not accounting for the 
size of char *.

Attached is a simple patch which fixes this.

I hope my analysis is correct :)

-- 
Guido Falsi <[email protected]>

_______________________________________________
gnokii-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/gnokii-users
realloc.diff (text/plain, 747 B)
diff --git a/common/cfgreader.c b/common/cfgreader.c
index 7359081..89af7ba 100644
--- a/common/cfgreader.c
+++ b/common/cfgreader.c
@@ -1000,7 +1000,7 @@ static bool cfg_get_log_target(gn_log_target *t, const char *opt)
 #define CHECK_SIZE()	if (*retval >= size) { \
 	void *aux; \
 	size *= 2; \
-	aux = realloc(config_file_locations, size); \
+	aux = realloc(config_file_locations, size * sizeof(char *)); \
 	if (aux) \
 		config_file_locations = aux; \
 	else {\
@@ -1116,7 +1116,7 @@ static char **get_locations(int *retval)
 		if (i >= xcd_size) {
 			void *aux;
 			xcd_size *= 2;
-			aux = realloc(xdg_config_dir, xcd_size);
+			aux = realloc(xdg_config_dir, xcd_size * sizeof(char *));
 			if (aux)
 				xdg_config_dir = aux;
 			else {