git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 1/4] wt-status: fix possible use of uninitialized variable
Date: Thu, 21 Mar 2013 07:05:28 -0400	[thread overview]
Message-ID: <20130321110527.GA18819@sigill.intra.peff.net> (raw)
In-Reply-To: <20130321110338.GA18552@sigill.intra.peff.net>

In wt_status_print_change_data, we accept a change_type flag
that is meant to be either WT_STATUS_UPDATED or
WT_STATUS_CHANGED.  We then switch() on this value to set
the local variable "status" for each case, but do not
provide a fallback "default" label to the switch statement.

As a result, the compiler realizes that "status" might be
unset, and complains with a warning. To silence this
warning, we use the "int status = status" trick.  This is
correct with the current code, as all callers provide one of
the two expected change_type flags. However, it's also a
maintenance trap, as there is nothing to prevent future
callers from passing another flag, nor to document this
assumption.

Instead of using the "x = x" hack, let's handle the default
case in the switch() statement with a die("BUG"). That tells
the compiler and any readers of the code exactly what the
function's input assumptions are.

We could also convert the flag to an enum, which would
provide a compile-time check on the function input. However,
since these flags are part of a larger enum, that would make
the code unnecessarily complex (we would have to make a new
enum with just the two flags, and then convert it to the old
enum for passing to sub-functions).

Signed-off-by: Jeff King <peff@peff.net>
---
 wt-status.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/wt-status.c b/wt-status.c
index ef405d0..7555817 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -264,7 +264,7 @@ static void wt_status_print_change_data(struct wt_status *s,
 {
 	struct wt_status_change_data *d = it->util;
 	const char *c = color(change_type, s);
-	int status = status;
+	int status;
 	char *one_name;
 	char *two_name;
 	const char *one, *two;
@@ -292,6 +292,9 @@ static void wt_status_print_change_data(struct wt_status *s,
 		}
 		status = d->worktree_status;
 		break;
+	default:
+		die("BUG: unhandled change_type %d in wt_status_print_change_data",
+		    change_type);
 	}
 
 	one = quote_path(one_name, -1, &onebuf, s->prefix);
-- 
1.8.2.rc2.8.g2161951

  reply	other threads:[~2013-03-21 11:06 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-21 11:03 [PATCH 0/4] drop some "int x = x" hacks to silence gcc warnings Jeff King
2013-03-21 11:05 ` Jeff King [this message]
2013-03-21 19:49   ` [PATCH 1/4] wt-status: fix possible use of uninitialized variable Jonathan Nieder
2013-03-21 19:55     ` Junio C Hamano
2013-03-21 19:58       ` Jonathan Nieder
2013-03-22 16:15     ` Jeff King
2013-03-21 11:08 ` [PATCH 2/4] fast-import: use pointer-to-pointer to keep list tail Jeff King
2013-03-21 20:43   ` Jonathan Nieder
2013-03-21 11:10 ` [PATCH 3/4] drop some obsolete "x = x" compiler warning hacks Jeff King
2013-03-21 15:16   ` Erik Faye-Lund
2013-03-21 20:47   ` Jonathan Nieder
2013-03-24  7:17     ` Torsten Bögershausen
2013-03-21 11:13 ` [PATCH 4/4] transport: drop "int cmp = cmp" hack Jeff King
2013-03-21 20:59   ` Jonathan Nieder
2013-03-24  4:00   ` Junio C Hamano
2013-03-24  9:32     ` Jeff King
2013-03-24 14:54       ` Torsten Bögershausen
2013-03-25 19:50       ` Junio C Hamano
2013-03-25 21:06         ` Jeff King
2013-03-25 21:55           ` Junio C Hamano
2013-03-21 11:45 ` [PATCH 0/4] drop some "int x = x" hacks to silence gcc warnings Johannes Sixt
2013-03-21 11:55   ` Jeff King
2013-03-21 14:58     ` Junio C Hamano
2013-03-21 15:19       ` Junio C Hamano
2013-03-21 15:44         ` Jeff King
2013-03-21 15:44           ` [PATCH 5/4] fast-import: clarify "inline" logic in file_change_m Jeff King
2013-03-21 15:45           ` [PATCH 6/4] run-command: always set failed_errno in start_command Jeff King
2013-03-21 21:02           ` [PATCH 0/4] drop some "int x = x" hacks to silence gcc warnings Jonathan Nieder
2013-03-22 16:18           ` Jeff King
2013-03-22 16:19             ` [PATCH 7/4] submodule: clarify logic in show_submodule_summary Jeff King
2013-03-22 21:10               ` Junio C Hamano
2013-03-22 16:21             ` [PATCH 8/4] match-trees: drop "x = x" initializations Jeff King
2013-03-22 21:26               ` Junio C Hamano
2013-03-22 21:33                 ` Junio C Hamano
2013-03-22 21:36                   ` Jeff King
2013-03-23 18:57               ` René Scharfe
2013-03-24  4:55                 ` Junio C Hamano
2013-03-24 10:01                   ` Jeff King
2013-03-24 22:46                   ` René Scharfe
2013-03-25 16:10                     ` Junio C Hamano
2013-03-21 13:44   ` [PATCH 0/4] drop some "int x = x" hacks to silence gcc warnings Joachim Schmitz
2013-03-21 13:56     ` Joachim Schmitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130321110527.GA18819@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).