Skip to content

Commit 176f9b1

Browse files
committed
Fix buffer underflow in xdl_build_script
The loop in xdl_build_script used `i1 >= 0 || i2 >= 0`, causing `i1` (or `i2`) to reach 0 and then access `rchg1[i1-1]` (or `rchg2[i2-1]`), which underflows the buffer. This commit adds explicit `i1 > 0` and `i2 > 0` checks around those array accesses to prevent invalid negative indexing. Signed-off-by: Alex Guo <[email protected]>
1 parent 8613c2b commit 176f9b1

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

xdiff/xdiffi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,10 @@ int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
951951
* Trivial. Collects "groups" of changes and creates an edit script.
952952
*/
953953
for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
954-
if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
955-
for (l1 = i1; rchg1[i1 - 1]; i1--);
956-
for (l2 = i2; rchg2[i2 - 1]; i2--);
954+
if ((i1 > 0 && rchg1[i1 - 1]) ||
955+
(i2 > 0 && rchg2[i2 - 1])) {
956+
for (l1 = i1; i1 > 0 && rchg1[i1 - 1]; i1--);
957+
for (l2 = i2; i2 > 0 && rchg2[i2 - 1]; i2--);
957958

958959
if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
959960
xdl_free_script(cscr);

0 commit comments

Comments
 (0)