Skip to content

Commit 2768ae6

Browse files
committed
Addressed copilot's comments
1 parent 0d56a02 commit 2768ae6

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

src/delta.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
397397

398398
if (!found) {
399399
if (*(ctx->src_b + ctx->off_b) == ESC) {
400+
if ((p_off + 1) >= (len - BLOCK_HDR_SIZE))
401+
break;
400402
*(patch + p_off++) = ESC;
401403
*(patch + p_off++) = ESC;
402404
} else {

src/store_sbrk.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1+
/* store_sbrk.c
2+
*
3+
* Copyright (C) 2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
122
#include <stddef.h>
23+
#include <limits.h>
224

325
#include "store_sbrk.h"
426

@@ -8,8 +30,11 @@ void *wolfboot_store_sbrk(unsigned int incr, uint8_t **heap,
830
uint8_t *heap_limit = heap_base + heap_size;
931
void *old_heap = *heap;
1032

11-
if (((incr >> 2) << 2) != incr)
12-
incr = ((incr >> 2) + 1) << 2;
33+
if ((incr & 3U) != 0U) {
34+
if (incr > (UINT_MAX - 3U))
35+
return (void *)-1;
36+
incr = (incr + 3U) & ~3U;
37+
}
1338

1439
if (*heap == NULL) {
1540
*heap = heap_base;

src/store_sbrk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/* store_sbrk.h
2+
*
3+
* Copyright (C) 2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
122
#ifndef WOLFBOOT_STORE_SBRK_H
223
#define WOLFBOOT_STORE_SBRK_H
324

tools/unit-tests/unit-delta.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,27 @@ START_TEST(test_wb_diff_preserves_trailing_header_margin_for_escape)
217217
}
218218
END_TEST
219219

220+
START_TEST(test_wb_diff_preserves_main_loop_header_margin_for_escape)
221+
{
222+
WB_DIFF_CTX diff_ctx;
223+
uint8_t src_a[64] = {0};
224+
uint8_t src_b[64] = {0};
225+
uint8_t patch[BLOCK_HDR_SIZE + 2] = {0};
226+
int ret;
227+
228+
memset(src_b, 0x5a, BLOCK_HDR_SIZE + 1);
229+
src_b[0] = ESC;
230+
231+
ret = wb_diff_init(&diff_ctx, src_a, sizeof(src_a), src_b, BLOCK_HDR_SIZE + 1);
232+
ck_assert_int_eq(ret, 0);
233+
234+
ret = wb_diff(&diff_ctx, patch, BLOCK_HDR_SIZE + 1);
235+
236+
ck_assert_int_eq(ret, 0);
237+
ck_assert_uint_eq(patch[0], 0);
238+
}
239+
END_TEST
240+
220241
static void initialize_buffers(uint8_t *src_a, uint8_t *src_b, size_t size)
221242
{
222243
uint32_t pseudo_rand = 0;
@@ -326,6 +347,7 @@ Suite *patch_diff_suite(void)
326347
tcase_add_test(tc_wolfboot_delta, test_wb_diff_match_extends_to_src_b_end);
327348
tcase_add_test(tc_wolfboot_delta, test_wb_diff_self_match_extends_to_src_b_end);
328349
tcase_add_test(tc_wolfboot_delta, test_wb_diff_preserves_trailing_header_margin_for_escape);
350+
tcase_add_test(tc_wolfboot_delta, test_wb_diff_preserves_main_loop_header_margin_for_escape);
329351
tcase_add_test(tc_wolfboot_delta, test_wb_patch_and_diff);
330352
suite_add_tcase(s, tc_wolfboot_delta);
331353

tools/unit-tests/unit-store-sbrk.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
/* unit-store-sbrk.c
22
*
33
* Unit tests for store allocator helper.
4+
*
5+
* Copyright (C) 2025 wolfSSL Inc.
6+
*
7+
* This file is part of wolfBoot.
8+
*
9+
* wolfBoot is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* wolfBoot is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
422
*/
523

624
#include <check.h>
25+
#include <limits.h>
726
#include <stdint.h>
827

928
#include "../../src/store_sbrk.h"
@@ -36,13 +55,27 @@ START_TEST(test_sbrk_rejects_overflow)
3655
}
3756
END_TEST
3857

58+
START_TEST(test_sbrk_rejects_alignment_overflow)
59+
{
60+
uint8_t heap_buf[16];
61+
uint8_t *heap = NULL;
62+
void *ret;
63+
64+
ret = wolfboot_store_sbrk(UINT_MAX - 1U, &heap, heap_buf, sizeof(heap_buf));
65+
66+
ck_assert_ptr_eq(ret, (void *)-1);
67+
ck_assert_ptr_eq(heap, NULL);
68+
}
69+
END_TEST
70+
3971
Suite *wolfboot_suite(void)
4072
{
4173
Suite *s = suite_create("store-sbrk");
4274
TCase *tcase = tcase_create("store_sbrk");
4375

4476
tcase_add_test(tcase, test_sbrk_first_call_advances_heap);
4577
tcase_add_test(tcase, test_sbrk_rejects_overflow);
78+
tcase_add_test(tcase, test_sbrk_rejects_alignment_overflow);
4679
suite_add_tcase(s, tcase);
4780
return s;
4881
}

0 commit comments

Comments
 (0)