Skip to content

Commit

Permalink
wt-status: fix possible use of uninitialized variable
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Mar 21, 2013
1 parent 3aa99df commit b8527d5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion wt-status.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ static void wt_status_print_change_data(struct wt_status *s,
{ {
struct wt_status_change_data *d = it->util; struct wt_status_change_data *d = it->util;
const char *c = color(change_type, s); const char *c = color(change_type, s);
int status = status; int status;
char *one_name; char *one_name;
char *two_name; char *two_name;
const char *one, *two; const char *one, *two;
Expand Down Expand Up @@ -292,6 +292,9 @@ static void wt_status_print_change_data(struct wt_status *s,
} }
status = d->worktree_status; status = d->worktree_status;
break; 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); one = quote_path(one_name, -1, &onebuf, s->prefix);
Expand Down

0 comments on commit b8527d5

Please sign in to comment.