aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-02-28 12:08:22 -0700
committerTom Rini <trini@konsulko.com>2022-04-06 14:01:42 -0400
commit99aca9efe10c005189b47160d178d18f14301c7e (patch)
treeaeec82c199fce85b9d4bd59c40030b8039b40ce9 /test
parent64aefc4800b7fb09d179ab1f0ede0363cff27856 (diff)
downloadu-boot-99aca9efe10c005189b47160d178d18f14301c7e.zip
u-boot-99aca9efe10c005189b47160d178d18f14301c7e.tar.gz
u-boot-99aca9efe10c005189b47160d178d18f14301c7e.tar.bz2
abuf: Correct a corner case with abuf_realloc()
If the buffer is empty and not allocated, then abuf_realloc() tries to copy invalid data. This happens because an incorrect change to use memdup() was added after the original code was written. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/lib/abuf.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/lib/abuf.c b/test/lib/abuf.c
index 086c9b2..42ee4c1 100644
--- a/test/lib/abuf.c
+++ b/test/lib/abuf.c
@@ -126,6 +126,35 @@ static int lib_test_abuf_realloc(struct unit_test_state *uts)
}
LIB_TEST(lib_test_abuf_realloc, 0);
+/* Test abuf_realloc() on an non-allocated buffer of zero size */
+static int lib_test_abuf_realloc_size(struct unit_test_state *uts)
+{
+ struct abuf buf;
+ ulong start;
+
+ start = ut_check_free();
+
+ abuf_init(&buf);
+
+ /* Allocate some space */
+ ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
+ ut_assertnonnull(buf.data);
+ ut_asserteq(TEST_DATA_LEN, buf.size);
+ ut_asserteq(true, buf.alloced);
+
+ /* Free it */
+ ut_asserteq(true, abuf_realloc(&buf, 0));
+ ut_assertnull(buf.data);
+ ut_asserteq(0, buf.size);
+ ut_asserteq(false, buf.alloced);
+
+ /* Check for memory leaks */
+ ut_assertok(ut_check_delta(start));
+
+ return 0;
+}
+LIB_TEST(lib_test_abuf_realloc_size, 0);
+
/* Test handling of buffers that are too large */
static int lib_test_abuf_large(struct unit_test_state *uts)
{