Skip to content

Commit

Permalink
show_ident_date: fix tz range check
Browse files Browse the repository at this point in the history
Commit 1dca155 (log: handle integer overflow in
timestamps, 2014-02-24) tried to catch integer overflow
coming from strtol() on the timezone field by comparing against
LONG_MIN/LONG_MAX. However, the intermediate "tz" variable
is an "int", which means it can never be LONG_MAX on LP64
systems; we would truncate the output from strtol before the
comparison.

Clang's -Wtautological-constant-out-of-range-compare notices
this and rightly complains.

Let's instead store the result of strtol in a long, and then
compare it against INT_MIN/INT_MAX. This will catch overflow
from strtol, and also overflow when we pass the result as an
int to show_date.

Reported-by: Eric Sunshine <[email protected]>
Signed-off-by: Jeff King <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
peff authored and gitster committed Mar 7, 2014
1 parent 2b15846 commit 3f419d4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ static const char *show_ident_date(const struct ident_split *ident,
enum date_mode mode)
{
unsigned long date = 0;
int tz = 0;
long tz = 0;

if (ident->date_begin && ident->date_end)
date = strtoul(ident->date_begin, NULL, 10);
Expand All @@ -406,7 +406,7 @@ static const char *show_ident_date(const struct ident_split *ident,
else {
if (ident->tz_begin && ident->tz_end)
tz = strtol(ident->tz_begin, NULL, 10);
if (tz == LONG_MAX || tz == LONG_MIN)
if (tz >= INT_MAX || tz <= INT_MIN)
tz = 0;
}
return show_date(date, tz, mode);
Expand Down

0 comments on commit 3f419d4

Please sign in to comment.