Add asprintf_safe helper function We only ever care about whether we error out or not, so let's wrap this into something more sane. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
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 */
diff --git a/test/common.c b/test/common.c
index d6619d4..c6f6644 100644
--- a/test/common.c
+++ b/test/common.c
@@ -165,7 +165,6 @@ test_key_seq(struct xkb_keymap *keymap, ...)
char *
test_get_path(const char *path_rel)
{
- int ret;
char *path;
const char *srcdir;
@@ -176,9 +175,9 @@ test_get_path(const char *path_rel)
if (path_rel[0] == '/')
return strdup(path_rel);
- ret = asprintf(&path, "%s/test/data%s%s", srcdir,
- path_rel[0] ? "/" : "", path_rel);
- if (ret < 0) {
+ path = asprintf_safe("%s/test/data%s%s", srcdir,
+ path_rel[0] ? "/" : "", path_rel);
+ if (!path) {
fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
return NULL;
}
diff --git a/test/compose.c b/test/compose.c
index 37d33bb..5ba5751 100644
--- a/test/compose.c
+++ b/test/compose.c
@@ -485,18 +485,16 @@ static void
test_include(struct xkb_context *ctx)
{
char *path, *table_string;
- int ret;
path = test_get_path("compose/en_US.UTF-8/Compose");
assert(path);
/* We don't have a mechanism to change the include paths like we
* have for keymaps. So we must include the full path. */
- ret = asprintf(&table_string,
- "<dead_tilde> <space> : \"foo\" X\n"
- "include \"%s\"\n"
- "<dead_tilde> <dead_tilde> : \"bar\" Y\n", path);
- assert(ret >= 0);
+ table_string = asprintf_safe("<dead_tilde> <space> : \"foo\" X\n"
+ "include \"%s\"\n"
+ "<dead_tilde> <dead_tilde> : \"bar\" Y\n", path);
+ assert(table_string);
assert(test_compose_seq_buffer(ctx, table_string,
/* No conflict. */
diff --git a/test/context.c b/test/context.c
index 2f5fd37..f91be54 100644
--- a/test/context.c
+++ b/test/context.c
@@ -85,8 +85,8 @@ static const char *makedir(const char *parent, const char *path)
char *dirname;
int err;
- err = asprintf(&dirname, "%s/%s", parent, path);
- assert(err >= 0);
+ dirname = asprintf_safe("%s/%s", parent, path);
+ assert(dirname);
err = mkdir(dirname, 0777);
assert(err == 0);
diff --git a/test/registry.c b/test/registry.c
index 68e74d0..fc5f6da 100644
--- a/test/registry.c
+++ b/test/registry.c
@@ -104,8 +104,8 @@ test_create_rules(const char *ruleset,
int rc;
FILE *fp;
- rc = asprintf(&tmpdir, "/tmp/%s.%d.XXXXXX", ruleset, iteration++);
- assert(rc > 0);
+ tmpdir = asprintf_safe("/tmp/%s.%d.XXXXXX", ruleset, iteration++);
+ assert(tmpdir);
assert(mkdtemp(tmpdir) == tmpdir);
rc = snprintf_safe(buf, sizeof(buf), "%s/rules", tmpdir);