On Sun, Jun 03, 2012 at 01:21:47PM +0100, Thomas Adam wrote:
> The C standard dictates that calling free(NULL) is a no-op.  So we can
> safely assume this to be OK.

I'm a disaster.  I forgot to include some files.  Oops.  Here's V2,
attached.

-- Thomas Adam

-- 
"Deep in my heart I wish I was wrong.  But deep in my heart I know I am
not." -- Morrissey ("Girl Least Likely To" -- off of Viva Hate.)
>From 68de240da5a8f2fa1046a7ef2325c57754287a1a Mon Sep 17 00:00:00 2001
From: Thomas Adam <tho...@xteddy.org>
Date: Sun, 3 Jun 2012 13:04:00 +0100
Subject: [PATCH v2 1/1] Don't check for NULL before xfree()
To: tmux-users@lists.sourceforge.net

The C standard dictates that calling free(NULL) is a no-op.  So we can
safely assume this to be OK.

This patch removes the fatalx() call in xmalloc.c:xfree() and all callers of
xfree() where they were previously checking for NULL have been removed.
---
 trunk/arguments.c          |   12 ++++--------
 trunk/array.h              |    3 +--
 trunk/cmd-command-prompt.c |    9 +++------
 trunk/cmd-confirm-before.c |    3 +--
 trunk/cmd-if-shell.c       |    3 +--
 trunk/cmd-load-buffer.c    |    3 +--
 trunk/cmd-string.c         |    9 +++------
 trunk/cmd.c                |   27 +++++++++------------------
 trunk/environ.c            |    9 +++------
 trunk/grid.c               |   12 ++++--------
 trunk/screen.c             |    6 ++----
 trunk/server-client.c      |   19 ++++++-------------
 trunk/session.c            |    6 ++----
 trunk/status.c             |   15 +++++----------
 trunk/tmux.c               |   15 +++++----------
 trunk/tty-term.c           |    3 +--
 trunk/window-copy.c        |    3 +--
 trunk/window.c             |   27 +++++++++------------------
 trunk/xmalloc.c            |    2 --
 19 files changed, 61 insertions(+), 125 deletions(-)

diff --git a/trunk/arguments.c b/trunk/arguments.c
index 2710cef..4c94b0c 100644
--- a/trunk/arguments.c
+++ b/trunk/arguments.c
@@ -75,8 +75,7 @@ args_parse(const char *template, int argc, char **argv)
 
 		bit_set(args->flags, opt);
 		if (ptr[1] == ':') {
-			if (args->values[opt] != NULL)
-				xfree(args->values[opt]);
+			xfree(args->values[opt]);
 			args->values[opt] = xstrdup(optarg);
 		}
 	}
@@ -97,10 +96,8 @@ args_free(struct args *args)
 
 	cmd_free_argv(args->argc, args->argv);
 
-	for (i = 0; i < SCHAR_MAX; i++) {
-		if (args->values[i] != NULL)
-			xfree(args->values[i]);
-	}
+	for (i = 0; i < SCHAR_MAX; i++)
+		xfree(args->values[i]);
 
 	xfree(args->flags);
 	xfree(args);
@@ -182,8 +179,7 @@ args_has(struct args *args, u_char ch)
 void
 args_set(struct args *args, u_char ch, const char *value)
 {
-	if (args->values[ch] != NULL)
-		xfree(args->values[ch]);
+	xfree(args->values[ch]);
 	if (value != NULL)
 		args->values[ch] = xstrdup(value);
 	else
diff --git a/trunk/array.h b/trunk/array.h
index 50f0da8..a9d4ad6 100644
--- a/trunk/array.h
+++ b/trunk/array.h
@@ -109,8 +109,7 @@
 } while (0)
 
 #define ARRAY_FREE(a) do {						\
-	if ((a)->list != NULL)						\
-		xfree((a)->list);					\
+	xfree((a)->list);						\
 	ARRAY_INIT(a);							\
 } while (0)
 #define ARRAY_FREEALL(a) do {						\
diff --git a/trunk/cmd-command-prompt.c b/trunk/cmd-command-prompt.c
index 55e69b3..747130f 100644
--- a/trunk/cmd-command-prompt.c
+++ b/trunk/cmd-command-prompt.c
@@ -205,11 +205,8 @@ cmd_command_prompt_free(void *data)
 {
 	struct cmd_command_prompt_cdata	*cdata = data;
 
-	if (cdata->inputs != NULL)
-		xfree(cdata->inputs);
-	if (cdata->prompts != NULL)
-		xfree(cdata->prompts);
-	if (cdata->template != NULL)
-		xfree(cdata->template);
+	xfree(cdata->inputs);
+	xfree(cdata->prompts);
+	xfree(cdata->template);
 	xfree(cdata);
 }
