git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] show_ident_date: fix always-false conditional

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Mar 7, 2014, 01:35 UTC
Message-ID
<1394156124-3953-1-git-send-email-sunshine@sunshineco.com>

1dca155fe3fa (log: handle integer overflow in timestamps, 2014-02-24) assigns the result of strtol() to an 'int' and then checks it against LONG_MIN and LONG_MAX, indicating underflow or overflow, even though 'int' may not be large enough to represent those values.

On Mac, the compiler complains:
    warning: comparison of constant 9223372036854775807 with
      expression of type 'int' is always false
      [-Wtautological-constant-out-of-range-compare]
      if (<<tz == LONG_MAX>> || tz == LONG_MIN)
Similarly for the LONG_MIN case. Fix this.
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---

Alternately, the result of strtol() could be assigned temporarily to a 'long', compared against LONG_MIN and LONG_MAX, and then assigned to the 'int' "tz" variable. I chose the 'errno' approach instead because its dead obvious, even to the most casual reader who hasn't checked the strtol() man page, that it's handling a conversion failure. However, I could go either way.

This patch is atop 'next'.
 pretty.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pretty.c b/pretty.c
index 3b811ed..8903116 100644
--- a/pretty.c
+++ b/pretty.c
@@ -403,10 +403,10 @@ static const char *show_ident_date(const struct ident_split *ident,
 		date = strtoul(ident->date_begin, NULL, 10);
 	if (date_overflows(date))
 		date = 0;
-	else {
-		if (ident->tz_begin && ident->tz_end)
-			tz = strtol(ident->tz_begin, NULL, 10);
-		if (tz == LONG_MAX || tz == LONG_MIN)
+	else if (ident->tz_begin && ident->tz_end) {
+		errno = 0;
+		tz = strtol(ident->tz_begin, NULL, 10);
+		if (errno)
 			tz = 0;
 	}
 	return show_date(date, tz, mode);
-- 
1.8.3.2
Next: Jeff King
Message 1 of 3 in “show_ident_date: fix always-false conditional”
  1. show_ident_date: fix always-false conditionalEric Sunshine, Mar 7, 2014
  2. Jeff KingMar 7, 2014
  3. Eric SunshineMar 7, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.