summaryrefslogtreecommitdiff
path: root/zaptel.h
diff options
context:
space:
mode:
authorjpeeler <jpeeler@5390a7c7-147a-4af0-8ec9-7488f05a26cb>2008-02-15 23:33:44 +0000
committerjpeeler <jpeeler@5390a7c7-147a-4af0-8ec9-7488f05a26cb>2008-02-15 23:33:44 +0000
commit890f4784c1483bbf9d4fbc83d9dd210b5b93d43e (patch)
treea6e737a6434cb08ac4cbb9cde4f38e0ea98d7c01 /zaptel.h
parentc1cbe89ec4051c38ec600cc8b4cc0ed0deb10bb4 (diff)
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
Diffstat (limited to 'zaptel.h')
-rw-r--r--zaptel.h26
1 files changed, 26 insertions, 0 deletions
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 */