diff --git a/trunk/cmd-confirm-before.c b/trunk/cmd-confirm-before.c
index 159b938..5436053 100644
--- a/trunk/cmd-confirm-before.c
+++ b/trunk/cmd-confirm-before.c
@@ -144,7 +144,6 @@ cmd_confirm_before_free(void *data)
 {
 	struct cmd_confirm_before_data	*cdata = data;
 
-	if (cdata->cmd != NULL)
-		xfree(cdata->cmd);
+	xfree(cdata->cmd);
 	xfree(cdata);
 }
diff --git a/trunk/cmd-if-shell.c b/trunk/cmd-if-shell.c
index 220fc33..44a8ee5 100644
--- a/trunk/cmd-if-shell.c
+++ b/trunk/cmd-if-shell.c
@@ -115,8 +115,7 @@ cmd_if_shell_free(void *data)
 	if (ctx->curclient != NULL)
 		ctx->curclient->references--;
 
-	if (cdata->cmd_else != NULL)
-		xfree(cdata->cmd_else);
+	xfree(cdata->cmd_else);
 	xfree(cdata->cmd_if);
 	xfree(cdata);
 }
diff --git a/trunk/cmd-load-buffer.c b/trunk/cmd-load-buffer.c
index 0df0cb2..34339f3 100644
--- a/trunk/cmd-load-buffer.c
+++ b/trunk/cmd-load-buffer.c
@@ -134,8 +134,7 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
 	return (0);
 
 error:
-	if (pdata != NULL)
-		xfree(pdata);
+	xfree(pdata);
 	if (f != NULL)
 		fclose(f);
 	return (-1);
diff --git a/trunk/cmd-string.c b/trunk/cmd-string.c
index 0dd2a77..64c2283 100644
--- a/trunk/cmd-string.c
+++ b/trunk/cmd-string.c
@@ -170,8 +170,7 @@ error:
 	xasprintf(cause, "invalid or unknown command: %s", s);
 
 out:
-	if (buf != NULL)
-		xfree(buf);
+	xfree(buf);
 
 	if (argv != NULL) {
 		for (i = 0; i < argc; i++)
@@ -239,8 +238,7 @@ cmd_string_string(const char *s, size_t *p, char endch, int esc)
 	return (buf);
 
 error:
-	if (buf != NULL)
-		xfree(buf);
+	xfree(buf);
 	return (NULL);
 }
 
@@ -309,8 +307,7 @@ cmd_string_variable(const char *s, size_t *p)
 	return (xstrdup(envent->value));
 
 error:
-	if (buf != NULL)
-		xfree(buf);
+	xfree(buf);
 	return (NULL);
 }
 
diff --git a/trunk/cmd.c b/trunk/cmd.c
index 7aefeff..5941692 100644
--- a/trunk/cmd.c
+++ b/trunk/cmd.c
@@ -196,10 +196,8 @@ cmd_free_argv(int argc, char **argv)
 
 	if (argc == 0)
 		return;
-	for (i = 0; i < argc; i++) {
-		if (argv[i] != NULL)
-			xfree(argv[i]);
-	}
+	for (i = 0; i < argc; i++)
+		xfree(argv[i]);
 	xfree(argv);
 }
 
@@ -289,8 +287,7 @@ cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
 void
 cmd_free(struct cmd *cmd)
 {
-	if (cmd->args != NULL)
-		args_free(cmd->args);
+	args_free(cmd->args);
 	xfree(cmd);
 }
 
@@ -897,8 +894,7 @@ no_session:
 		ctx->error(ctx, "multiple sessions: %s", arg);
 	else
 		ctx->error(ctx, "session not found: %s", arg);
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (NULL);
 
 not_found:
@@ -906,8 +902,7 @@ not_found:
 		ctx->error(ctx, "multiple windows: %s", arg);
 	else
 		ctx->error(ctx, "window not found: %s", arg);
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (NULL);
 }
 
@@ -998,8 +993,7 @@ cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
 	} else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
 		goto invalid_index;
 
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (idx);
 
 no_colon:
