Skip to content

Commit 7430750

Browse files
committed
refactor(splits): Minor cleanups to periph invocation
* Add strlcpy from public domain version. * Leverage strlcpy to detect truncation of behavior dev strs, and log. * Use `offsetof` for cleaner detection on peripheral side.
1 parent b8700ea commit 7430750

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld)
2323
# find_package(Zephyr) which defines the target.
2424
target_include_directories(app PRIVATE include)
2525
target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c)
26+
target_sources(app PRIVATE src/stdlib.c)
2627
target_sources(app PRIVATE src/activity.c)
2728
target_sources(app PRIVATE src/kscan.c)
2829
target_sources(app PRIVATE src/matrix_transform.c)

app/include/zmk/stdlib.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2022 The ZMK Contributors
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdlib.h> /* for size_t */
10+
11+
/*
12+
* ANSI C version of strlcpy
13+
* Based on the NetBSD strlcpy man page.
14+
*
15+
* Nathan Myers <[email protected]>, 2003/06/03
16+
* Placed in the public domain.
17+
*/
18+
19+
size_t strlcpy(char *dst, const char *src, size_t size);

app/src/split/bluetooth/central.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
1919

20+
#include <zmk/stdlib.h>
2021
#include <zmk/ble.h>
2122
#include <zmk/behavior.h>
2223
#include <zmk/split/bluetooth/uuid.h>
@@ -549,8 +550,12 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi
549550
.position = event.position,
550551
.state = state ? 1 : 0,
551552
}};
552-
strncpy(payload.behavior_dev, binding->behavior_dev, ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1);
553-
payload.behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1] = '\0';
553+
const size_t payload_dev_size = sizeof(payload.behavior_dev);
554+
if (strlcpy(payload.behavior_dev, binding->behavior_dev, payload_dev_size) >=
555+
payload_dev_size) {
556+
LOG_ERR("Truncated behavior label %s to %s before invoking peripheral behavior",
557+
log_strdup(binding->behavior_dev), log_strdup(payload.behavior_dev));
558+
}
554559

555560
struct zmk_split_run_behavior_payload_wrapper wrapper = {.source = source, .payload = payload};
556561
return split_bt_invoke_behavior_payload(wrapper);

app/src/split/bluetooth/service.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ static ssize_t split_svc_run_behavior(struct bt_conn *conn, const struct bt_gatt
5151
// We run if:
5252
// 1: We've gotten all the position/state/param data.
5353
// 2: We have a null terminated string for the behavior device label.
54+
const size_t behavior_dev_offset =
55+
offsetof(struct zmk_split_run_behavior_payload, behavior_dev);
5456
if ((end_addr > sizeof(struct zmk_split_run_behavior_data)) &&
55-
payload->behavior_dev[end_addr - sizeof(struct zmk_split_run_behavior_data) - 1] == '\0') {
57+
payload->behavior_dev[end_addr - behavior_dev_offset - 1] == '\0') {
5658
struct zmk_behavior_binding binding = {
5759
.param1 = payload->data.param1,
5860
.param2 = payload->data.param2,

app/src/stdlib.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2022 The ZMK Contributors
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <zmk/stdlib.h>
8+
#include <string.h>
9+
10+
/*
11+
* ANSI C version of strlcpy
12+
* Based on the NetBSD strlcpy man page.
13+
*
14+
* Nathan Myers <[email protected]>, 2003/06/03
15+
* Placed in the public domain.
16+
*/
17+
18+
size_t strlcpy(char *dst, const char *src, size_t size) {
19+
const size_t len = strlen(src);
20+
if (size != 0) {
21+
memcpy(dst, src, (len > size - 1) ? size - 1 : len);
22+
dst[size - 1] = 0;
23+
}
24+
return len;
25+
}

0 commit comments

Comments
 (0)