Skip to content

Commit b51ba60

Browse files
SeppoTakalojhedberg
authored andcommitted
tests: mcumgr: smp_client: Fix double free on test case
smp_client is automatically freeing all net_buf* given for it, so testcase does not need to call smp_client_buf_free() for buffers that are processed. Signed-off-by: Seppo Takalo <[email protected]>
1 parent be216ba commit b51ba60

File tree

1 file changed

+19
-27
lines changed
  • tests/subsys/mgmt/mcumgr/smp_client/src

1 file changed

+19
-27
lines changed

tests/subsys/mgmt/mcumgr/smp_client/src/main.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include <mgmt/mcumgr/transport/smp_internal.h>
1919
#include "smp_transport_stub.h"
2020

21-
static struct net_buf *buf[5];
22-
2321
static uint32_t testing_user_data;
2422
static void *response_ptr;
2523
static struct net_buf *res_buf;
@@ -35,9 +33,10 @@ int smp_client_res_cb(struct net_buf *nb, void *user_data)
3533
ZTEST(smp_client, test_buf_alloc)
3634
{
3735
struct smp_client_object smp_client;
36+
struct net_buf *buf[5];
3837

3938
/* Allocate all 4 buffer's and verify that 5th fail */
40-
for (int i = 0; i < 5; i++) {
39+
for (int i = 0; i < ARRAY_SIZE(buf); i++) {
4140
buf[i] = smp_client_buf_allocation(&smp_client, MGMT_GROUP_ID_IMAGE, 1,
4241
MGMT_OP_WRITE, SMP_MCUMGR_VERSION_1);
4342
if (i == 4) {
@@ -50,9 +49,11 @@ ZTEST(smp_client, test_buf_alloc)
5049
}
5150
}
5251

53-
for (int i = 0; i < 4; i++) {
54-
smp_client_buf_free(buf[i]);
55-
buf[i] = NULL;
52+
for (int i = 0; i < ARRAY_SIZE(buf); i++) {
53+
if (buf[i]) {
54+
smp_client_buf_free(buf[i]);
55+
buf[i] = NULL;
56+
}
5657
}
5758
}
5859

@@ -75,33 +76,34 @@ ZTEST(smp_client, test_msg_send_timeout)
7576
ZTEST(smp_client, test_msg_response_handler)
7677
{
7778
struct smp_hdr dst_hdr;
79+
struct net_buf *a, *b;
7880
int rc;
7981

8082

8183
response_ptr = NULL;
8284
res_buf = NULL;
8385

84-
buf[0] = smp_client_buf_allocation(&smp_client, MGMT_GROUP_ID_IMAGE, 1, MGMT_OP_WRITE,
86+
a = smp_client_buf_allocation(&smp_client, MGMT_GROUP_ID_IMAGE, 1, MGMT_OP_WRITE,
8587
SMP_MCUMGR_VERSION_1);
86-
zassert_not_null(buf[0], "Buffer was Null");
87-
rc = smp_client_send_cmd(&smp_client, buf[0], smp_client_res_cb, &testing_user_data, 8);
88+
zassert_not_null(a, "Buffer was Null");
89+
rc = smp_client_send_cmd(&smp_client, a, smp_client_res_cb, &testing_user_data, 8);
8890
zassert_equal(MGMT_ERR_EOK, rc, "Expected to receive %d response %d", MGMT_ERR_EOK, rc);
89-
buf[1] = smp_client_buf_allocation(&smp_client, MGMT_GROUP_ID_IMAGE, 1, MGMT_OP_WRITE,
91+
b = smp_client_buf_allocation(&smp_client, MGMT_GROUP_ID_IMAGE, 1, MGMT_OP_WRITE,
9092
SMP_MCUMGR_VERSION_1);
91-
zassert_not_null(buf[0], "Buffer was Null");
93+
zassert_not_null(b, "Buffer was Null");
9294
/* Read Pushed packet Header */
93-
smp_transport_read_hdr(buf[0], &dst_hdr);
94-
smp_client_single_response(buf[1], &dst_hdr);
95+
smp_transport_read_hdr(a, &dst_hdr);
96+
smp_client_single_response(b, &dst_hdr);
9597
zassert_is_null(res_buf, "NULL pointer was not returned");
9698
zassert_is_null(response_ptr, "NULL pointer was not returned");
9799
/* Set Correct OP */
98100
dst_hdr.nh_op = MGMT_OP_WRITE_RSP;
99-
smp_client_single_response(buf[1], &dst_hdr);
100-
zassert_equal_ptr(res_buf, buf[1], "Response Buf not correct");
101+
smp_client_single_response(b, &dst_hdr);
102+
zassert_equal_ptr(res_buf, b, "Response Buf not correct");
101103
zassert_equal_ptr(response_ptr, &testing_user_data, "User data not returned correctly");
102104
response_ptr = NULL;
103105
res_buf = NULL;
104-
smp_client_single_response(buf[1], &dst_hdr);
106+
smp_client_single_response(b, &dst_hdr);
105107
zassert_is_null(res_buf, "NULL pointer was not returned");
106108
zassert_is_null(response_ptr, "NULL pointer was not returned");
107109
}
@@ -114,15 +116,5 @@ static void *setup_custom_os(void)
114116
return NULL;
115117
}
116118

117-
static void cleanup_test(void *p)
118-
{
119-
for (int i = 0; i < 5; i++) {
120-
if (buf[i]) {
121-
smp_client_buf_free(buf[i]);
122-
buf[i] = NULL;
123-
}
124-
}
125-
}
126-
127119
/* Main test set */
128-
ZTEST_SUITE(smp_client, NULL, setup_custom_os, NULL, cleanup_test, NULL);
120+
ZTEST_SUITE(smp_client, NULL, setup_custom_os, NULL, NULL, NULL);

0 commit comments

Comments
 (0)