Skip to content

Commit e99fc24

Browse files
committed
Addressed copilot's comments
1 parent 249e078 commit e99fc24

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/delta.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
293293
break;
294294
if ((memcmp(pa, (ctx->src_b + ctx->off_b), BLOCK_HDR_SIZE) == 0)) {
295295
uintptr_t b_start;
296+
uint8_t *pa_limit = ctx->src_a + ctx->size_a;
296297
/* Identical areas of BLOCK_HDR_SIZE bytes match between the images.
297298
* initialize match_len; blk_start is the relative offset within
298299
* the src image.
@@ -302,13 +303,13 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
302303
b_start = ctx->off_b;
303304
pa+= BLOCK_HDR_SIZE;
304305
ctx->off_b += BLOCK_HDR_SIZE;
305-
while (((uintptr_t)(pa - ctx->src_a) < (uintptr_t)ctx->size_a) &&
306+
while ((pa < pa_limit) &&
306307
(ctx->off_b < ctx->size_b) &&
307308
(*pa == *(ctx->src_b + ctx->off_b))) {
308309
/* Extend matching block if possible, as long as the
309310
* identical sequence continues.
310311
*/
311-
if ((uint32_t)(pa + 1 - ctx->src_a) >= ctx->size_a) {
312+
if ((pa + 1) >= pa_limit) {
312313
/* Stop matching if the source image size limit is hit. */
313314
break;
314315
}
@@ -337,6 +338,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
337338
if (!found) {
338339
/* Try matching an earlier section in the resulting image */
339340
uintptr_t pb_end = page_start * wolfboot_sector_size;
341+
uint8_t *pb_limit = ctx->src_b + pb_end;
340342
pb = ctx->src_b;
341343
while (((uintptr_t)(pb - ctx->src_b) < pb_end) && (p_off < len)) {
342344
/* Check image boundary */
@@ -362,7 +364,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
362364
blk_start = pb - ctx->src_b;
363365
pb+= BLOCK_HDR_SIZE;
364366
ctx->off_b += BLOCK_HDR_SIZE;
365-
while (((uintptr_t)(pb - ctx->src_b) < pb_end) &&
367+
while ((pb < pb_limit) &&
366368
(ctx->off_b < ctx->size_b) &&
367369
(*pb == *(ctx->src_b + ctx->off_b))) {
368370
/* Extend match as long as the areas have the
@@ -372,7 +374,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
372374
* block size.
373375
*/
374376
pb++;
375-
if ((uint32_t)(pb - ctx->src_b) >= pb_end) {
377+
if (pb >= pb_limit) {
376378
pb--;
377379
break;
378380
}

tools/unit-tests/unit-delta.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ START_TEST(test_wb_diff_self_match_extends_to_src_b_end)
167167
uint8_t *src_a;
168168
uint8_t *src_b;
169169
uint8_t patch[DELTA_BLOCK_SIZE] = {0};
170-
uint32_t sector_size;
170+
int sector_size_ret;
171+
size_t sector_size;
171172
int ret;
172173

173-
sector_size = wb_diff_get_sector_size();
174-
ck_assert_uint_gt(sector_size, BLOCK_HDR_SIZE);
174+
sector_size_ret = wb_diff_get_sector_size();
175+
ck_assert_int_gt(sector_size_ret, BLOCK_HDR_SIZE);
176+
sector_size = (size_t)sector_size_ret;
175177

176178
src_a = calloc(1, sector_size + BLOCK_HDR_SIZE);
177179
src_b = calloc(1, sector_size + BLOCK_HDR_SIZE + 1);
@@ -212,16 +214,24 @@ static void initialize_buffers(uint8_t *src_a, uint8_t *src_b, size_t size)
212214
}
213215

214216
/* Introduce differences */
215-
src_b[100] = src_a[100] + 1;
216-
src_b[200] = src_a[200] + 2;
217+
if (size > 100) {
218+
src_b[100] = src_a[100] + 1;
219+
}
220+
if (size > 200) {
221+
src_b[200] = src_a[200] + 2;
222+
}
217223

218224
/* 10-bytes difference across two blocks */
219-
for (int i = 1020; i < 1040; ++i) {
225+
for (int i = 1020; i < 1040 && (size_t)i < size; ++i) {
220226
src_b[i] = src_a[i] + 3;
221227
}
222228

223-
src_a[510] = ESC;
224-
src_b[1022] = ESC;
229+
if (size > 510) {
230+
src_a[510] = ESC;
231+
}
232+
if (size > 1022) {
233+
src_b[1022] = ESC;
234+
}
225235

226236
}
227237

0 commit comments

Comments
 (0)