From 18deb1c0bd1ed2e138f6cba7634b156d295725a7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 7 Jul 2020 13:14:13 +1000 Subject: [PATCH] AP_Common: added strncpy_noterm used to suppress string termination warnings --- libraries/AP_Common/AP_Common.cpp | 13 +++++++++++++ libraries/AP_Common/AP_Common.h | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/libraries/AP_Common/AP_Common.cpp b/libraries/AP_Common/AP_Common.cpp index 94cdc4f10f..3d749353fb 100644 --- a/libraries/AP_Common/AP_Common.cpp +++ b/libraries/AP_Common/AP_Common.cpp @@ -70,3 +70,16 @@ bool hex_to_uint8(uint8_t a, uint8_t &res) } return true; } + +/* + strncpy without the warning for not leaving room for nul termination + */ +void strncpy_noterm(char *dest, const char *src, size_t n) +{ + size_t len = strnlen(src, n); + if (len < n) { + // include nul term if it fits + len++; + } + memcpy(dest, src, len); +} diff --git a/libraries/AP_Common/AP_Common.h b/libraries/AP_Common/AP_Common.h index a31e7087f9..d1573a2ef7 100644 --- a/libraries/AP_Common/AP_Common.h +++ b/libraries/AP_Common/AP_Common.h @@ -138,3 +138,8 @@ template struct assert_storage_size { bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound); bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an ascii hex character + +/* + strncpy without the warning for not leaving room for nul termination + */ +void strncpy_noterm(char *dest, const char *src, size_t n);