Skip to content

Commit 153fde5

Browse files
committed
delta-apply: fix sign extension
We compute offsets by executing `off |= (*delta++ << 24)` for multiple constants, where `off` is of type `size_t` and `delta` is of type `unsigned char`. The usual arithmetic conversions (see ISO C89 §3.2.1.5 "Usual arithmetic conversions") kick in here, causing us to promote both operands to `int` and then extending the result to an `unsigned long` when OR'ing it with `off`. The integer promotion to `int` may result in wrong size calculations for big values. Fix the issue by making the constants `unsigned long`, causing both operands to be promoted to `unsigned long`.
1 parent 7f40771 commit 153fde5

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/delta-apply.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ int git__delta_apply(
9090
size_t off = 0, len = 0;
9191

9292
if (cmd & 0x01) off = *delta++;
93-
if (cmd & 0x02) off |= *delta++ << 8;
94-
if (cmd & 0x04) off |= *delta++ << 16;
95-
if (cmd & 0x08) off |= *delta++ << 24;
93+
if (cmd & 0x02) off |= *delta++ << 8UL;
94+
if (cmd & 0x04) off |= *delta++ << 16UL;
95+
if (cmd & 0x08) off |= *delta++ << 24UL;
9696

9797
if (cmd & 0x10) len = *delta++;
98-
if (cmd & 0x20) len |= *delta++ << 8;
99-
if (cmd & 0x40) len |= *delta++ << 16;
98+
if (cmd & 0x20) len |= *delta++ << 8UL;
99+
if (cmd & 0x40) len |= *delta++ << 16UL;
100100
if (!len) len = 0x10000;
101101

102102
if (base_len < off + len || res_sz < len)

0 commit comments

Comments
 (0)