Commit a2965dbecb29aa632bfb7346d3f970ae1545dde3

Stefan Sperling 2018-06-22T09:52:27

fix got_object_idset_remove_random() for single-element set

diff --git a/lib/object_idset.c b/lib/object_idset.c
index 234ba60..bfd1313 100644
--- a/lib/object_idset.c
+++ b/lib/object_idset.c
@@ -199,10 +199,13 @@ got_object_idset_remove_random(void **data, struct got_object_idset *set)
 	if (set->nelem == 0)
 		return got_error(GOT_ERR_NO_OBJ);
 
-	n = arc4random_uniform(set->nelem);
+	if (set->nelem == 1)
+		n = 0;
+	else
+		n = arc4random_uniform(set->nelem);
 	for (i = 0; i < nitems(set->entries); i++) {
 		TAILQ_FOREACH_SAFE(entry, &set->entries[i], entry, tmp) {
-			if (--n == 0) {
+			if (n == 0) {
 				TAILQ_REMOVE(&set->entries[i], entry, entry);
 				if (data)
 					*data = entry->data;
@@ -210,8 +213,8 @@ got_object_idset_remove_random(void **data, struct got_object_idset *set)
 				set->nelem--;
 				return NULL;
 			}
+			n--;
 		}
-
 	}
 
 	return got_error(GOT_ERR_NO_OBJ);