Browse Source

AP_Common: added strncpy_noterm

used to suppress string termination warnings
c415-sdk
Andrew Tridgell 5 years ago
parent
commit
18deb1c0bd
  1. 13
      libraries/AP_Common/AP_Common.cpp
  2. 5
      libraries/AP_Common/AP_Common.h

13
libraries/AP_Common/AP_Common.cpp

@ -70,3 +70,16 @@ bool hex_to_uint8(uint8_t a, uint8_t &res) @@ -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);
}

5
libraries/AP_Common/AP_Common.h

@ -138,3 +138,8 @@ template<typename s, size_t t> struct assert_storage_size { @@ -138,3 +138,8 @@ template<typename s, size_t t> 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);

Loading…
Cancel
Save