[PATCH:xmodmap 3/3] Replace chk_malloc + sprintf with asprintf
walter harms
wharms at bfs.de
Sat Nov 12 01:14:07 PST 2011
Am 12.11.2011 06:27, schrieb Alan Coopersmith:
> Includes local private copy of asprintf for OS'es without it in libc.
> Removes chk_malloc as no callers remain anymore.
>
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
> configure.ac | 2 +-
> xmodmap.c | 69 +++++++++++++++++++++++++++++++++++++++++++++------------
> xmodmap.h | 5 +++-
> 3 files changed, 59 insertions(+), 17 deletions(-)
>
> diff --git a/configure.ac b/configure.ac
> index de88e4c..f9ebca3 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -37,7 +37,7 @@ XORG_DEFAULT_OPTIONS
>
> AC_CONFIG_HEADERS([config.h])
>
> -AC_CHECK_FUNCS([strncasecmp])
> +AC_CHECK_FUNCS([strncasecmp asprintf])
>
> # Checks for pkg-config packages
> PKG_CHECK_MODULES(XMODMAP, x11 xproto >= 7.0.17)
> diff --git a/xmodmap.c b/xmodmap.c
> index f49bc33..58a8e70 100644
> --- a/xmodmap.c
> +++ b/xmodmap.c
> @@ -31,6 +31,7 @@ from The Open Group.
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
> +#include <stdarg.h>
> #include "xmodmap.h"
>
> const char *ProgramName;
> @@ -50,16 +51,54 @@ Exit(int status)
> exit (status);
> }
>
> -void *
> -chk_malloc(size_t n_bytes)
> +static void _X_NORETURN
> +FatalError(const char *message)
> {
> - void *buf = malloc(n_bytes);
> - if (!buf) {
> - fprintf(stderr, "%s: Could not allocate %d bytes\n", ProgramName, (int)n_bytes);
> - Exit(-1);
> + fprintf(stderr, "%s: %s\n", ProgramName, message);
> + Exit(-1);
> +}
> +
Exit(-1); translates into exit(-1); (Why ?)
this does not work as intended. maybe 1 or EXIT_FAILURE is better here.
re
wh
> +#ifndef HAVE_ASPRINTF
> +/* sprintf variant found in newer libc's which allocates string to print to */
> +static int _X_ATTRIBUTE_PRINTF(2,3)
> +asprintf(char ** ret, const char *format, ...)
> +{
> + char buf[256];
> + int len;
> + va_list ap;
> +
> + va_start(ap, format);
> + len = vsnprintf(buf, sizeof(buf), format, ap);
> + va_end(ap);
> +
> + if (len < 0)
> + return -1;
> +
> + if (len < sizeof(buf))
> + {
> + *ret = strdup(buf);
> }
> - return buf;
> + else
> + {
> + *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
> + if (*ret != NULL)
> + {
> + va_start(ap, format);
> + len = vsnprintf(*ret, len + 1, format, ap);
> + va_end(ap);
> + if (len < 0) {
> + free(*ret);
> + *ret = NULL;
> + }
> + }
> + }
> +
> + if (*ret == NULL)
> + return -1;
> +
> + return len;
> }
> +#endif /* HAVE_ASPRINTF */
>
> static const char help_message[] =
> "\nwhere options include:\n"
> @@ -247,11 +286,11 @@ main(int argc, char *argv[])
> char *cmd;
> didAnything = True;
> if (++i >= argc) usage ();
> - cmd = chk_malloc (strlen ("remove control = ") + strlen (argv[i]) + 1);
> - (void) sprintf (cmd, "remove %s = %s",
> + if (asprintf (&cmd, "remove %s = %s",
> ((arg[1] == 's') ? "shift" :
> ((arg[1] == 'l') ? "lock" :
> - "control")), argv[i]);
> + "control")), argv[i]) == -1)
> + FatalError("Could not allocate memory for remove cmd");
> process_line (cmd);
> continue;
> }
> @@ -269,8 +308,8 @@ main(int argc, char *argv[])
> char *cmd;
> didAnything = True;
> if (++i >= argc) usage ();
> - cmd = chk_malloc (strlen ("add modX = ") + strlen (argv[i]) + 1);
> - (void) sprintf (cmd, "add mod%c = %s", arg[1], argv[i]);
> + if (asprintf (&cmd, "add mod%c = %s", arg[1], argv[i]) == -1)
> + FatalError("Could not allocate memory for add cmd");
> process_line (cmd);
> continue;
> }
> @@ -285,11 +324,11 @@ main(int argc, char *argv[])
> char *cmd;
> didAnything = True;
> if (++i >= argc) usage ();
> - cmd = chk_malloc (strlen ("add control = ") + strlen (argv[i]) + 1);
> - (void) sprintf (cmd, "add %s = %s",
> + if (asprintf (&cmd, "add %s = %s",
> ((arg[1] == 's') ? "shift" :
> ((arg[1] == 'l') ? "lock" :
> - "control")), argv[i]);
> + "control")), argv[i]) == -1)
> + FatalError("Could not allocate memory for remove cmd");
> process_line (cmd);
> continue;
> }
> diff --git a/xmodmap.h b/xmodmap.h
> index 1540b2a..686ee1b 100644
> --- a/xmodmap.h
> +++ b/xmodmap.h
> @@ -26,6 +26,10 @@ from The Open Group.
>
> */
>
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> extern const char *ProgramName;
> extern Display *dpy;
> extern int min_keycode, max_keycode;
> @@ -56,4 +60,3 @@ extern void PrintPointerMap(FILE *fp);
> extern int SetPointerMap(unsigned char *map, int n);
>
> extern void _X_NORETURN Exit(int status);
> -extern void *chk_malloc(size_t n_bytes);
More information about the xorg-devel
mailing list