shithub: scc

Download patch

ref: 30c13a046ae2be0b83933a64f197250430f21c68
parent: 355f73cb919d009d7527b5083ae8341f0ceea97d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jun 1 02:13:32 EDT 2022

driver/posix: Don't free in error

In case of error we were freeing some memory, but some of that
memory was not dynamically allocated, and in some cases it was
string contants. The best approach in this case is not freeing
anything because we are not going to save anything since at that
stage we can only finish with error.

--- a/src/cmd/cc/posix/cc.c
+++ b/src/cmd/cc/posix/cc.c
@@ -193,7 +193,7 @@
 		for (n = 0; ldflags[n]; ++n)
 			addarg(tool, ldflags[n]);
 		addarg(tool, "-o");
-		t->outfile = outfile ? outfile : xstrdup("a.out");
+		t->outfile = outfile ? outfile : "a.out";
 		addarg(tool, t->outfile);
 		for (n = 0; syslibs[n]; ++n) {
 			addarg(tool, "-L");
@@ -283,7 +283,7 @@
 			objfile = (cflag || kflag) ? infile : NULL;
 			objfile = outfname(objfile, "o");
 		}
-		t->outfile = xstrdup(objfile);
+		t->outfile = objfile;
 		addarg(tool, t->outfile);
 		break;
 	default:
@@ -296,7 +296,7 @@
 	} else {
 		t->in = -1;
 		if (infile)
-			addarg(tool, xstrdup(infile));
+			addarg(tool, infile);
 	}
 
 	if (nexttool < LAST_TOOL) {
@@ -415,15 +415,11 @@
 			failed = tool;
 		if (tool >= failed && t->outfile)
 			unlink(t->outfile);
-		for (i = t->nparams; i < t->args.n; ++i)
-			free(t->args.s[i]);
 		t->args.n = t->nparams;
 		t->pid = 0;
 	}
 	if (failed < LAST_TOOL) {
 		unlink(objfile);
-		free(objfile);
-		objfile = NULL;
 		return 0;
 	}
 
@@ -576,7 +572,7 @@
 		arch = EARGF(usage());
 		break;
 	case 'o':
-		outfile = xstrdup(EARGF(usage()));
+		outfile = EARGF(usage());
 		break;
 	case 's':
 		sflag = 1;
@@ -638,8 +634,8 @@
 		for (n = 0; n < linkargs.n; ++n)
 			addarg(LD, linkargs.s[n]);
 
-		addarg(LD, xstrdup("-lc"));
-		addarg(LD, xstrdup("-lcrt"));
+		addarg(LD, "-lc");
+		addarg(LD, "-lcrt");
 
 		for (n = 0; syscrtse[n]; ++n)
 			addarg(LD, path(syscrtse[n]));