util: check asprintf return

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-11-10 10:35:53 +01:00
parent c13654f08d
commit 83eadb46cb
6 changed files with 23 additions and 27 deletions

View File

@ -19,7 +19,6 @@
* *
*/ */
#define _GNU_SOURCE
#include <getopt.h> #include <getopt.h>
#include <locale.h> #include <locale.h>

View File

@ -19,13 +19,8 @@
* *
*/ */
/* asprintf */
#define _GNU_SOURCE
#include <glib.h> #include <glib.h>
#include <stdio.h>
#include "common/markup.h" #include "common/markup.h"
/** Callback to invoke when the opening tag of an element is seen. /** Callback to invoke when the opening tag of an element is seen.

View File

@ -21,7 +21,6 @@
*/ */
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>

View File

@ -23,11 +23,15 @@
#ifndef AWESOME_COMMON_UTIL_H #ifndef AWESOME_COMMON_UTIL_H
#define AWESOME_COMMON_UTIL_H #define AWESOME_COMMON_UTIL_H
/* asprintf */
#define _GNU_SOURCE
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <stdio.h>
#if !(defined (__FreeBSD__) || defined(__OpenBSD__)) #if !(defined (__FreeBSD__) || defined(__OpenBSD__))
#include <alloca.h> #include <alloca.h>
@ -352,5 +356,11 @@ const char * name_func_rlookup(void *, const name_func_link_t *);
void a_exec(const char *); void a_exec(const char *);
char ** a_strsplit(const char *, ssize_t, 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 #endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -19,10 +19,7 @@
* *
*/ */
/* asprintf() */ #include "common/util.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include <xcb/xcb_atom.h> #include <xcb/xcb_atom.h>
@ -31,7 +28,6 @@
/* CURSORFONT */ /* CURSORFONT */
#include <X11/Xlibint.h> #include <X11/Xlibint.h>
#include "common/util.h"
#include "common/xutil.h" #include "common/xutil.h"
#include "common/atoms.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 /* Extensions generally provide their own requests so we just
* store the request code */ * store the request code */
if(err->request_code >= (sizeof(xutil_label_request) / sizeof(char *))) 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 else
err->request_label = a_strdup(xutil_label_request[err->request_code]); err->request_label = a_strdup(xutil_label_request[err->request_code]);
/* Extensions may also define their own errors, so just store the /* Extensions may also define their own errors, so just store the
* error_code */ * error_code */
if(e->error_code >= (sizeof(xutil_label_error) / sizeof(char *))) 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 else
err->error_label = a_strdup(xutil_label_error[e->error_code]); err->error_label = a_strdup(xutil_label_error[e->error_code]);

23
luaa.c
View File

@ -19,11 +19,9 @@
* *
*/ */
/* asprintf */ #include "common/util.h"
#define _GNU_SOURCE
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
@ -31,7 +29,6 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <ev.h> #include <ev.h>
@ -884,16 +881,16 @@ luaA_init(void)
if((dir = getenv("XDG_CONFIG_HOME"))) if((dir = getenv("XDG_CONFIG_HOME")))
{ {
char *path; 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); luaA_dostring(globalconf.L, path);
p_delete(&path); p_delete(&path);
} }
else else
{ {
char *path, *homedir = getenv("HOME"); char *path, *homedir = getenv("HOME");
asprintf(&path, a_asprintf(&path,
"package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"", "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
homedir, homedir); homedir, homedir);
luaA_dostring(globalconf.L, path); luaA_dostring(globalconf.L, path);
p_delete(&path); p_delete(&path);
} }
@ -909,8 +906,8 @@ luaA_init(void)
for(buf = xdg_files; *buf; buf++) for(buf = xdg_files; *buf; buf++)
{ {
char *confpath; char *confpath;
asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
*buf, *buf); *buf, *buf);
luaA_dostring(globalconf.L, confpath); luaA_dostring(globalconf.L, confpath);
p_delete(&confpath); p_delete(&confpath);
} }
@ -971,9 +968,9 @@ luaA_parserc(const char *confpatharg, bool run)
} }
if((confdir = getenv("XDG_CONFIG_HOME"))) if((confdir = getenv("XDG_CONFIG_HOME")))
asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir); a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
else 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 */ /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
if(luaA_loadrc(confpath, run)) if(luaA_loadrc(confpath, run))
@ -996,7 +993,7 @@ luaA_parserc(const char *confpatharg, bool run)
for(buf = xdg_files; *buf && !ret; buf++) 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)) if(luaA_loadrc(confpath, run))
{ {
ret = true; ret = true;