webui: apply category on add + allow no category

api_add now forwards the chosen category to the daemon (and fixes a
use-after-free reading it from the freed request). Mark issues #3, #8, #9
done in ISSUES.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ookami125 2026-06-21 23:34:50 -04:00
parent b633b7d216
commit 8295b8522c
2 changed files with 14 additions and 4 deletions

View file

@ -1358,11 +1358,16 @@ static void api_add(int fd, const char *body, size_t len) {
const char *data = json_string_value(json_object_get(req, "data"));
const char *save_path = json_string_value(json_object_get(req, "savePath"));
/* The UI parses the .torrent client-side and sends a display name; the
* daemon only knows the temp upload path, so we keep the name here. */
* daemon only knows the temp upload path, so we keep the name here. Copy
* out of `req` since it is freed before these are used below. */
const char *display = json_string_value(json_object_get(req, "name"));
char display_name[256] = {0};
if (display && *display)
snprintf(display_name, sizeof display_name, "%s", display);
const char *category = json_string_value(json_object_get(req, "category"));
char category_name[256] = {0};
if (category && *category)
snprintf(category_name, sizeof category_name, "%s", category);
if (!save_path || !*save_path) save_path = ".";
if (!source) source = magnet;
if ((!source || !*source) && (!data || !*data)) {
@ -1388,6 +1393,10 @@ static void api_add(int fd, const char *body, size_t len) {
/* Forward the display name so the daemon persists it for restore. */
if (display_name[0])
json_object_set_new(params, "name", json_string(display_name));
/* An empty category means "Uncategorized"; only forward a real one. The
* daemon (spawn_torrent) reads "category" and persists it. */
if (category_name[0])
json_object_set_new(params, "category", json_string(category_name));
json_t *result = rpc_call_json("add_torrent", params);
json_decref(params);
json_decref(req);
@ -1397,6 +1406,7 @@ static void api_add(int fd, const char *body, size_t len) {
}
uint64_t new_id = json_u64(result, "torrent_id");
if (display_name[0]) store_set_name(new_id, display_name);
if (category_name[0]) store_set_category(new_id, category_name);
char id[32];
snprintf(id, sizeof id, "%llu", (unsigned long long)new_id);
json_t *reply = json_pack("{s:b,s:s}", "ok", 1, "hash", id);