@@ -1038,8 +1032,7 @@ no_session:
 		ctx->error(ctx, "multiple sessions: %s", arg);
 	else
 		ctx->error(ctx, "session not found: %s", arg);
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (-2);
 
 invalid_index:
@@ -1047,8 +1040,7 @@ invalid_index:
 		goto not_found;
 	ctx->error(ctx, "invalid index: %s", arg);
 
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (-2);
 
 not_found:
@@ -1056,8 +1048,7 @@ not_found:
 		ctx->error(ctx, "multiple windows: %s", arg);
 	else
 		ctx->error(ctx, "window not found: %s", arg);
-	if (sessptr != NULL)
-		xfree(sessptr);
+	xfree(sessptr);
 	return (-2);
 }
 
diff --git a/trunk/environ.c b/trunk/environ.c
index 584b8de..3c88de9 100644
--- a/trunk/environ.c
+++ b/trunk/environ.c
@@ -52,8 +52,7 @@ environ_free(struct environ *env)
 		envent = RB_ROOT(env);
 		RB_REMOVE(environ, env, envent);
 		xfree(envent->name);
-		if (envent->value != NULL)
-			xfree(envent->value);
+		xfree(envent->value);
 		xfree(envent);
 	}
 }
@@ -85,8 +84,7 @@ environ_set(struct environ *env, const char *name, const char *value)
 	struct environ_entry	*envent;
 
 	if ((envent = environ_find(env, name)) != NULL) {
-		if (envent->value != NULL)
-			xfree(envent->value);
+		xfree(envent->value);
 		if (value != NULL)
 			envent->value = xstrdup(value);
 		else
@@ -130,8 +128,7 @@ environ_unset(struct environ *env, const char *name)
 		return;
 	RB_REMOVE(environ, env, envent);
 	xfree(envent->name);
-	if (envent->value != NULL)
-		xfree(envent->value);
+	xfree(envent->value);
 	xfree(envent);
 }
 
diff --git a/trunk/grid.c b/trunk/grid.c
index e9922d9..0c96028 100644
--- a/trunk/grid.c
+++ b/trunk/grid.c
@@ -98,10 +98,8 @@ grid_destroy(struct grid *gd)
 
 	for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
 		gl = &gd->linedata[yy];
-		if (gl->celldata != NULL)
-			xfree(gl->celldata);
-		if (gl->utf8data != NULL)
-			xfree(gl->utf8data);
+		xfree(gl->celldata);
+		xfree(gl->utf8data);
 	}
 
 	xfree(gd->linedata);
@@ -373,10 +371,8 @@ grid_clear_lines(struct grid *gd, u_int py, u_int ny)
 
 	for (yy = py; yy < py + ny; yy++) {
 		gl = &gd->linedata[yy];
-		if (gl->celldata != NULL)
-			xfree(gl->celldata);
-		if (gl->utf8data != NULL)
-			xfree(gl->utf8data);
+		xfree(gl->celldata);
+		xfree(gl->utf8data);
 		memset(gl, 0, sizeof *gl);
 	}
 }
