diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compose/paths.c | 13 | ||||
-rw-r--r-- | src/context.c | 13 | ||||
-rw-r--r-- | src/registry.c | 13 | ||||
-rw-r--r-- | src/utils.h | 32 |
4 files changed, 47 insertions, 24 deletions
diff --git a/src/compose/paths.c b/src/compose/paths.c index f37c759..19eafa4 100644 --- a/src/compose/paths.c +++ b/src/compose/paths.c @@ -25,6 +25,7 @@ #include "utils.h" #include "paths.h" +#include "utils.h" enum resolve_name_direction { LEFT_TO_RIGHT, @@ -151,19 +152,13 @@ get_xcomposefile_path(void) char * get_home_xcompose_file_path(void) { - int ret; const char *home; - char *path; home = secure_getenv("HOME"); if (!home) return NULL; - ret = asprintf(&path, "%s/.XCompose", home); - if (ret <0) - return NULL; - - return path; + return asprintf_safe("%s/.XCompose", home); } char * @@ -195,10 +190,8 @@ get_locale_compose_file_path(const char *locale) } else { const char *xlocaledir = get_xlocaledir_path(); - int ret = asprintf(&path, "%s/%s", xlocaledir, resolved); + path = asprintf_safe("%s/%s", xlocaledir, resolved); free(resolved); - if (ret < 0) - return NULL; } return path; diff --git a/src/context.c b/src/context.c index 768fe5c..ef9b774 100644 --- a/src/context.c +++ b/src/context.c @@ -98,30 +98,29 @@ xkb_context_include_path_append_default(struct xkb_context *ctx) { const char *home, *xdg, *root; char *user_path; - int err; int ret = 0; home = secure_getenv("HOME"); xdg = secure_getenv("XDG_CONFIG_HOME"); if (xdg != NULL) { - err = asprintf(&user_path, "%s/xkb", xdg); - if (err >= 0) { + user_path = asprintf_safe("%s/xkb", xdg); + if (user_path) { ret |= xkb_context_include_path_append(ctx, user_path); free(user_path); } } else if (home != NULL) { /* XDG_CONFIG_HOME fallback is $HOME/.config/ */ - err = asprintf(&user_path, "%s/.config/xkb", home); - if (err >= 0) { + user_path = asprintf_safe("%s/.config/xkb", home); + if (user_path) { ret |= xkb_context_include_path_append(ctx, user_path); free(user_path); } } if (home != NULL) { - err = asprintf(&user_path, "%s/.xkb", home); - if (err >= 0) { + user_path = asprintf_safe("%s/.xkb", home); + if (user_path) { ret |= xkb_context_include_path_append(ctx, user_path); free(user_path); } diff --git a/src/registry.c b/src/registry.c index 19e004b..956d850 100644 --- a/src/registry.c +++ b/src/registry.c @@ -585,7 +585,6 @@ rxkb_context_include_path_append_default(struct rxkb_context *ctx) { const char *home, *xdg, *root; char *user_path; - int err; bool ret = false; if (ctx->context_state != CONTEXT_NEW) { @@ -597,23 +596,23 @@ rxkb_context_include_path_append_default(struct rxkb_context *ctx) xdg = secure_getenv("XDG_CONFIG_HOME"); if (xdg != NULL) { - err = asprintf(&user_path, "%s/xkb", xdg); - if (err >= 0) { + user_path = asprintf_safe("%s/xkb", xdg); + if (user_path) { ret |= rxkb_context_include_path_append(ctx, user_path); free(user_path); } } else if (home != NULL) { /* XDG_CONFIG_HOME fallback is $HOME/.config/ */ - err = asprintf(&user_path, "%s/.config/xkb", home); - if (err >= 0) { + user_path = asprintf_safe("%s/.config/xkb", home); + if (user_path) { ret |= rxkb_context_include_path_append(ctx, user_path); free(user_path); } } if (home != NULL) { - err = asprintf(&user_path, "%s/.xkb", home); - if (err >= 0) { + user_path = asprintf_safe("%s/.xkb", home); + if (user_path) { ret |= rxkb_context_include_path_append(ctx, user_path); free(user_path); } diff --git a/src/utils.h b/src/utils.h index 67080d0..d9827c0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -313,4 +313,36 @@ snprintf_safe(char *buf, size_t sz, const char *format, ...) return rc >= 0 && (size_t)rc < sz; } +static inline char * +ATTR_PRINTF(1, 0) +vasprintf_safe(const char *fmt, va_list args) +{ + char *str; + int len; + + len = vasprintf(&str, fmt, args); + + if (len == -1) + return NULL; + + return str; +} + +/** + * A version of asprintf that returns the allocated string or NULL on error. + */ +static inline char * +ATTR_PRINTF(1, 2) +asprintf_safe(const char *fmt, ...) +{ + va_list args; + char *str; + + va_start(args, fmt); + str = vasprintf_safe(fmt, args); + va_end(args); + + return str; +} + #endif /* UTILS_H */ |