From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796483218; b=TrW7zJqJr8TWCPWR+PunbbPuDHLs0LGcAJqdpOGFEP87b7pqZ9ZH+sYUJc+e4kgPeHkF vdcgs7v6L8CT6VqmyfuErPM9qiltcdJ75kd0Lm315eZcE+6u97yBPDsBSe0DxlM/kDc9j Ps/sfiT0yk0WGN6GFIlLtpZ9zAHXtGMaKENvcnG8uaq79MT2YHYyBfUbbZNhZKTFFpSwv EfBPY6N96a5wFfICUeXnFOCnfzhQgx/FqnGgpvLgCpKYRQ6oZgw3RxiVEHwSawsPQXW0N QUaXy4/92tQ+9SfMbqtsWOeg22R0NtBfSQIVoBq66jjrDbMIxWqtNEz5ZsydatSRsRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796483218; 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=Kgh0QVx1EehCBRGdmcBRq5mfO54CQlIjsnBne6kWHvo=; b=BdJwFaMOVNmmqaDkqwEyk2/5l85baD3J3iQDrgq2EE7MUfD5r5I2TQ7qDMyXh9vcYaoB kfDjNF//LkJRraSYh01nD0NZ1EcLKeazQj49wgezpOGF3dmGxNmiXlYNOUzLEkYZe0HDN sot3rwu4Qa+KzOkyFA52ZHcCHtGFIjxsRemvD/kKiRqHLxd58790aquDbVo+WLYvkPLFm 2VWlcrGqmpg2r1tyClkN3QRIpLu5Abn/H1wuk0KMZXDuCT/UTypHpmFw9I13ysb6GKfRB H+ixhOpiDxSRl8FecSSqvGTKoMfrae6fZWxJ8LzSiVg0CzX+GCwwjdMQuCHgAuaCVsQ== 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=TbNei6QY; arc=none smtp.remote-ip=209.85.210.52 Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (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 92791606A322 for ; Sat, 5 Dec 2026 15:06:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 92791606A322 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=TbNei6QY Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-8479f46ecd8so890720a34.0 for ; Sat, 05 Dec 2026 07:06:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796483216; x=1797088016; 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=Kgh0QVx1EehCBRGdmcBRq5mfO54CQlIjsnBne6kWHvo=; b=TbNei6QYrUtMcPRkb4dxQScezkpjKGbQPNLXO9wcoQwPXt3cGJGussjXyYNBjsYqP5 E1D/cnCLDOyn6Kf2SEZ7Z+BikQBlU6gBm1wKyutAv0J5HWM3S/5dKbFSmblard5fDcZ6 VWmByQ93CanqvJJqLpnWuYZPoyaz+bgJY8zCSkoC4hgYjp3xlDSzE9aFx5y+UKN7Gh+J mX1wAucU4K2EPKOnluGyHh8pFKjfut06WZcG+6lW/FlGQRdRIQtEOFXCyHkTugw8MkDX Bsg4OxY/AcpPpNCF9V3Nhw+Lkh9q7i/ZpBsPNhrIq96Lu+XNE1gKYvbxHsmpBFnxy6Ic ocow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796483216; x=1797088016; 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=Kgh0QVx1EehCBRGdmcBRq5mfO54CQlIjsnBne6kWHvo=; b=GBWjDH2LffY9oCeDgTLFwmM4LLChIUDKOTYxP5qT0oFikpc9kd9CaBY7VQG042KNE7 yTfacxW2jtMFuzDWVtcsVj+2soqf7R5LInaBThmmcWRMlbLPzs7DKoHAadZN6FwBs+Gn 8D99No5nzFNRIuFFXFN0dkHEi/cPYgwfyxXN8YH+2+FLY/oP8VuwMBDwnAqqWsVhUmiI WaqgqgPXRZTi7duz5kcFGOVoUJgep3MVYGXiEVY+xdqmANWNEb5EkB9dxlrsTXQMpB4w JKx5WIeM+rzWks9p+Fm71v4qQYR1Zw4UPht+rjHRNgdE7MncB5F9t9A8hpdwka/6v1r7 flQw== X-Gm-Message-State: APuU4Z8p8LWcomt5KmIX6euWt3dyTO3qMR29BmbS6ta89dDhueOH7UQ+ 9bffL/jWLHTfK/7NEUVrjbT2QHbrloI053IgHvnIOaiOL+JqJq9Py4fYXmYuqBWW X-Gm-Gg: AZFdTZch2/oxbL8Ueix8QwJCUGz+8kgQt4SKKwdF1R+sypr52PxvjwoDMCKsP7rVJLC FspC1Et2659iRDohyr8xyhiBYEXXGK1K83mBP+hYV67BCzPRe9+aNPM7Ap4xHG8CGs1cl1GCdtL fKPrQrxhrzsgGVpMhoaUMXrYksuMiuwFigeb5f5NT4kjL5GUXq2BmVvP90D67ZR5U4/UNhiP9iI jEXxcYptUhB5Zqh4Obx/TJll92D6FoNd/2v4sEQynuOvIrJGXert2c6RIUVljRqpBIAxjNjTnkm qv4fkEvRcv/FTgr0CH/sGTVep3/3eeuPy0rvLp1/dgxiYLWtpFkeV26cXjbZJ6zoVG+2tKYxrlp SnUDGlrqWXzvFSTvS9aJmSCVzJF0iAh+myOzAdPPUab6U6tLhbBkQIpJiiuVTjW6d5rAzxaicxo 8+rCF4cGMRaQ9lndBbgt38uC4mWtuvpYYY2zt9Hx5QwsACyQP5uGihZ0x4olMBLtMbNoAPonZsd a0zXO1LjSD6ssnpaCgm+RrIrpNS6a9r14DV/RxR7QK3mg5Lo3eitwrU4/v6UW1OW4EO24WMKjfD OrzlxenHcroLMQzhgXoBzBIxAZAX9UW2WwQ= X-Received: by 2002:a05:6830:440e:b0:862:c9ab:687a with SMTP id 46e09a7af769-863f884d94emr5815980a34.0.1796483216284; Sat, 05 Dec 2026 07:06:56 -0800 (PST) Received: from [127.0.0.1] ([52.165.103.180]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f81e7629sm5571086a34.6.2026.12.05.07.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 07:06:54 -0800 (PST) Message-Id: In-Reply-To: References: From: "Johannes Schindelin via GitGitGadget" Date: Sat, 05 Dec 2026 15:06:49 +0000 Subject: [PATCH v5 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 v4: * Fixed the commit message to no longer claim a Boolean return value of read_in_full_or_error(). * Fixed the incomplete weekday arithmetic guard that I introduced in v4, simplifying it in the process. 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 | 33 ++++++++--- 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, 225 insertions(+), 72 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-v5 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2253/dscho/coverity-fixes-overflow-v5 Pull-Request: https://github.com/gitgitgadget/git/pull/2253 Range-diff vs v4: 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: ef314d773f ! 4: 8d85d97fe1 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 failure. + that returns a negative value to indicate failure and 0 for success. 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 @@ wrapper.c: ssize_t xpread(int fd, void *buf, size_t len, off_t offset) -ssize_t read_in_full(int fd, void *buf, size_t count) +/* + * Read until count bytes have been read or EOF. On success, store the number -+ * read in out if non-NULL. Return false on an I/O error, with errno set. ++ * read in out. Return false on an I/O error, with errno set. + */ +static bool read_in_full_1(int fd, void *buf, size_t count, size_t *out) { 5: cb693161ca ! 5: 9025fec315 date: avoid overflow in approxidate relative-time multiplication @@ Commit message 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. + against overflows in that logic, too. Helped-by: Phillip Wood Helped-by: Junio C Hamano @@ date.c: static const char *approxidate_alpha(const char *date, struct tm *tm, st 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; ++ const int max_days = INT_MAX / (24 * 60 * 60); ++ /* `*num` is guaranteed to be positive here */ + int diff, 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)) ++ if (n > (max_days - diff) / 7) + return NULL; -+ + diff += 7*n; + +- update_tm(tm, now, diff * 24 * 60 * 60); + update_tm(tm, now, (time_t)(diff * 24 * 60 * 60)); *touched = 1; return end; -- gitgitgadget