Fix overflow issue
[platform/upstream/libxkbcommon.git] / src / registry.c
index 0c42a3b..d6181e1 100644 (file)
@@ -564,18 +564,14 @@ rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path)
         return false;
     }
 
-    tmp = strdup(path);
-    if (!tmp)
-        goto err;
-
     err = stat(path, &stat_buf);
     if (err != 0)
-        goto err;
+        return false;
     if (!S_ISDIR(stat_buf.st_mode))
-        goto err;
+        return false;
 
     if (!check_eaccess(path, R_OK | X_OK))
-        goto err;
+        return false;
 
     /* Pre-filter for the 99.9% case - if we can't assemble the default ruleset
      * path, complain here instead of during parsing later. The niche cases
@@ -583,22 +579,22 @@ rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path)
      */
     if (!snprintf_safe(rules, sizeof(rules), "%s/rules/%s.xml",
                        path, DEFAULT_XKB_RULES))
-        goto err;
+        return false;
+
+    tmp = strdup(path);
+    if (!tmp)
+        return false;
 
     darray_append(ctx->includes, tmp);
 
     return true;
-
-err:
-    free(tmp);
-    return false;
 }
 
 XKB_EXPORT bool
 rxkb_context_include_path_append_default(struct rxkb_context *ctx)
 {
     const char *home, *xdg, *root, *extra;
-    char *user_path;
+    char user_path[PATH_MAX];
     bool ret = false;
 
     if (ctx->context_state != CONTEXT_NEW) {
@@ -610,26 +606,17 @@ rxkb_context_include_path_append_default(struct rxkb_context *ctx)
 
     xdg = rxkb_context_getenv(ctx, "XDG_CONFIG_HOME");
     if (xdg != NULL) {
-        user_path = asprintf_safe("%s/xkb", xdg);
-        if (user_path) {
+        if (snprintf_safe(user_path, sizeof(user_path), "%s/xkb", xdg))
             ret |= rxkb_context_include_path_append(ctx, user_path);
-            free(user_path);
-        }
     } else if (home != NULL) {
         /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
-        user_path = asprintf_safe("%s/.config/xkb", home);
-        if (user_path) {
+        if (snprintf_safe(user_path, sizeof(user_path), "%s/.config/xkb", home))
             ret |= rxkb_context_include_path_append(ctx, user_path);
-            free(user_path);
-        }
     }
 
     if (home != NULL) {
-        user_path = asprintf_safe("%s/.xkb", home);
-        if (user_path) {
+        if (snprintf_safe(user_path, sizeof(user_path), "%s/.xkb", home))
             ret |= rxkb_context_include_path_append(ctx, user_path);
-            free(user_path);
-        }
     }
 
     extra = rxkb_context_getenv(ctx, "XKB_CONFIG_EXTRA_PATH");