xkbcomp: simplify buffer handling in the include handling Don't do the realloc dance, just asprintf to the buffer and move on. The check is likely pointless anyway, if we run out of asprintf size, log_error will probably blow up as well. 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
diff --git a/src/xkbcomp/include.c b/src/xkbcomp/include.c
index 8b97558..556b438 100644
--- a/src/xkbcomp/include.c
+++ b/src/xkbcomp/include.c
@@ -203,39 +203,25 @@ FindFileInXkbPath(struct xkb_context *ctx, const char *name,
FILE *file = NULL;
char *buf = NULL;
const char *typeDir;
- size_t buf_size = 0, typeDirLen, name_len;
typeDir = DirectoryForInclude(type);
- typeDirLen = strlen(typeDir);
- name_len = strlen(name);
for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
- size_t new_buf_size = strlen(xkb_context_include_path_get(ctx, i)) +
- typeDirLen + name_len + 3;
- int ret;
- if (new_buf_size > buf_size) {
- void *buf_new = realloc(buf, new_buf_size);
- if (buf_new) {
- buf_size = new_buf_size;
- buf = buf_new;
- } else {
- log_err(ctx, "Cannot realloc for name (%s/%s/%s)\n",
- xkb_context_include_path_get(ctx, i), typeDir, name);
- continue;
- }
- }
- ret = snprintf(buf, buf_size, "%s/%s/%s",
- xkb_context_include_path_get(ctx, i),
- typeDir, name);
- if (ret < 0) {
- log_err(ctx, "snprintf error (%s/%s/%s)\n",
+ buf = asprintf_safe("%s/%s/%s", xkb_context_include_path_get(ctx, i),
+ typeDir, name);
+ if (!buf) {
+ log_err(ctx, "Failed to alloc buffer for (%s/%s/%s)\n",
xkb_context_include_path_get(ctx, i), typeDir, name);
continue;
}
file = fopen(buf, "rb");
- if (file)
+ if (!file) {
+ free(buf);
+ buf = NULL;
+ } else {
break;
+ }
}
if (!file) {