[PATCH] Remove useless if-before-free tests.

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

 



Hi Karel,

E.g., in this example, the "if (p)" test is useless.

  if (p)
    free (p);

I've been removing such tests systematically.
Here's where I proposed it to the git folks, along with justification
for why it's ok to perform this transformation, these days (no one
uses SunOS4 anymore):

    http://thread.gmane.org/gmane.comp.version-control.git/74187


Signed-off-by: Jim Meyering <meyering@xxxxxxxxxx>
---
 login-utils/setpwnam.c   |    2 +-
 login-utils/simpleinit.c |    4 ++--
 mount/mount_mntent.c     |    3 +--
 mount/realpath.c         |    9 +++------
 partx/crc32.c            |    4 ++--
 partx/gpt.c              |    6 ++----
 6 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c
index 5a46863..2aa7dd5 100644
--- a/login-utils/setpwnam.c
+++ b/login-utils/setpwnam.c
@@ -185,7 +185,7 @@ fail:
     if (fp != NULL) fclose (fp);
     if (pwf != NULL) fclose(pwf);
     if (fd >= 0) close (fd);
-    if (linebuf != NULL) free(linebuf);
+    free(linebuf);
     unlink(PTMP_FILE);
     errno = save_errno;
     return -1;
diff --git a/login-utils/simpleinit.c b/login-utils/simpleinit.c
index 89c14a8..739306c 100644
--- a/login-utils/simpleinit.c
+++ b/login-utils/simpleinit.c
@@ -1048,7 +1048,7 @@ static int run_command (const char *file, const char *name, pid_t pid)

 	if ( ( script = calloc (1, sizeof *script) ) == NULL )
 	{
-	    if (needer != NULL) free (needer);
+	    free (needer);
 	    return SIG_FAILED;
 	}
 	service = calloc (1, strlen (name) + sizeof *service);
@@ -1073,7 +1073,7 @@ static int run_command (const char *file, const char *name, pid_t pid)
 		unavailable_services->prev = service;
 	    unavailable_services = service;
 	    free (script);
-	    if (needer != NULL) free (needer);
+	    free (needer);
 	    return SIG_FAILED;
 	    /*break;*/
 	  default:  /*  Parent  */
diff --git a/mount/mount_mntent.c b/mount/mount_mntent.c
index 7ac312e..e6e64bb 100644
--- a/mount/mount_mntent.c
+++ b/mount/mount_mntent.c
@@ -112,8 +112,7 @@ my_endmntent (mntFILE *mfp) {
 	if (mfp) {
 		if (mfp->mntent_fp)
 			fclose(mfp->mntent_fp);
-		if (mfp->mntent_file)
-			free(mfp->mntent_file);
+		free(mfp->mntent_file);
 		free(mfp);
 	}
 }
diff --git a/mount/realpath.c b/mount/realpath.c
index fdb15b7..dbcd42a 100644
--- a/mount/realpath.c
+++ b/mount/realpath.c
@@ -146,8 +146,7 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) {
 			newbuf = xmalloc(m + n + 1);
 			memcpy(newbuf, link_path, n);
 			memcpy(newbuf + n, path, m + 1);
-			if (buf)
-				free(buf);
+			free(buf);
 			path = buf = newbuf;
 #endif
 		}
@@ -159,12 +158,10 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) {
 	/* Make sure it's null terminated. */
 	*npath = '\0';

-	if (buf)
-		free(buf);
+	free(buf);
 	return resolved_path;

  err:
-	if (buf)
-		free(buf);
+	free(buf);
 	return NULL;
 }
diff --git a/partx/crc32.c b/partx/crc32.c
index 42d803d..4120f72 100644
--- a/partx/crc32.c
+++ b/partx/crc32.c
@@ -96,7 +96,7 @@ crc32init_le(void)
 static void
 crc32cleanup_le(void)
 {
-	if (crc32table_le) free(crc32table_le);
+	free(crc32table_le);
 	crc32table_le = NULL;
 }

@@ -198,7 +198,7 @@ crc32init_be(void)
 static void
 crc32cleanup_be(void)
 {
-	if (crc32table_be) free(crc32table_be);
+	free(crc32table_be);
 	crc32table_be = NULL;
 }

diff --git a/partx/gpt.c b/partx/gpt.c
index adcaccd..b146c9e 100644
--- a/partx/gpt.c
+++ b/partx/gpt.c
@@ -585,10 +585,8 @@ read_gpt_pt (int fd, struct slice all, struct slice *sp, int ns)
         int last_used_index=-1;

 	if (!find_valid_gpt (fd, &gpt, &ptes) || !gpt || !ptes) {
-		if (gpt)
-			free (gpt);
-		if (ptes)
-			free (ptes);
+		free (gpt);
+		free (ptes);
 		return 0;
 	}

--
1.5.4.4.482.g5f904
--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux