tech-toolchain archive

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

Re: [PATCH] make: Don't check for non-NULL when calling free()



Tilman Sauerbeck [2015-10-05 21:27]:

> here's a patch for make that removes some redundant checks for non-NULL
> pointers when calling free() (attached).

I attached a second patch that removes more of these unnecessary
NULL-checks.

Regards,
Tilman

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
diff --git a/usr.bin/make/arch.c b/usr.bin/make/arch.c
index 5dbe340..936fb53 100644
--- a/usr.bin/make/arch.c
+++ b/usr.bin/make/arch.c
@@ -204,8 +204,7 @@ ArchFree(void *ap)
 	free(Hash_GetValue(entry));
 
     free(a->name);
-    if (a->fnametab)
-	free(a->fnametab);
+    free(a->fnametab);
     Hash_DeleteTable(&a->members);
     free(a);
 }
@@ -262,8 +261,8 @@ Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
 	    char	*result;
 
 	    result = Var_Parse(cp, ctxt, TRUE, TRUE, FALSE, &length, &freeIt);
-	    if (freeIt)
-		free(freeIt);
+	    free(freeIt);
+
 	    if (result == var_Error) {
 		return(FAILURE);
 	    } else {
@@ -303,8 +302,8 @@ Arch_ParseArchive(char **linePtr, Lst nodeLst, GNode *ctxt)
 		char	*result;
 
 		result = Var_Parse(cp, ctxt, TRUE, TRUE, FALSE, &length, &freeIt);
-		if (freeIt)
-		    free(freeIt);
+		free(freeIt);
+
 		if (result == var_Error) {
 		    return(FAILURE);
 		} else {
@@ -711,8 +710,7 @@ ArchStatMember(char *archive, char *member, Boolean hash)
 badarch:
     fclose(arch);
     Hash_DeleteTable(&ar->members);
-    if (ar->fnametab)
-	free(ar->fnametab);
+    free(ar->fnametab);
     free(ar);
     return NULL;
 }
@@ -997,10 +995,10 @@ Arch_Touch(GNode *gn)
     arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
 			  Var_Value(MEMBER, gn, &p2),
 			  &arh, "r+");
-    if (p1)
-	free(p1);
-    if (p2)
-	free(p2);
+
+    free(p1);
+    free(p2);
+
     snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
 
     if (arch != NULL) {
@@ -1079,10 +1077,9 @@ Arch_MTime(GNode *gn)
     arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
 			     Var_Value(MEMBER, gn, &p2),
 			     TRUE);
