diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 1e153fb6..7681efab 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -23,6 +23,7 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld) # find_package(Zephyr) which defines the target. target_include_directories(app PRIVATE include) target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c) +target_sources(app PRIVATE src/stdlib.c) target_sources(app PRIVATE src/activity.c) target_sources(app PRIVATE src/kscan.c) target_sources(app PRIVATE src/matrix_transform.c) diff --git a/app/include/zmk/stdlib.h b/app/include/zmk/stdlib.h new file mode 100644 index 00000000..fa8fe673 --- /dev/null +++ b/app/include/zmk/stdlib.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include /* for size_t */ + +/* + * ANSI C version of strlcpy + * Based on the NetBSD strlcpy man page. + * + * Nathan Myers , 2003/06/03 + * Placed in the public domain. + */ + +size_t strlcpy(char *dst, const char *src, size_t size); \ No newline at end of file diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 0d52d051..8a0e79ea 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -17,6 +17,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include #include #include #include @@ -549,8 +550,12 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi .position = event.position, .state = state ? 1 : 0, }}; - strncpy(payload.behavior_dev, binding->behavior_dev, ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1); - payload.behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1] = '\0'; + const size_t payload_dev_size = sizeof(payload.behavior_dev); + if (strlcpy(payload.behavior_dev, binding->behavior_dev, payload_dev_size) >= + payload_dev_size) { + LOG_ERR("Truncated behavior label %s to %s before invoking peripheral behavior", + log_strdup(binding->behavior_dev), log_strdup(payload.behavior_dev)); + } struct zmk_split_run_behavior_payload_wrapper wrapper = {.source = source, .payload = payload}; return split_bt_invoke_behavior_payload(wrapper); diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index ca192d7d..7de78506 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -51,8 +51,10 @@ static ssize_t split_svc_run_behavior(struct bt_conn *conn, const struct bt_gatt // We run if: // 1: We've gotten all the position/state/param data. // 2: We have a null terminated string for the behavior device label. + const size_t behavior_dev_offset = + offsetof(struct zmk_split_run_behavior_payload, behavior_dev); if ((end_addr > sizeof(struct zmk_split_run_behavior_data)) && - payload->behavior_dev[end_addr - sizeof(struct zmk_split_run_behavior_data) - 1] == '\0') { + payload->behavior_dev[end_addr - behavior_dev_offset - 1] == '\0') { struct zmk_behavior_binding binding = { .param1 = payload->data.param1, .param2 = payload->data.param2, diff --git a/app/src/stdlib.c b/app/src/stdlib.c new file mode 100644 index 00000000..5bca1696 --- /dev/null +++ b/app/src/stdlib.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +/* + * ANSI C version of strlcpy + * Based on the NetBSD strlcpy man page. + * + * Nathan Myers , 2003/06/03 + * Placed in the public domain. + */ + +size_t strlcpy(char *dst, const char *src, size_t size) { + const size_t len = strlen(src); + if (size != 0) { + memcpy(dst, src, (len > size - 1) ? size - 1 : len); + dst[size - 1] = 0; + } + return len; +}