Commit a7472cb3288c2323830462f53706b013f33c444e

Stefan Sperling 2022-04-14T15:00:59

check return value of RB_INSERT; ok + memleak fix by op@

diff --git a/include/got_error.h b/include/got_error.h
index 15a2771..8ae16da 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -167,6 +167,7 @@
 #define GOT_ERR_NO_PATCH	149
 #define GOT_ERR_HUNK_FAILED	150
 #define GOT_ERR_PATCH_FAILED	151
+#define GOT_ERR_FILEIDX_DUP_ENTRY 152
 
 struct got_error {
         int code;
diff --git a/lib/error.c b/lib/error.c
index 19d6e55..29541c0 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -215,6 +215,7 @@ static const struct got_error got_errors[] = {
 	{ GOT_ERR_NO_PATCH, "no patch found" },
 	{ GOT_ERR_HUNK_FAILED, "hunk failed to apply" },
 	{ GOT_ERR_PATCH_FAILED, "patch failed to apply" },
+	{ GOT_ERR_FILEIDX_DUP_ENTRY, "duplicate file index entry" },
 };
 
 static struct got_custom_error {
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 6ea3dea..b05a813 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -265,7 +265,9 @@ add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
 	if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
 		return got_error(GOT_ERR_NO_SPACE);
 
-	RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
+	if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
+		return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
+
 	fileindex->nentries++;
 	return NULL;
 }
diff --git a/lib/object_idset.c b/lib/object_idset.c
index 37ea8e2..06f7347 100644
--- a/lib/object_idset.c
+++ b/lib/object_idset.c
@@ -102,7 +102,11 @@ got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
 	memcpy(&new->id, id, sizeof(new->id));
 	new->data = data;
 
-	RB_INSERT(got_object_idset_tree, &set->entries, new);
+	if (RB_INSERT(got_object_idset_tree, &set->entries, new) != NULL) {
+		free(new);
+		return got_error(GOT_ERR_OBJ_EXISTS);
+	}
+
 	set->totelem++;
 	return NULL;
 }