-    if (p1)
-	free(p1);
-    if (p2)
-	free(p2);
+
+    free(p1);
+    free(p2);
 
     if (arhPtr != NULL) {
 	modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
diff --git a/usr.bin/make/compat.c b/usr.bin/make/compat.c
index 2fbf91d..58c6ce6 100644
--- a/usr.bin/make/compat.c
+++ b/usr.bin/make/compat.c
@@ -143,8 +143,8 @@ CompatInterrupt(int signo)
 	if (!noExecute && eunlink(file) != -1) {
 	    Error("*** %s removed", file);
 	}
-	if (p1)
-	    free(p1);
+
+	free(p1);
 
 	/*
 	 * Run .INTERRUPT only if hit with interrupt signal
@@ -371,10 +371,10 @@ again:
 	execError("exec", av[0]);
 	_exit(1);
     }
-    if (mav)
-	free(mav);
-    if (bp)
-	free(bp);
+
+    free(mav);
+    free(bp);
+
     Lst_Replace(cmdNode, NULL);
 
 #ifdef USE_META
@@ -513,8 +513,7 @@ Compat_Make(void *gnp, void *pgnp)
 	if (Lst_Member(gn->iParents, pgn) != NULL) {
 	    char *p1;
 	    Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
-	    if (p1)
-		free(p1);
+	    free(p1);
 	}
 
 	/*
@@ -617,8 +616,7 @@ Compat_Make(void *gnp, void *pgnp)
 	if (Lst_Member(gn->iParents, pgn) != NULL) {
 	    char *p1;
 	    Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
-	    if (p1)
-		free(p1);
+	    free(p1);
 	}
 	switch(gn->made) {
 	    case BEINGMADE:
diff --git a/usr.bin/make/cond.c b/usr.bin/make/cond.c
index 47097b5..8e999dd 100644
--- a/usr.bin/make/cond.c
+++ b/usr.bin/make/cond.c
@@ -291,8 +291,7 @@ CondGetArg(char **linePtr, char **argPtr, const char *func)
 
 	    cp2 = Var_Parse(cp, VAR_CMD, TRUE, TRUE, FALSE, &len, &freeIt);
 	    Buf_AddBytes(&buf, strlen(cp2), cp2);
-	    if (freeIt)
-		free(freeIt);
+	    free(freeIt);
 	    cp += len;
 	    continue;
 	}
@@ -346,8 +345,8 @@ CondDoDefined(int argLen MAKE_ATTR_UNUSED, const char *arg)
     } else {
 	result = FALSE;
     }
-    if (p1)
-	free(p1);
+
+    free(p1);
     return (result);
 }
 
@@ -805,10 +804,8 @@ do_string_compare:
     }
 
 done:
-    if (lhsFree)
-	free(lhsFree);
-    if (rhsFree)
-	free(rhsFree);
+    free(lhsFree);
+    free(rhsFree);
     return t;
 }
 
@@ -848,8 +845,7 @@ get_mpt_arg(char **linePtr, char **argPtr, const char *func MAKE_ATTR_UNUSED)
      * true/false here.
      */
     length = *val ? 2 : 1;
-    if (freeIt)
-	free(freeIt);
+    free(freeIt);
     return length;
 }
 
@@ -900,8 +896,7 @@ compare_function(Boolean doEval)
 	}
 	/* Evaluate the argument using the required function. */
 	t = !doEval || fn_def->fn_proc(arglen, arg);
-	if (arg)
-	    free(arg);
+	free(arg);
 	condExpr = cp;
 	return t;
     }
@@ -933,8 +928,7 @@ compare_function(Boolean doEval)
      * be empty - even if it contained a variable expansion.
      */
     t = !doEval || if_info->defProc(arglen, arg) != if_info->doNot;
-    if (arg)
-	free(arg);
+    free(arg);
     return t;
 }
 
diff --git a/usr.bin/make/job.c b/usr.bin/make/job.c
index 8e74ce5..4af6a2f 100644
--- a/usr.bin/make/job.c
+++ b/usr.bin/make/job.c
@@ -853,8 +853,7 @@ JobPrintCommand(void *cmdp, void *jobp)
     
     DBPRINTF(cmdTemplate, cmd);
     free(cmdStart);
-    if (escCmd)
-        free(escCmd);
+    free(escCmd);
     if (errOff) {
 	/*
 	 * If echoing is already off, there's no point in issuing the
@@ -1220,8 +1219,7 @@ Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
 	     */
 	    Make_HandleUse(DEFAULT, gn);
 	    Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0);
