From 57c4385a186b505f74bf0f14fc14112a5d4097a5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 26 Nov 2011 17:01:33 +1100 Subject: [PATCH] AP_Common: replace strlcat_P() with a C implementation the one in the Arduino libc was giving us bogus results on one machine, and is suspect. We couldn't spot what is wrong in the assembler, but replacing the implementation with a C one fixes the bug, so we replaced it --- libraries/AP_Common/AP_Common.h | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/libraries/AP_Common/AP_Common.h b/libraries/AP_Common/AP_Common.h index 71c2f112e1..2aa11ec7f6 100644 --- a/libraries/AP_Common/AP_Common.h +++ b/libraries/AP_Common/AP_Common.h @@ -105,9 +105,34 @@ static inline int strcmp_P(const char *str1, const prog_char_t *pstr) return strcmp_P(str1, (const prog_char *)pstr); } -static inline size_t strlcat_P(char *buffer, const prog_char_t *pstr, size_t buffer_size) +static inline size_t strlen_P(const prog_char_t *pstr) { - return strlcat_P(buffer, (const prog_char *)pstr, buffer_size); + return strlen_P((const prog_char *)pstr); +} + +// strlcat_P() in AVR libc seems to be broken +static inline size_t strlcat_P(char *d, const prog_char_t *s, size_t bufsize) +{ + size_t len1 = strlen(d); + size_t len2 = strlen_P(s); + size_t ret = len1 + len2; + + if (len1+len2 >= bufsize) { + if (bufsize < (len1+1)) { + return ret; + } + len2 = bufsize - (len1+1); + } + if (len2 > 0) { + memcpy_P(d+len1, s, len2); + d[len1+len2] = 0; + } + return ret; +} + +static inline char *strncpy_P(char *buffer, const prog_char_t *pstr, size_t buffer_size) +{ + return strncpy_P(buffer, (const prog_char *)pstr, buffer_size); }