diff --git a/trunk/screen.c b/trunk/screen.c
index 6f0f3e4..edd6f42 100644
--- a/trunk/screen.c
+++ b/trunk/screen.c
@@ -71,8 +71,7 @@ screen_reinit(struct screen *s)
 void
 screen_free(struct screen *s)
 {
-	if (s->tabs != NULL)
-		xfree(s->tabs);
+	xfree(s->tabs);
 	xfree(s->title);
 	xfree(s->ccolour);
 	grid_destroy(s->grid);
@@ -84,8 +83,7 @@ screen_reset_tabs(struct screen *s)
 {
 	u_int	i;
 
-	if (s->tabs != NULL)
-		xfree(s->tabs);
+	xfree(s->tabs);
 
 	if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
 		fatal("bit_alloc failed");
diff --git a/trunk/server-client.c b/trunk/server-client.c
index d1f7239..88e264a 100644
--- a/trunk/server-client.c
+++ b/trunk/server-client.c
@@ -147,16 +147,14 @@ server_client_lost(struct client *c)
 	status_free_jobs(&c->status_old);
 	screen_free(&c->status);
 
-	if (c->title != NULL)
-		xfree(c->title);
+	xfree(c->title);
 
 	evtimer_del(&c->repeat_timer);
 
 	if (event_initialized(&c->identify_timer))
 		evtimer_del(&c->identify_timer);
 
-	if (c->message_string != NULL)
-		xfree(c->message_string);
+	xfree(c->message_string);
 	if (event_initialized (&c->message_timer))
 		evtimer_del(&c->message_timer);
 	for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
@@ -165,13 +163,9 @@ server_client_lost(struct client *c)
 	}
 	ARRAY_FREE(&c->message_log);
 
-	if (c->prompt_string != NULL)
-		xfree(c->prompt_string);
-	if (c->prompt_buffer != NULL)
-		xfree(c->prompt_buffer);
-
-	if (c->cwd != NULL)
-		xfree(c->cwd);
+	xfree(c->prompt_string);
+	xfree(c->prompt_buffer);
+	xfree(c->cwd);
 
 	environ_free(&c->environ);
 
@@ -660,8 +654,7 @@ server_client_set_title(struct client *c)
 
 	title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
 	if (c->title == NULL || strcmp(title, c->title) != 0) {
-		if (c->title != NULL)
-			xfree(c->title);
+		xfree(c->title);
 		c->title = xstrdup(title);
 		tty_set_title(&c->tty, c->title);
 	}
diff --git a/trunk/session.c b/trunk/session.c
index 0d3b8dd..1696d08 100644
--- a/trunk/session.c
+++ b/trunk/session.c
@@ -125,8 +125,7 @@ session_create(const char *name, const char *cmd, const char *cwd,
 		s->name = NULL;
 		do {
 			s->idx = next_session++;
-			if (s->name != NULL)
-				xfree (s->name);
+			xfree (s->name);
 			xasprintf(&s->name, "%u", s->idx);
 		} while (RB_FIND(sessions, &sessions, s) != NULL);
 	}
@@ -156,8 +155,7 @@ session_destroy(struct session *s)
 	RB_REMOVE(sessions, &sessions, s);
 	notify_session_closed(s);
 
-	if (s->tio != NULL)
-		xfree(s->tio);
+	xfree(s->tio);
 
 	session_group_remove(s);
 	environ_free(&s->environ);
diff --git a/trunk/status.c b/trunk/status.c
index 2a419bb..2cba29b 100644
--- a/trunk/status.c
+++ b/trunk/status.c
@@ -222,8 +222,7 @@ status_redraw(struct client *c)
 	/* Calculate the total size needed for the window list. */
 	wlstart = wloffset = wlwidth = 0;
 	RB_FOREACH(wl, winlinks, &s->windows) {
-		if (wl->status_text != NULL)
-			xfree(wl->status_text);
+		xfree(wl->status_text);
 		memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
 		wl->status_text = status_print(c, wl, t, &wl->status_cell);
 		wl->status_width =
@@ -372,10 +371,8 @@ draw:
 	screen_write_stop(&ctx);
 
 out:
-	if (left != NULL)
-		xfree(left);
-	if (right != NULL)
-		xfree(right);
+	xfree(left);
+	xfree(right);
 
 	if (grid_compare(c->status.grid, old_status.grid) == 0) {
 		screen_free(&old_status);
@@ -491,8 +488,7 @@ do_replace:
 	}
 
 out:
-	if (freeptr != NULL)
-		xfree(freeptr);
+	xfree(freeptr);
 	return;
 
 skip_to:
@@ -615,8 +611,7 @@ status_free_jobs(struct status_out_tree *sotree)
 		so_next = RB_NEXT(status_out_tree, sotree, so);
 
 		RB_REMOVE(status_out_tree, sotree, so);
-		if (so->out != NULL)
-			xfree(so->out);
+		xfree(so->out);
 		xfree(so->cmd);
 		xfree(so);
 	}
diff --git a/trunk/tmux.c b/trunk/tmux.c
index 3f5e6cd..0ce12d9 100644
--- a/trunk/tmux.c
+++ b/trunk/tmux.c
@@ -255,32 +255,28 @@ main(int argc, char **argv)
 			flags &= ~IDENTIFY_256COLOURS;
 			break;
 		case 'c':
-			if (shell_cmd != NULL)
-				xfree(shell_cmd);
+			xfree(shell_cmd);
 			shell_cmd = xstrdup(optarg);
 			break;
 		case 'V':
 			printf("%s %s\n", __progname, VERSION);
 			exit(0);
 		case 'f':
-			if (cfg_file != NULL)
-				xfree(cfg_file);
+			xfree(cfg_file);
 			cfg_file = xstrdup(optarg);
 			break;
 		case 'l':
 			login_shell = 1;
 			break;
 		case 'L':
-			if (label != NULL)
-				xfree(label);
+			xfree(label);
 			label = xstrdup(optarg);
 			break;
 		case 'q':
 			quiet = 1;
 			break;
 		case 'S':
-			if (path != NULL)
-				xfree(path);
+			xfree(path);
 			path = xstrdup(optarg);
 			break;
 		case 'u':
@@ -387,8 +383,7 @@ main(int argc, char **argv)
 			}
 		}
 	}
