Skip to content

Commit 42c4d6a

Browse files
committed
fix buffer overflow
- pcu/pcu_coll.c (pcu_merge_gather): fix buffer overflow on the local array when number of peers is odd. I was assuming that with power of 2 peers the local array always has an even size. Now, memcpy will never write past local + size. - added comment explaining it. Signed-off-by: Aiden Woodruff <[email protected]>
1 parent 75fe494 commit 42c4d6a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

pcu/pcu_coll.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,12 @@ void pcu_scan(pcu_mpi_t* mpi, pcu_coll* c, pcu_merge* m, void* data, size_t size
417417

418418
void pcu_merge_gather(int peers, int bit, void *local, void *incoming,
419419
size_t size) {
420-
// bit is equal to the current number of items in local and incoming.
421-
// Since all items incoming are from greater ranks, they got to the right.
422420
size_t block_size = size / peers;
423-
memcpy(local + bit * block_size, incoming, bit * block_size);
421+
// local has `bit` blocks.
422+
// incoming may have `bit` (if peers is a power of 2) or `bit - 1` blocks.
423+
// either way, writing `size - bit * block_size` prevents buffer overrun.
424+
// Also, all incoming blocks are from greater ranks, so they go to the right.
425+
memcpy(local + bit * block_size, incoming, size - bit * block_size);
424426
}
425427

426428
void pcu_gather(pcu_mpi_t* mpi, pcu_coll* c, const void *send_data,

0 commit comments

Comments
 (0)