Commit d4b78a5fca47c27aac4cea0220ad595c3312807f

Peter Hutterer 2020-07-10T15:01:31

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>

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) {