From eeceeea7632dfdaded119f0716b17e3c4e235ba1 Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Fri, 7 Feb 2025 21:35:43 -0800 Subject: [PATCH 1/2] Don't ignore errors reported by skip_bytes() If our read or skip callback reports an error here we should return immediately, rather than ignoring the error. --- cmp.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmp.c b/cmp.c index f8712da..0aebd74 100644 --- a/cmp.c +++ b/cmp.c @@ -2739,7 +2739,9 @@ bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj) { break; } - skip_bytes(ctx, size); + if (!skip_bytes(ctx, size)) { + return false; + } } } @@ -2806,7 +2808,9 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { break; } - skip_bytes(ctx, size); + if (!skip_bytes(ctx, size)) { + return false; + } } } @@ -2883,7 +2887,9 @@ bool cmp_skip_object_no_limit(cmp_ctx_t *ctx) { break; } - skip_bytes(ctx, size); + if (!skip_bytes(ctx, size)) { + return false; + } } } @@ -2975,7 +2981,9 @@ bool cmp_skip_object_limit(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit) { break; } - skip_bytes(ctx, size); + if (!skip_bytes(ctx, size)) { + return false; + } } } From b2e01867a4e3412db3002c5fe38d1f2ba5d1ba01 Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Sat, 8 Feb 2025 23:08:13 -0800 Subject: [PATCH 2/2] Add test for skip_bytes() fix --- test/test.c | 3 ++- test/tests.c | 31 +++++++++++++++++++++++++++++++ test/tests.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/test/test.c b/test/test.c index 89d263e..13fd597 100644 --- a/test/test.c +++ b/test/test.c @@ -33,7 +33,7 @@ THE SOFTWARE. int main(void) { /* Use the old CMocka API because Travis' latest Ubuntu is Trusty */ - const UnitTest tests[17] = { + const UnitTest tests[18] = { unit_test(test_msgpack), unit_test(test_fixedint), unit_test(test_numbers), @@ -51,6 +51,7 @@ int main(void) { #endif unit_test(test_skipping), + unit_test(test_skip_bytes), unit_test(test_deprecated_limited_skipping), unit_test(test_errors), unit_test(test_version), diff --git a/test/tests.c b/test/tests.c index 9e1046f..d9f96b1 100644 --- a/test/tests.c +++ b/test/tests.c @@ -4696,6 +4696,37 @@ void test_skipping(void **state) { teardown_cmp_and_buf(&cmp, &buf); } +void test_skip_bytes(void **state) { + buf_t buf; + cmp_ctx_t cmp; + cmp_object_t obj; + + (void)state; + + setup_cmp_and_buf(&cmp, &buf); + + M_BufferEnsureCapacity(&buf, 100); + + /* Write the string marker but omit the string contents. This should ensure + that skip_bytes() (and thus cmp_skip_object*) will fail when it tries + to skip the string. */ + assert_true(cmp_write_str_marker(&cmp, 20)); + + M_BufferSeek(&buf, 0); + assert_false(cmp_skip_object(&cmp, &obj)); + + M_BufferSeek(&buf, 0); + assert_false(cmp_skip_object_flat(&cmp, &obj)); + + M_BufferSeek(&buf, 0); + assert_false(cmp_skip_object_no_limit(&cmp)); + + M_BufferSeek(&buf, 0); + assert_false(cmp_skip_object_limit(&cmp, &obj, 1)); + + teardown_cmp_and_buf(&cmp, &buf); +} + void test_deprecated_limited_skipping(void **state) { buf_t buf; cmp_ctx_t cmp; diff --git a/test/tests.h b/test/tests.h index 57f458c..ec9535d 100644 --- a/test/tests.h +++ b/test/tests.h @@ -38,6 +38,7 @@ void test_obj(void **state); void test_float_flip(void **state); #endif void test_skipping(void **state); +void test_skip_bytes(void **state); void test_deprecated_limited_skipping(void **state); void test_errors(void **state); void test_version(void **state);