From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796394946; b=QIijiSb/qOAL6hL8pYW1WBogx1zoJJaH9VriJZm6f35JUQ3pMm8+9nGqnP3JFBCt9UaX o9EnhNjDgbWHdKZo/vHybb6122ktJ6HdwMggqOD9L5w9tApB9R/c1crU+pEnYoyIHQ9FD Mt2iNYjZosI+b3if9TceogwWe+DyC7P2iTy2AxDpFAQcm89rlG+gDz/cl8SM16tULuih+ fONNrbz+BwVA2y7am0srl4i1tq8vYiV0kG7NazHLlBe7GZuL+Hp1ujCKgEzzOvNjtE/HQ QbR2QhU9+B5aRbAq/JQ9sPtKaixMspOp+tdYg7FVz/jVxvdqWsNV75afwT+n+yr/Ajw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796394946; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To: References:From:Date:Subject:Fcc:Content-Type: Content-Transfer-Encoding:MIME-Version:To:Cc; bh=ubOAh15mJ+kIRndAlIM0TiEwEcj3OGyF5pQk9yftCkw=; b=TzveTmotEFoZTT0w7GxQZzNxDBT5WjsrsfYGvQRfJ/O8qAv32E8N/sjxD8o7QCXyszFV J12rn1T1pj2J/hvNgowq57dlvc7INv5KyvIBgq51kRHKa2BABkAUYoPN90m5PBEKmKsTk uzkh1Abpf5td+iK5brlaf1mdaLKSLOQBeGI5gMSwccep2VgIRe2VXwQs0wJXK31/M5aqW g0i+Aa6UJKPqV2W/vNOvuDXWKEromjo7ys/s5ycjFEAH1C4RzVHIA6iV8EP3xGonNIF/o 59Cf2OfYPe29gdyAGshj55GZdjHg3CScJMyODQ3qJA82+AjdC3MDaH+ghYealHkMY5g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VKcyLT04; arc=none smtp.remote-ip=209.85.128.53 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4217606A32C for ; Fri, 4 Dec 2026 14:35:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A4217606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VKcyLT04 Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4bb666304fdso18144815e9.0 for ; Fri, 04 Dec 2026 06:35:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796394944; x=1796999744; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=ubOAh15mJ+kIRndAlIM0TiEwEcj3OGyF5pQk9yftCkw=; b=VKcyLT04bOK1K7ATf9Y8n91kBK6qE8QVUGQWtHBnVCkj33YQ4x5ttC8lyHl7/l7JYC 2uwlvRCfLtxeVBh0pxctfLJO6cYci36BnhAjhsUVbJ6Rk+W5QusHAFRM000LLWj/NjuC NawltHCHn/2iFpM/+0YGAJWashy35euR3fXUQkLJMjsX11Km7uMjuJWViQy0cxEKgEbi mpYAoo/gtuWune4c09kOwVreiNJgpcgoz/HDdzz4cJlFqNi4TxF1IOXoFd2KLynA/Z5D Sm97p6KvmzWj7/dn0RyMKRW6xXkN/QG3HUuqSKvMpOb2/pYrcQcztSbq2njNBVufnpTr Z3wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796394944; x=1796999744; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ubOAh15mJ+kIRndAlIM0TiEwEcj3OGyF5pQk9yftCkw=; b=HykGqMmvAnkWOdVwCijqXsFpKDP2dof9bgRnv6RWI4bOo5RAqBQ3OluE+FJZLI9PV1 LIj85Sw8AkwHgnWmNYiaEiBRda6HUYK37Pxed4ebCF5iLp+dNfg/zWdt3Sh99eOFQHRr UuG+mvx92sMBAhY+f9xEPGSlBwuQF6qKjsIN4g3uYfv2+EcfMPb+vjdDcm4yNpAbvoRU bBPkVJ9TrXQI5UfiI4xc8AUoiBTx+gm0hV3LxYlsgXyPLmVckJF+C9SGt+4IkrIwT4ET F77/G2THPAXYGGQBW3+nnyTqrRbNaMbuErwJmu4l63f1VtMbXhsA2uy333nKHspjj6rx qWJA== X-Gm-Message-State: APuU4Z/wOYzl1JhQcdd/TrCWXiFAFjguwqxLqpz2mYPuP5b5RqLW8mV1 82Gbu3tDLD9uN5N9fyffCyUSKq+7C3yI2GL5q/1J8UYMTjB/DShIkBVci3EisA== X-Gm-Gg: AZFdTZfYr+q+0HqHIirkoDF7lFlVeHCUUywHgS3FoRpFYvvlvqeGkt7xhiZbye2lHIB dakDl9kJWUnvNyO8VmsDlKDw9Il8K5wYfsOQk6dOZFDiDx4IFH638sebsNyUUT6C9wGxlpdh0hY e9kN0wHql3zspjw+SsnKkLAbvALnjY3pfUB09gX5oSUM+TWo14ahfBImXXe0362qpTjSS9/SHY+ M24e1IJOFigXS6bvjXOiBPpmn/zHhjHpH2vKESUXVEOMFjMdd1mN+tymd8qHwKZ/45yy3vUO7Al b2tnsEq2NH+z0Sk7e1UCsdDV2GzWRMyl/mFYJcrmFyRWC7259oG+Or06OwKcz5R+fOowzGziPfn TnqdD6KckWp2tE+VghlrW4z/acXuS4Cvmsz5GUwow0RgKUHVg28kV1uPnfTmf9/187yWSpW9ik9 hBwDnwxls+/L6IMT2YrJYGwdh522mmLry0prFy/dL3MGke7srv+Cbk68VVL1UGjY1bIoZFfL3c7 bR4LgIjBTzALUklIf3tnwndU/2nmWMPhFtnEGi5CSFDsudHa/ps307ZYYECBZ3kwbW8TobS4VCd MzTMjqkEBddTg3EvhEZ0ZX6YmyajucaZCfUPmalo5MrGZtKXSFesTDvl1CK+maqLg9c1bLRMNmE = X-Received: by 2002:a05:600c:3ac4:b0:4bb:e68d:786a with SMTP id 5b1f17b1804b1-4bbeb8bb2ebmr46338255e9.14.1796394943845; Fri, 04 Dec 2026 06:35:43 -0800 (PST) Received: from [127.0.0.1] (ec2-3-41-210-122.compute-1.amazonaws.com. [3.41.210.122]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbea0b5e3csm57120885e9.0.2026.12.04.06.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 06:35:43 -0800 (PST) Message-Id: In-Reply-To: References: From: "Johannes Schindelin via GitGitGadget" Date: Fri, 04 Dec 2026 14:35:37 +0000 Subject: [PATCH v4 0/5] Address overflows pointed out by Coverity Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 To: git@vger.kernel.org Cc: Phillip Wood , Patrick Steinhardt , Harald Nordgren , Simon Richter , Aniket Gupta , Johannes Schindelin This patch series is part of my effort to bring Git for Windows' Coverity reports down to zero; Previous patch series have been merged in 347fe8a91b5 (Merge branch 'js/coverity-fixes', 2026-07-16), 0b75ef5168c (Merge branch 'js/coverity-fixes-null-safety', 2026-07-19), fcc8c931b53 (Merge branch 'js/coverity-unchecked-returns-fix', 2026-08-24) and 8bc9c7fcfd8 (Merge branch 'js/coverity-fixes', 2026-10-03). Changes since v3: * Fixed typo in 4/5's commit message. * Removed unnecessary conditional in read_in_full_1(). * The should_prune_worktree() function now also uses read_in_full_or_error() (and is simpler for it). * The num_t variable was renamed (because the _t suffix suggested a type, which is misleading). * The weekday logic is now also guarded against over-/underflows. Changes since v2: * Fixed a grammar error and an overstatement in the "Changes since v1" section :-) * Accepted Junio's rewritten explanation in 2/5's commit message. * Clarified the design decision to abort on programing errors in 4/5's commit message. * Adjusted a few straggler ssize_t variables that can now safely be int or even be dropped. Changes since v1: * Five callers of read_in_full() and all callers of write_in_full() are now no longer restricted by the range of ssize_t. * The signed_mult_overflows() and mult_overflows() macros have been dropped. * Since the approxidate() function only deals with non-negative values, the complicated overflow check could have been simplified. * Rebased to master, to avoid merge conflicts. Johannes Schindelin (5): archive-tar: guard get_path_prefix against zero-length input http: use `size_t` for byte position in fwrite_sha1_file wrapper: avoid byte-count overflow in write_in_full() wrapper: add read_in_full_or_error() date: avoid overflow in approxidate relative-time multiplication Makefile | 1 + archive-tar.c | 2 + builtin/blame.c | 2 +- combine-diff.c | 6 +- config.c | 14 ++--- date.c | 44 ++++++++++++--- dir.c | 2 +- entry.c | 2 +- grep.c | 2 +- http.c | 2 +- imap-send.c | 2 +- notes-merge.c | 13 +---- odb/streaming.c | 6 +- parallel-checkout.c | 2 +- refs/packed-backend.c | 4 +- strbuf.c | 10 +++- t/meson.build | 1 + t/t0006-date.sh | 13 +++++ t/unit-tests/u-reftable-stack.c | 2 +- t/unit-tests/u-wrapper.c | 99 +++++++++++++++++++++++++++++++++ worktree.c | 10 +--- wrapper.c | 55 +++++++++++++----- wrapper.h | 14 ++++- 23 files changed, 234 insertions(+), 74 deletions(-) create mode 100644 t/unit-tests/u-wrapper.c base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2253%2Fdscho%2Fcoverity-fixes-overflow-v4 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2253/dscho/coverity-fixes-overflow-v4 Pull-Request: https://github.com/gitgitgadget/git/pull/2253 Range-diff vs v3: 1: bb8225547f = 1: bb8225547f archive-tar: guard get_path_prefix against zero-length input 2: aa2d3961b2 = 2: aa2d3961b2 http: use `size_t` for byte position in fwrite_sha1_file 3: f38c7e7133 = 3: f38c7e7133 wrapper: avoid byte-count overflow in write_in_full() 4: d45982f1cc ! 4: ef314d773f wrapper: add read_in_full_or_error() @@ Commit message For those callers that do not require the return value to report the number of read bytes, add a new function, `read_in_full_or_error()`, - that returns a Boolean to indicate success or faiure. + that returns a Boolean to indicate success or failure. Callers that do need to know that byte count can still call `read_in_full()`, but will now abort to signal a programming error when the requested byte count exceeds what `ssize_t` can represent. + Note that one of the callers that _did_ look at the byte count is + `should_prune_worktree()`, but since all it did was to write an error + message about a short read, I also converted it to call + `read_in_full_or_error()`. The result is a bit simpler. + Pointed out by Coverity. Helped-by: Phillip Wood + Helped-by: Junio C Hamano Assisted-by: GPT-6.1 and GPT-6 Luna Signed-off-by: Johannes Schindelin @@ t/unit-tests/u-wrapper.c: void test_wrapper__write_in_full_error(void) + cl_assert_equal_i(errno, EBADF); +} + ## worktree.c ## +@@ worktree.c: int should_prune_worktree(struct repository *repo, + int rc = 0; + int fd; + size_t len; +- ssize_t read_result; ++ int read_result; + + *wtpath = NULL; + +@@ worktree.c: int should_prune_worktree(struct repository *repo, + len = xsize_t(st.st_size); + path = xmallocz(len); + +- read_result = read_in_full(fd, path, len); ++ read_result = read_in_full_or_error(fd, path, len); + close(fd); + if (read_result < 0) { + strbuf_addf(reason, _("unable to read gitdir file (%s)"), + strerror(errno)); + rc = 1; + goto done; +- } else if (read_result != len) { +- strbuf_addf(reason, +- _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), +- (uintmax_t)len, (uintmax_t)read_result); +- rc = 1; +- goto done; + } + while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) + len--; + ## wrapper.c ## @@ wrapper.c: ssize_t xpread(int fd, void *buf, size_t len, off_t offset) } @@ wrapper.c: ssize_t xpread(int fd, void *buf, size_t len, off_t offset) } - return total; -+ if (out) -+ *out = count - remaining; ++ *out = count - remaining; + return true; +} + 5: 23ec6cf8f0 ! 5: cb693161ca date: avoid overflow in approxidate relative-time multiplication @@ Commit message Pointed out by Coverity. + Even though Coverity did not point it out (but Junio Hamano did), the + weekday calculation suffers equally unguarded arithmetic. Let's guard + against overflows/underflows in that logic, too. + Helped-by: Phillip Wood + Helped-by: Junio C Hamano Assisted-by: Claude Opus 4.6 and GPT-6.1 Signed-off-by: Johannes Schindelin @@ date.c: static const char *approxidate_alpha(const char *date, struct tm *tm, st size_t len = strlen(tl->type); if (match_string(date, tl->type) >= len-1) { - update_tm(tm, now, tl->length * *num); -+ uintmax_t length = tl->length, num_t = *num, seconds; ++ uintmax_t length = tl->length, n = *num, seconds; + -+ if (unsigned_mult_overflows(length, num_t)) ++ if (unsigned_mult_overflows(length, n)) + return NULL; -+ seconds = length * num_t; ++ seconds = length * n; + if (date_overflows(seconds)) + return NULL; + update_tm(tm, now, (time_t)seconds); *num = 0; *touched = 1; return end; +@@ date.c: static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { +- int diff, n = *num -1; ++ int diff; ++ uintmax_t n = *num -1; + *num = 0; + + diff = tm->tm_wday - i; + if (diff <= 0) + n++; +- diff += 7*n; + +- update_tm(tm, now, diff * 24 * 60 * 60); ++ if (unsigned_mult_overflows(7, n)) ++ return NULL; ++ n *= 7; ++ ++ if (n > INT_MAX - diff) ++ return NULL; ++ diff += n; ++ ++ if (diff > INT_MAX / (24 * 60 * 60) || ++ diff < INT_MIN / (24 * 60 * 60)) ++ return NULL; ++ ++ update_tm(tm, now, (time_t)(diff * 24 * 60 * 60)); + *touched = 1; + return end; + } @@ date.c: static const char *approxidate_digit(const char *date, struct tm *tm, int *num, } -- gitgitgadget