From 890f4784c1483bbf9d4fbc83d9dd210b5b93d43e Mon Sep 17 00:00:00 2001 From: jpeeler Date: Fri, 15 Feb 2008 23:33:44 +0000 Subject: Fixes bug 11471. Replaced all instances of strncpy with zap_copy_string (added to zaptel.h) to fix any off by one errors and ensure destination string is NULL terminated. git-svn-id: http://svn.digium.com/svn/zaptel/branches/1.2@3833 5390a7c7-147a-4af0-8ec9-7488f05a26cb --- zaptel.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'zaptel.h') diff --git a/zaptel.h b/zaptel.h index da5f481..c27685e 100644 --- a/zaptel.h +++ b/zaptel.h @@ -1733,4 +1733,30 @@ struct zt_radio_param { /*! Maximum audio mask */ #define ZT_FORMAT_AUDIO_MASK ((1 << 16) - 1) +/*! + \brief Size-limited null-terminating string copy. + \param dst The destination buffer + \param src The source string + \param size The size of the destination buffer + \return Nothing. + + This is similar to \a strncpy, with two important differences: + - the destination buffer will \b always be null-terminated + - the destination buffer is not filled with zeros past the copied string length + These differences make it slightly more efficient, and safer to use since it will + not leave the destination buffer unterminated. There is no need to pass an artificially + reduced buffer size to this function (unlike \a strncpy), and the buffer does not need + to be initialized to zeroes prior to calling this function. +*/ +static inline void zap_copy_string(char *dst, const char *src, unsigned int size) +{ + while (*src && size) { + *dst++ = *src++; + size--; + } + if (__builtin_expect(!size, 0)) + dst--; + *dst = '\0'; +} + #endif /* _LINUX_ZAPTEL_H */ -- cgit v1.2.3