-	    if (p1)
-		free(p1);
+	    free(p1);
 	} else if (Dir_MTime(gn, 0) == 0 && (gn->type & OP_SPECIAL) == 0) {
 	    /*
 	     * The node wasn't the target of an operator we have no .DEFAULT
@@ -2386,8 +2384,7 @@ Job_ParseShell(char *line)
 	line++;
     }
 
-    if (shellArgv)
-	free(UNCONST(shellArgv));
+    free(UNCONST(shellArgv));
 
     memset(&newShell, 0, sizeof(newShell));
 
@@ -2635,8 +2632,7 @@ void
 Job_End(void)
 {
 #ifdef CLEANUP
-    if (shellArgv)
-	free(shellArgv);
+    free(shellArgv);
 #endif
 }
 
diff --git a/usr.bin/make/main.c b/usr.bin/make/main.c
index a818199..7ce5915 100644
--- a/usr.bin/make/main.c
+++ b/usr.bin/make/main.c
@@ -665,8 +665,7 @@ Main_ParseArgLine(const char *line)
 
 	buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2);
 	(void)snprintf(buf, len, "%s %s", argv0, line);
-	if (p1)
-		free(p1);
+	free(p1);
 
 	argv = brk_string(buf, &argc, TRUE, &args);
 	if (argv == NULL) {
@@ -716,8 +715,7 @@ Main_SetObjdir(const char *path)
 		}
 	}
 
-	if (p)
-		free(p);
+	free(p);
 	return rc;
 }
 
@@ -788,8 +786,8 @@ MakeMode(const char *mode)
 	    meta_mode_init(mode);
 #endif
     }
-    if (mp)
-	free(mp);
+
+    free(mp);
 }
 
 /*-
@@ -1241,8 +1239,7 @@ main(int argc, char **argv)
 	MakeMode(NULL);
 
 	Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
-	if (p1)
-	    free(p1);
+	free(p1);
 
 	if (!compatMake)
 	    Job_ServerStart(maxJobTokens, jp_0, jp_1);
@@ -1329,8 +1326,7 @@ main(int argc, char **argv)
 				value = Var_Value(var, VAR_GLOBAL, &p1);
 			}
 			printf("%s\n", value ? value : "");
-			if (p1)
-				free(p1);
+			free(p1);
 		}
 	} else {
 		/*
@@ -1454,8 +1450,7 @@ ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
 			name = Dir_FindFile(fname,
 				Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
 		if (!name || (fd = open(name, O_RDONLY)) == -1) {
-			if (name)
-				free(name);
+			free(name);
 			free(path);
 			return(-1);
 		}
diff --git a/usr.bin/make/make.c b/usr.bin/make/make.c
index 9088b82..6c9bcfa 100644
--- a/usr.bin/make/make.c
+++ b/usr.bin/make/make.c
@@ -482,8 +482,7 @@ Make_HandleUse(GNode *cgn, GNode *pgn)
 	    if (gn->uname == NULL) {
 		gn->uname = gn->name;
 	    } else {
-		if (gn->name)
-		    free(gn->name);
+		free(gn->name);
 	    }
 	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE, TRUE, FALSE);
 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
@@ -692,8 +691,7 @@ Make_Update(GNode *cgn)
     checked++;
 
     cname = Var_Value(TARGET, cgn, &p1);
-    if (p1)
-	free(p1);
+    free(p1);
 
     if (DEBUG(MAKE))
 	fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
@@ -838,8 +836,7 @@ Make_Update(GNode *cgn)
 		    Var_Set(PREFIX, cpref, pgn, 0);
 	    }
 	}
-	if (p1)
-	    free(p1);
+	free(p1);
 	Lst_Close(cgn->iParents);
     }
 }
@@ -907,8 +904,7 @@ MakeAddAllSrc(void *cgnp, void *pgnp)
 	}
 	if (allsrc != NULL)
 		Var_Append(ALLSRC, allsrc, pgn);
-	if (p2)
-	    free(p2);
+	free(p2);
 	if (pgn->type & OP_JOIN) {
 	    if (cgn->made == MADE) {
 		Var_Append(OODATE, child, pgn);
@@ -934,8 +930,7 @@ MakeAddAllSrc(void *cgnp, void *pgnp)
 	     */
 	    Var_Append(OODATE, child, pgn);
 	}
-	if (p1)
-	    free(p1);
+	free(p1);
     }
     return (0);
 }
@@ -981,8 +976,7 @@ Make_DoAllVar(GNode *gn)
     if (gn->type & OP_JOIN) {
 	char *p1;
 	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
-	if (p1)
-	    free(p1);
+	free(p1);
     }
     gn->flags |= DONE_ALLSRC;
 }
diff --git a/usr.bin/make/meta.c b/usr.bin/make/meta.c
index aa889a7..af3435c 100644
--- a/usr.bin/make/meta.c
+++ b/usr.bin/make/meta.c
@@ -296,8 +296,7 @@ meta_name(struct GNode *gn, char *mname, size_t mnamelen,
     }
     free(tp);
     for (i--; i >= 0; i--) {
-	if (p[i])
-	    free(p[i]);
+	free(p[i]);
     }
     return (mname);
 }
