[PATCH:libFS 7/7] Use strlcpy instead of strcpy/strncpy if it is available

Alan Coopersmith alan.coopersmith at oracle.com
Tue Jan 7 16:02:27 PST 2014


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 configure.ac   |    3 +++
 src/FSErrDis.c |    5 +++++
 src/FSlibInt.c |    4 ++++
 3 files changed, 12 insertions(+)

diff --git a/configure.ac b/configure.ac
index bcc4187..26fffb1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -51,6 +51,9 @@ PKG_CHECK_MODULES(FS, xproto >= 7.0.17 fontsproto xtrans)
 # Find needed libraries for TCP sockets (pre-pended in $LIBS)
 XTRANS_CONNECTION_FLAGS
 
+# Checks for library functions.
+AC_CHECK_FUNCS([strlcpy])
+
 # Allow checking code with lint, sparse, etc.
 XORG_WITH_LINT
 XORG_LINT_LIBRARY([FS])
diff --git a/src/FSErrDis.c b/src/FSErrDis.c
index 1a712ff..76ebab6 100644
--- a/src/FSErrDis.c
+++ b/src/FSErrDis.c
@@ -84,9 +84,14 @@ int FSGetErrorDatabaseText(
 {
     if (nbytes == 0)
 	return 0;
+#ifdef HAVE_STRLCPY
+    if (strlcpy(buffer, defaultp, nbytes) >= nbytes)
+	return 0;
+#else
     (void) strncpy(buffer, defaultp, nbytes);
     if ((strlen(defaultp) + 1) > nbytes)
 	buffer[nbytes - 1] = '\0';
+#endif
     return 1;
 }
 
diff --git a/src/FSlibInt.c b/src/FSlibInt.c
index 71f6ac1..090da3f 100644
--- a/src/FSlibInt.c
+++ b/src/FSlibInt.c
@@ -850,7 +850,11 @@ _FSPrintDefaultError(
 		ext && (ext->codes.major_opcode != event->request_code);
 		ext = ext->next);
 	if (ext)
+#ifdef HAVE_STRLCPY
+	    strlcpy(buffer, ext->name, sizeof(buffer));
+#else
 	    strcpy(buffer, ext->name);
+#endif
 	else
 	    buffer[0] = '\0';
     }
-- 
1.7.9.2



More information about the xorg-devel mailing list