From 83eadb46cbe182ec913aaef35e3b9b6c8f6ca695 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 10 Nov 2008 10:35:53 +0100 Subject: [PATCH] util: check asprintf return Signed-off-by: Julien Danjou --- awesome.c | 1 - common/markup.c | 5 ----- common/util.c | 1 - common/util.h | 10 ++++++++++ common/xutil.c | 10 +++------- luaa.c | 23 ++++++++++------------- 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/awesome.c b/awesome.c index 43a9a737..174ecfb5 100644 --- a/awesome.c +++ b/awesome.c @@ -19,7 +19,6 @@ * */ -#define _GNU_SOURCE #include #include diff --git a/common/markup.c b/common/markup.c index e34c585a..6de94d7f 100644 --- a/common/markup.c +++ b/common/markup.c @@ -19,13 +19,8 @@ * */ -/* asprintf */ -#define _GNU_SOURCE - #include -#include - #include "common/markup.h" /** Callback to invoke when the opening tag of an element is seen. diff --git a/common/util.c b/common/util.c index c49588a1..1833e8b2 100644 --- a/common/util.c +++ b/common/util.c @@ -21,7 +21,6 @@ */ #include -#include #include #include #include diff --git a/common/util.h b/common/util.h index a90cbb4e..7dc3be8a 100644 --- a/common/util.h +++ b/common/util.h @@ -23,11 +23,15 @@ #ifndef AWESOME_COMMON_UTIL_H #define AWESOME_COMMON_UTIL_H +/* asprintf */ +#define _GNU_SOURCE + #include #include #include #include #include +#include #if !(defined (__FreeBSD__) || defined(__OpenBSD__)) #include @@ -352,5 +356,11 @@ const char * name_func_rlookup(void *, const name_func_link_t *); void a_exec(const char *); char ** a_strsplit(const char *, ssize_t, char); +#define a_asprintf(strp, fmt, ...) \ + do { \ + if(asprintf(strp, fmt, ## __VA_ARGS__) < 0) \ + abort(); \ + } while(0) + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/common/xutil.c b/common/xutil.c index 40891daa..296467bb 100644 --- a/common/xutil.c +++ b/common/xutil.c @@ -19,10 +19,7 @@ * */ -/* asprintf() */ -#define _GNU_SOURCE - -#include +#include "common/util.h" #include #include @@ -31,7 +28,6 @@ /* CURSORFONT */ #include -#include "common/util.h" #include "common/xutil.h" #include "common/atoms.h" @@ -319,14 +315,14 @@ xutil_error_init(const xcb_generic_error_t *e, xutil_error_t *err) /* Extensions generally provide their own requests so we just * store the request code */ if(err->request_code >= (sizeof(xutil_label_request) / sizeof(char *))) - asprintf(&err->request_label, "%d", err->request_code); + a_asprintf(&err->request_label, "%d", err->request_code); else err->request_label = a_strdup(xutil_label_request[err->request_code]); /* Extensions may also define their own errors, so just store the * error_code */ if(e->error_code >= (sizeof(xutil_label_error) / sizeof(char *))) - asprintf(&err->error_label, "%d", e->error_code); + a_asprintf(&err->error_label, "%d", e->error_code); else err->error_label = a_strdup(xutil_label_error[e->error_code]); diff --git a/luaa.c b/luaa.c index 6d79a5ac..66ee966d 100644 --- a/luaa.c +++ b/luaa.c @@ -19,11 +19,9 @@ * */ -/* asprintf */ -#define _GNU_SOURCE +#include "common/util.h" #include -#include #include #include #include @@ -31,7 +29,6 @@ #include #include #include -#include #include @@ -884,16 +881,16 @@ luaA_init(void) if((dir = getenv("XDG_CONFIG_HOME"))) { char *path; - asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir); + a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir); luaA_dostring(globalconf.L, path); p_delete(&path); } else { char *path, *homedir = getenv("HOME"); - asprintf(&path, - "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"", - homedir, homedir); + a_asprintf(&path, + "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"", + homedir, homedir); luaA_dostring(globalconf.L, path); p_delete(&path); } @@ -909,8 +906,8 @@ luaA_init(void) for(buf = xdg_files; *buf; buf++) { char *confpath; - asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", - *buf, *buf); + a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", + *buf, *buf); luaA_dostring(globalconf.L, confpath); p_delete(&confpath); } @@ -971,9 +968,9 @@ luaA_parserc(const char *confpatharg, bool run) } if((confdir = getenv("XDG_CONFIG_HOME"))) - asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir); + a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir); else - asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME")); + a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME")); /* try to run XDG_CONFIG_HOME/awesome/rc.lua */ if(luaA_loadrc(confpath, run)) @@ -996,7 +993,7 @@ luaA_parserc(const char *confpatharg, bool run) for(buf = xdg_files; *buf && !ret; buf++) { - asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf); + a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf); if(luaA_loadrc(confpath, run)) { ret = true;