commit e1f5d7cf67ae80171a7360796fb87c328f22cff3 from: Stefan Sperling date: Thu May 19 07:36:33 2022 UTC avoid malloc/free for duplicate check in got_pathlists_insert() ok op@ commit - c9b75c7bd7036a6895bbc3eba2ed0369238ebab4 commit + e1f5d7cf67ae80171a7360796fb87c328f22cff3 blob - d0ec896bd7350a9da6048dc4c9ee1020412b2d56 blob + f4b3d337163db35814aa20bf256f110b9febeb41 --- lib/path.c +++ lib/path.c @@ -224,17 +224,11 @@ got_pathlist_insert(struct got_pathlist_entry **insert struct got_pathlist_head *pathlist, const char *path, void *data) { struct got_pathlist_entry *new, *pe; + size_t path_len = strlen(path); if (inserted) *inserted = NULL; - new = malloc(sizeof(*new)); - if (new == NULL) - return got_error_from_errno("malloc"); - new->path = path; - new->path_len = strlen(path); - new->data = data; - /* * Many callers will provide paths in a somewhat sorted order while * constructing a path list from inputs such as tree objects or @@ -244,21 +238,24 @@ got_pathlist_insert(struct got_pathlist_entry **insert */ pe = TAILQ_LAST(pathlist, got_pathlist_head); while (pe) { - int cmp = got_path_cmp(pe->path, new->path, - pe->path_len, new->path_len); - if (cmp == 0) { - free(new); /* duplicate */ - return NULL; - } else if (cmp < 0) { - TAILQ_INSERT_AFTER(pathlist, pe, new, entry); - if (inserted) - *inserted = new; - return NULL; - } + int cmp = got_path_cmp(pe->path, path, pe->path_len, path_len); + if (cmp == 0) + return NULL; /* duplicate */ + else if (cmp < 0) + break; pe = TAILQ_PREV(pe, got_pathlist_head, entry); } - TAILQ_INSERT_HEAD(pathlist, new, entry); + new = malloc(sizeof(*new)); + if (new == NULL) + return got_error_from_errno("malloc"); + new->path = path; + new->path_len = path_len; + new->data = data; + if (pe) + TAILQ_INSERT_AFTER(pathlist, pe, new, entry); + else + TAILQ_INSERT_HEAD(pathlist, new, entry); if (inserted) *inserted = new; return NULL;