[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MiNT] mktbl.c - not a single free() but several malloc's



I just had a quick look at mktbl.c - and the first thing that I recognize
is that it doesn't free the memory that was allocated with malloc();

Is that OK, will freemint free that memory? What If I run that tool with
plain TOS?

Patch is attached.
--- tools/mktbl/mktbl.c.orig	2010-08-10 20:54:45.000000000 +0200
+++ tools/mktbl/mktbl.c	2010-08-10 21:13:02.000000000 +0200
@@ -116,18 +116,21 @@
 int
 main(int argc, char **argv)
 {
-	char *line, *ln, *outname, *o;
+	char *line, *ln, *outname, *free_outname, *o;
 	long r, n = 1, num, flen;
 	short w, warn = 0;
 	size_t buf;
 	FILE *fd, *out;
 
+	fd = 0;
+	out = 0;
+	free_outname = NULL;
+	line = NULL;
 	if (argc <= 2)
 	{
 		if (argc < 2 || (argc >= 2 && (strcmp(argv[1], "--help") == 0)))
 		{
 			printf("Usage: %s src-file [tbl-file]\n", argv[0]);
-
 			return 1;
 		}
 
@@ -138,6 +141,7 @@
 			fprintf(stderr, "Out of RAM\n");
 			return 10;
 		}
+		free_outname = outname;
 		strcpy(outname, argv[1]);
 		o = strrchr(outname, '.');
 		if (o == NULL)
@@ -152,17 +156,23 @@
 
 	buf = 1024;
 	line = malloc(buf);	/* should be plenty */
-	if (!line)
-		return 2;
-
+	if (!line) 
+	{
+		r = 2;
+		goto error;
+	}
 	fd = fopen(argv[1], "r");
 	if (!fd)
-		return 3;
-
+	{
+		r = 3;
+		goto error;
+	}
 	out = fopen(outname, "w");
 	if (!fd)
-		return 4;
-
+	{
+		r = 4;
+		goto error;
+	}
 	do
 	{
 		r = getdelim(&line, &buf, '\n', fd);
@@ -265,12 +275,16 @@
 	r = 0;
 
 error:
-	fclose(fd);
-	fclose(out);
-
+	if (fd)
+		fclose(fd);
+	if(out)	
+		fclose(out);
 	if (r)
 		unlink(outname);
-
+	if (free_outname)
+		free(outname);
+	if (line)
+		free(line);
 	return r;
 }