[PATCH:xf86-input-mouse] Use asprintf (or Xprintf on old servers) instead of strdup+sprintf

Alan Coopersmith alan.coopersmith at oracle.com
Sun Oct 20 06:51:31 CEST 2013


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 configure.ac |    3 +++
 src/mouse.c  |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/configure.ac b/configure.ac
index ee6a345..bd782a5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,6 +44,9 @@ XORG_MACROS_VERSION(1.8)
 XORG_DEFAULT_OPTIONS
 XORG_WITH_LINT
 
+# Checks for library functions
+AC_CHECK_FUNCS([asprintf])
+
 # Obtain compiler/linker options from server and required extensions
 PKG_CHECK_MODULES(XORG, [xorg-server >= 1.7] xproto inputproto)
 
diff --git a/src/mouse.c b/src/mouse.c
index f60d6c2..143664c 100644
--- a/src/mouse.c
+++ b/src/mouse.c
@@ -74,6 +74,13 @@
 #include "mousePriv.h"
 #include "mipointer.h"
 
+/* Xorg >= 1.10 provides an asprintf() implementation even if libc doesn't */
+#include "xorgVersion.h"
+#if defined(HAVE_ASPRINTF) || \
+    (XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,9,99,901,0))
+# define USE_ASPRINTF
+#endif
+
 enum {
     /* number of bits in mapped nibble */
     NIB_BITS=4,
@@ -441,20 +448,27 @@ MouseCommonOptions(InputInfoPtr pInfo)
         } else if (sscanf(s, "%d %d %d %d", &b1, &b2, &b3, &b4) >= 2 &&
                  b1 > 0 && b1 <= MSE_MAXBUTTONS &&
                  b2 > 0 && b2 <= MSE_MAXBUTTONS) {
-            msg = xstrdup("buttons XX and YY");
-            if (msg)
-                sprintf(msg, "buttons %d and %d", b1, b2);
             pMse->negativeZ = 1 << (b1-1);
             pMse->positiveZ = 1 << (b2-1);
             if (b3 > 0 && b3 <= MSE_MAXBUTTONS &&
                 b4 > 0 && b4 <= MSE_MAXBUTTONS) {
-                if (msg)
-                    free(msg);
-                msg = xstrdup("buttons XX, YY, ZZ and WW");
-                if (msg)
-                    sprintf(msg, "buttons %d, %d, %d and %d", b1, b2, b3, b4);
                 pMse->negativeW = 1 << (b3-1);
                 pMse->positiveW = 1 << (b4-1);
+#ifdef USE_ASPRINTF
+                if (asprintf(&msg, "buttons %d, %d, %d and %d",
+                             b1, b2, b3, b4) == -1)
+                    msg = NULL;
+#else
+                msg = Xprintf("buttons %d, %d, %d and %d", b1, b2, b3, b4);
+#endif
+            }
+            else {
+#ifdef USE_ASPRINTF
+                if (asprintf(&msg, "buttons %d and %d", b1, b2) == -1)
+                    msg = NULL;
+#else
+                msg = Xprintf("buttons %d and %d", b1, b2);
+#endif
             }
             if (b1 > pMse->buttons) pMse->buttons = b1;
             if (b2 > pMse->buttons) pMse->buttons = b2;
@@ -509,9 +523,12 @@ MouseCommonOptions(InputInfoPtr pInfo)
             if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
                  b1 > 0 && b1 <= MSE_MAXBUTTONS &&
                  b2 > 0 && b2 <= MSE_MAXBUTTONS) {
-                msg = xstrdup("buttons XX and YY");
-                if (msg)
-                    sprintf(msg, "buttons %d and %d", b1, b2);
+#ifdef USE_ASPRINTF
+                if (asprintf(&msg, "buttons %d and %d", b1, b2) == -1)
+                    msg = NULL;
+#else
+                msg = Xprintf("buttons %d and %d", b1, b2);
+#endif
                 pMse->negativeX = b1;
                 pMse->positiveX = b2;
                 if (b1 > pMse->buttons) pMse->buttons = b1;
@@ -534,9 +551,12 @@ MouseCommonOptions(InputInfoPtr pInfo)
             if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
                  b1 > 0 && b1 <= MSE_MAXBUTTONS &&
                  b2 > 0 && b2 <= MSE_MAXBUTTONS) {
-                msg = xstrdup("buttons XX and YY");
-                if (msg)
-                    sprintf(msg, "buttons %d and %d", b1, b2);
+#ifdef USE_ASPRINTF
+                if (asprintf(&msg, "buttons %d and %d", b1, b2) == -1)
+                    msg = NULL;
+#else
+                msg = Xprintf("buttons %d and %d", b1, b2);
+#endif
                 pMse->negativeY = b1;
                 pMse->positiveY = b2;
                 if (b1 > pMse->buttons) pMse->buttons = b1;
@@ -606,9 +626,12 @@ MouseCommonOptions(InputInfoPtr pInfo)
         if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
             (b1 > 0) && (b1 <= MSE_MAXBUTTONS) &&
             (b2 > 0) && (b2 <= MSE_MAXBUTTONS)) {
-            msg = xstrdup("buttons XX and YY");
-            if (msg)
-                sprintf(msg, "buttons %d and %d", b1, b2);
+#ifdef USE_ASPRINTF
+            if (asprintf(&msg, "buttons %d and %d", b1, b2) == -1)
+                msg = NULL;
+#else
+            msg = Xprintf("buttons %d and %d", b1, b2);
+#endif
             pMse->doubleClickTargetButton = b1;
             pMse->doubleClickTargetButtonMask = 1 << (b1 - 1);
             pMse->doubleClickSourceButtonMask = 1 << (b2 - 1);
-- 
1.7.9.2



More information about the xorg-devel mailing list