[PATCH:xf86-input-mouse] Use asprintf (or Xprintf on old servers) instead of strdup+sprintf
Peter Hutterer
peter.hutterer at who-t.net
Tue Oct 22 00:02:06 CEST 2013
On Sat, Oct 19, 2013 at 09:51:31PM -0700, Alan Coopersmith wrote:
> 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))
use 1.10.0 as version here. let's not worry about release candidates here and
it will make the code easier to read.
> +# 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
if Xprintf is an API that we still support, already uses
asprintf anyway why not use just that and ditch the #ifdef's?
anyway Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net> if this
approach is what you prefer but I think Xprintf is good enough until we're
ready to ditch pre-1.10.
Cheers,
Peter
> + }
> + 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
>
> _______________________________________________
> xorg-devel at lists.x.org: X.Org development
> Archives: http://lists.x.org/archives/xorg-devel
> Info: http://lists.x.org/mailman/listinfo/xorg-devel
>
More information about the xorg-devel
mailing list