[Mesa-dev] [PATCH v3 1/3] util: Add util_strncpy

Robert Foss robert.foss at collabora.com
Mon Jul 3 03:49:18 UTC 2017


Add strncpy wrapper that makes sure that the string is always ends
with a null byte.

Signed-off-by: Robert Foss <robert.foss at collabora.com>
---
Changes since v2:
  Brian Paul <brianp at vmware.com>
    - Patch added

 src/util/u_string.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/util/u_string.h b/src/util/u_string.h
index e88e13f42c..a8fb92174e 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -48,6 +48,15 @@
 extern "C" {
 #endif
 
+static inline char*
+util_strncpy(char *dst, const char *src, size_t n)
+{
+   strncpy(dst, src, n);
+   dst[n-1] = '\0';
+
+   return dst;
+}
+
 #ifdef _GNU_SOURCE
 
 #define util_strchrnul strchrnul
-- 
2.11.0



More information about the mesa-dev mailing list