-	if (label != NULL)
-		xfree(label);
+	xfree(label);
 	if (realpath(path, socket_path) == NULL)
 		strlcpy(socket_path, path, sizeof socket_path);
 	xfree(path);
diff --git a/trunk/tty-term.c b/trunk/tty-term.c
index 64aced0..b6b4915 100644
--- a/trunk/tty-term.c
+++ b/trunk/tty-term.c
@@ -299,8 +299,7 @@ tty_term_override(struct tty_term *term, const char *overrides)
 				}
 			}
 
-			if (val != NULL)
-				xfree(val);
+			xfree(val);
 		}
 	}
 
diff --git a/trunk/window-copy.c b/trunk/window-copy.c
index 7b2a8ae..858fc15 100644
--- a/trunk/window-copy.c
+++ b/trunk/window-copy.c
@@ -240,8 +240,7 @@ window_copy_free(struct window_pane *wp)
 	if (wp->fd != -1)
 		bufferevent_enable(wp->event, EV_READ|EV_WRITE);
 
-	if (data->searchstr != NULL)
-		xfree(data->searchstr);
+	xfree(data->searchstr);
 	xfree(data->inputstr);
 
 	if (data->backing != &wp->base) {
diff --git a/trunk/window.c b/trunk/window.c
index 8eb11f2..d6360bd 100644
--- a/trunk/window.c
+++ b/trunk/window.c
@@ -176,8 +176,7 @@ winlink_remove(struct winlinks *wwl, struct winlink *wl)
 	struct window	*w = wl->window;
 
 	RB_REMOVE(winlinks, wwl, wl);
-	if (wl->status_text != NULL)
-		xfree(wl->status_text);
+	xfree(wl->status_text);
 	xfree(wl);
 
 	if (w != NULL) {
@@ -356,16 +355,14 @@ window_destroy(struct window *w)
 
 	window_destroy_panes(w);
 
-	if (w->name != NULL)
-		xfree(w->name);
+	xfree(w->name);
 	xfree(w);
 }
 
 void
 window_set_name(struct window *w, const char *new_name)
 {
-	if (w->name != NULL)
-		xfree(w->name);
+	xfree(w->name);
 	w->name = xstrdup(new_name);
 	notify_window_renamed(w);
 }
@@ -668,12 +665,9 @@ window_pane_destroy(struct window_pane *wp)
 
 	RB_REMOVE(window_pane_tree, &all_window_panes, wp);
 
-	if (wp->cwd != NULL)
-		xfree(wp->cwd);
-	if (wp->shell != NULL)
-		xfree(wp->shell);
-	if (wp->cmd != NULL)
-		xfree(wp->cmd);
+	xfree(wp->cwd);
+	xfree(wp->shell);
+	xfree(wp->cmd);
 	xfree(wp);
 }
 
@@ -691,18 +685,15 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
 		close(wp->fd);
 	}
 	if (cmd != NULL) {
-		if (wp->cmd != NULL)
-			xfree(wp->cmd);
+		xfree(wp->cmd);
 		wp->cmd = xstrdup(cmd);
 	}
 	if (shell != NULL) {
-		if (wp->shell != NULL)
-			xfree(wp->shell);
+		xfree(wp->shell);
 		wp->shell = xstrdup(shell);
 	}
 	if (cwd != NULL) {
-		if (wp->cwd != NULL)
-			xfree(wp->cwd);
+		xfree(wp->cwd);
 		wp->cwd = xstrdup(cwd);
 	}
 
diff --git a/trunk/xmalloc.c b/trunk/xmalloc.c
index 74262ed..7416088 100644
--- a/trunk/xmalloc.c
+++ b/trunk/xmalloc.c
@@ -85,8 +85,6 @@ xrealloc(void *oldptr, size_t nmemb, size_t size)
 void
 xfree(void *ptr)
 {
-	if (ptr == NULL)
-		fatalx("null pointer");
 	free(ptr);
 }
 
-- 
1.7.10

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to