Browse Source
* 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.xmkb
Peter Johanson
3 years ago
committed by
Pete Johanson
5 changed files with 55 additions and 3 deletions
@ -0,0 +1,19 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <stdlib.h> /* for size_t */ |
||||||
|
|
||||||
|
/*
|
||||||
|
* ANSI C version of strlcpy |
||||||
|
* Based on the NetBSD strlcpy man page. |
||||||
|
* |
||||||
|
* Nathan Myers <ncm-nospam@cantrip.org>, 2003/06/03 |
||||||
|
* Placed in the public domain. |
||||||
|
*/ |
||||||
|
|
||||||
|
size_t strlcpy(char *dst, const char *src, size_t size); |
@ -0,0 +1,25 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <zmk/stdlib.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
/*
|
||||||
|
* ANSI C version of strlcpy |
||||||
|
* Based on the NetBSD strlcpy man page. |
||||||
|
* |
||||||
|
* Nathan Myers <ncm-nospam@cantrip.org>, 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; |
||||||
|
} |
Loading…
Reference in new issue