@@ -349,8 +348,7 @@ is_submake(void *cmdp, void *gnp)
 	    }
 	}
     }
-    if (mp)
-	free(mp);
+    free(mp);
     return (rc);
 }
 
@@ -370,8 +368,7 @@ printCMD(void *cmdp, void *mfpp)
 	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE, TRUE);
     }
     fprintf(mfp->fp, "CMD %s\n", cmd);
-    if (cp)
-	free(cp);
+    free(cp);
     return 0;
 }
 
@@ -520,8 +517,7 @@ meta_create(BuildMon *pbm, GNode *gn)
     }
  out:
     for (i--; i >= 0; i--) {
-	if (p[i])
-	    free(p[i]);
+	free(p[i]);
     }
 
     return (mf.fp);
@@ -1031,14 +1027,12 @@ meta_oodate(GNode *gn, Boolean oodate)
 			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
 			if (ldir) {
 			    strlcpy(latestdir, ldir, sizeof(latestdir));
-			    if (tp)
-				free(tp);
+			    free(tp);
 			}
 			ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
 			if (ldir) {
 			    strlcpy(lcwd, ldir, sizeof(lcwd));
-			    if (tp)
-				free(tp);
+			    free(tp);
 			}
 		    }
 		    /* Skip past the pid. */
@@ -1395,8 +1389,7 @@ meta_oodate(GNode *gn, Boolean oodate)
 	 */
 	Var_Delete(OODATE, gn);
 	Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
-	if (cp)
-	    free(cp);
+	free(cp);
     }
     return oodate;
 }
diff --git a/usr.bin/make/parse.c b/usr.bin/make/parse.c
index fdc024b..cf9428c 100644
--- a/usr.bin/make/parse.c
+++ b/usr.bin/make/parse.c
@@ -1220,8 +1220,7 @@ ParseDoDependency(char *line)
 		void    *freeIt;
 
 		(void)Var_Parse(cp, VAR_CMD, TRUE, TRUE, FALSE, &length, &freeIt);
-		if (freeIt)
-		    free(freeIt);
+		free(freeIt);
 		cp += length-1;
 	    }
 	}
@@ -2325,10 +2324,8 @@ ParseSetIncludedFile(void)
 	fprintf(debug_file, "%s: ${.INCLUDEDFROMDIR} = `%s' "
 	    "${.INCLUDEDFROMFILE} = `%s'\n", __func__, pd, pf);
 
-    if (fp)
-	free(fp);
-    if (dp)
-	free(dp);
+    free(fp);
+    free(dp);
 }
 /*-
  *---------------------------------------------------------------------
diff --git a/usr.bin/make/suff.c b/usr.bin/make/suff.c
index a84f366..393aec3 100644
--- a/usr.bin/make/suff.c
+++ b/usr.bin/make/suff.c
@@ -1614,8 +1614,7 @@ SuffExpandChildren(LstNode cln, GNode *pgn)
 			cp += len - 1;
 		    }
 
-		    if (freeIt)
-			free(freeIt);
+		    free(freeIt);
 		} else if (*cp == '\\' && *cp != '\0') {
 		    /*
 		     * Escaped something -- skip over it
@@ -1933,8 +1932,7 @@ SuffFindArchiveDeps(GNode *gn, Lst slst)
     for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
 	char *p1;
 	Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn, 0);
-	if (p1)
-	    free(p1);
+	free(p1);
 
     }
 
diff --git a/usr.bin/make/targ.c b/usr.bin/make/targ.c
index 5db5477..15ad1c0 100644
--- a/usr.bin/make/targ.c
+++ b/usr.bin/make/targ.c
@@ -292,10 +292,8 @@ TargFreeGN(void *gnp)
 
 
     free(gn->name);
-    if (gn->uname)
-	free(gn->uname);
-    if (gn->path)
-	free(gn->path);
+    free(gn->uname);
+    free(gn->path);
     /* gn->fname points to name allocated when file was opened, don't free */
 
     Lst_Destroy(gn->iParents, NULL);


Home | Main Index | Thread Index | Old Index