From: Joseph Sutton Date: Thu, 26 Jan 2023 19:06:47 +0000 (+1300) Subject: ldb: Make ldb_msg_remove_attr O(n) X-Git-Tag: talloc-2.4.1~1455 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5f053711bd5b78f2eff035b4b287995ae286901;p=thirdparty%2Fsamba.git ldb: Make ldb_msg_remove_attr O(n) Previously it was O(n²). Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- diff --git a/lib/ldb/common/ldb_msg.c b/lib/ldb/common/ldb_msg.c index 9cd7998e21c..4146de185d7 100644 --- a/lib/ldb/common/ldb_msg.c +++ b/lib/ldb/common/ldb_msg.c @@ -1464,11 +1464,18 @@ void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element */ void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr) { - struct ldb_message_element *el; + unsigned int i; + unsigned int num_del = 0; - while ((el = ldb_msg_find_element(msg, attr)) != NULL) { - ldb_msg_remove_element(msg, el); + for (i = 0; i < msg->num_elements; ++i) { + if (ldb_attr_cmp(msg->elements[i].name, attr) == 0) { + ++num_del; + } else if (num_del) { + msg->elements[i - num_del] = msg->elements[i]; + } } + + msg->num_elements -= num_del; } /*