Skip to content

Commit 7e0967b

Browse files
committed
tests: cloud: Use strnlen() for ssafety
Use strnlen() instead of strlen(). It was safe in this case, but always nice to use strnlen() when possible. Flagged by Sonarcloud. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent 26543a4 commit 7e0967b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/module/cloud/src/cloud_module_test.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
*
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
6+
7+
/* Ensure 'strnlen' is available even with -std=c99. */
8+
#if !defined(_POSIX_C_SOURCE)
9+
#define _POSIX_C_SOURCE 200809L
10+
#endif
11+
612
#include <unity.h>
713
#include <zephyr/fff.h>
814
#include <zephyr/task_wdt/task_wdt.h>
915
#include <zephyr/net/coap.h>
1016
#include <zephyr/net/coap_client.h>
17+
#include <zephyr/zbus/zbus.h>
1118

1219
#include "environmental.h"
1320
#include "cloud_module.h"
@@ -185,7 +192,7 @@ void test_sending_payload(void)
185192
struct cloud_msg msg = {
186193
.type = CLOUD_PAYLOAD_JSON,
187194
.payload.buffer = "{\"test\": 1}",
188-
.payload.buffer_data_len = strlen(msg.payload.buffer),
195+
.payload.buffer_data_len = strnlen(msg.payload.buffer, sizeof(msg.payload.buffer)),
189196
};
190197

191198
err = zbus_chan_pub(&CLOUD_CHAN, &msg, K_SECONDS(1));
@@ -222,7 +229,7 @@ void test_connected_paused_to_ready_send_payload(void)
222229
struct cloud_msg msg = {
223230
.type = CLOUD_PAYLOAD_JSON,
224231
.payload.buffer = "{\"Another\": \"test\"}",
225-
.payload.buffer_data_len = strlen(msg.payload.buffer),
232+
.payload.buffer_data_len = strnlen(msg.payload.buffer, sizeof(msg.payload.buffer)),
226233
};
227234

228235
/* Reset call count */

0 commit comments

Comments
 (0)