commit 49e8084f17c186e6b7188980436c524822c0be12571f8f061abcedd0b8f02991 from: Stefan Sperling date: Tue Feb 24 20:59:40 2026 UTC avoid doing asprintf/free per tree entry in got_pack_load_tree_entries() Allocate one path buffer and keep growing it as needed. For directories we still need to copy the path. But blobs can use the path buffer directly. ok op@ commit - 1b4a5311e029af3216154de43599c34a9cf5f0eb01708564bd49ae836a846998 commit + 49e8084f17c186e6b7188980436c524822c0be12571f8f061abcedd0b8f02991 blob - b3782a72b716bd9b1ad93009e1293ea85370f35d97c65aca9b5b180c956a991a blob + f3d4a58b959613b05d584b861277117559333ecc4ec25b8a47392da7f9bf7528 --- lib/pack_create.c +++ lib/pack_create.c @@ -778,7 +778,7 @@ got_pack_load_tree_entries(struct got_object_id_queue { const struct got_error *err; char *p = NULL; - int i; + int i, ret, psize = PATH_MAX; (*ntrees)++; err = got_pack_report_progress(progress_cb, progress_arg, rl, @@ -786,6 +786,11 @@ got_pack_load_tree_entries(struct got_object_id_queue if (err) return err; + p = malloc(psize); + if (p == NULL) + return got_error_from_errno("malloc"); + p[0] = '\0'; + for (i = 0; i < got_object_tree_get_nentries(tree); i++) { struct got_tree_entry *e = got_object_tree_get_entry(tree, i); struct got_object_id *id = got_tree_entry_get_id(e); @@ -809,20 +814,47 @@ got_pack_load_tree_entries(struct got_object_id_queue if (ids == NULL && S_ISDIR(mode)) continue; - if (asprintf(&p, "%s%s%s", dpath, + ret = snprintf(p, psize, "%s%s%s", dpath, got_path_is_root_dir(dpath) ? "" : "/", - got_tree_entry_get_name(e)) == -1) { - err = got_error_from_errno("asprintf"); + got_tree_entry_get_name(e)); + if (ret == -1) { + err = got_error_from_errno("snprintf"); break; } + if (ret >= psize) { + char *s; + s = realloc(p, ret + 1); + if (s == NULL) { + err = got_error_from_errno("realloc"); + break; + } + p = s; + psize = ret + 1; + + ret = snprintf(p, psize, "%s%s%s", dpath, + got_path_is_root_dir(dpath) ? "" : "/", + got_tree_entry_get_name(e)); + if (ret == -1) { + err = got_error_from_errno("snprintf"); + break; + } + if (ret >= psize) { /* should not happen */ + err = got_error(GOT_ERR_NO_SPACE); + break; + } + } + if (S_ISDIR(mode)) { struct got_object_qid *qid; err = got_object_qid_alloc(&qid, id); if (err) break; - qid->data = p; - p = NULL; + qid->data = strdup(p); + if (qid->data == NULL) { + err = got_error_from_errno("strdup"); + break; + } STAILQ_INSERT_TAIL(ids, qid, entry); } else if (S_ISREG(mode) || S_ISLNK(mode)) { err = got_pack_add_object(want_meta, @@ -832,11 +864,6 @@ got_pack_load_tree_entries(struct got_object_id_queue progress_cb, progress_arg, rl); if (err) break; - free(p); - p = NULL; - } else { - free(p); - p = NULL; } }