Commit ec6d1a368f4f81cda70f8517eb0dec5028b847b8

Josh Rickmar 2021-03-21T18:49:57

Fix strftime(3) short buffer checks strftime(3) returns 0 if the buffer was too short to write the complete string (including NUL) and will never return more than maxsize-1. ok stsp

diff --git a/got/got.c b/got/got.c
index 9bcd279..5a81fdd 100644
--- a/got/got.c
+++ b/got/got.c
@@ -4570,7 +4570,7 @@ blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
 	if (localtime_r(&committer_time, &tm) == NULL)
 		return got_error_from_errno("localtime_r");
 	if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
-	    &tm) >= sizeof(bline->datebuf)) {
+	    &tm) == 0) {
 		err = got_error(GOT_ERR_NO_SPACE);
 		goto done;
 	}
@@ -10073,7 +10073,7 @@ print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
 	tm = localtime_r(&mtime, &mytm);
 	if (tm == NULL)
 		return NULL;
-	if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
+	if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
 		return got_error(GOT_ERR_NO_SPACE);
 	printf("timestamp: %s\n", datebuf);
 
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index 5251b5a..3b1cd05 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -3821,7 +3821,7 @@ gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
 	if (localtime_r(&committer_time, &tm) == NULL)
 		return got_error_from_errno("localtime_r");
 	if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
-	    &tm) >= sizeof(bline->datebuf)) {
+	    &tm) == 0) {
 		err = got_error(GOT_ERR_NO_SPACE);
 		goto done;
 	}
diff --git a/tog/tog.c b/tog/tog.c
index 66c10af..c426e9d 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1343,8 +1343,7 @@ draw_commit(struct tog_view *view, struct got_commit_object *commit,
 	committer_time = got_object_commit_get_committer_time(commit);
 	if (localtime_r(&committer_time, &tm) == NULL)
 		return got_error_from_errno("localtime_r");
-	if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
-	    >= sizeof(datebuf))
+	if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
 		return got_error(GOT_ERR_NO_SPACE);
 
 	if (avail <= date_display_cols)