From: Gary Lockyer Date: Mon, 12 Mar 2018 19:10:54 +0000 (+1300) Subject: ldb index: Fix truncation key length calculation X-Git-Tag: ldb-1.4.0~708 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d161a6dcfef09ad939df76c5c77eb8b7519a9326;p=thirdparty%2Fsamba.git ldb index: Fix truncation key length calculation Signed-off-by: Gary Lockyer Reviewed-by: Andrew Bartlett --- diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c index 076db10f2dd..ed28c4f642f 100644 --- a/lib/ldb/ldb_tdb/ldb_index.c +++ b/lib/ldb/ldb_tdb/ldb_index.c @@ -848,6 +848,19 @@ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb, unsigned indx_len = 0; unsigned frmt_len = 0; + if (max_key_length < 4) { + ldb_asprintf_errstring( + ldb, + __location__ ": max_key_length of (%u) < 4", + max_key_length); + return NULL; + } + /* + * ltdb_key_dn() makes something 4 bytes longer, it adds a leading + * "DN=" and a trailing string terninator + */ + max_key_length -= 4; + if (attr[0] == '@') { attr_for_dn = attr; v = *value; @@ -911,12 +924,13 @@ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb, if (should_b64_encode) { unsigned vstr_len = 0; char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length); + unsigned num_separators = 3; if (!vstr) { talloc_free(attr_folded); return NULL; } vstr_len = strlen(vstr); - key_len = 3 + indx_len + attr_len + vstr_len; + key_len = num_separators + indx_len + attr_len + vstr_len; if (key_len > max_key_length) { unsigned excess = key_len - max_key_length; frmt_len = vstr_len - excess; @@ -943,7 +957,8 @@ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb, } talloc_free(vstr); } else { - key_len = 2 + indx_len + attr_len + (int)v.length; + unsigned num_separators = 2; + key_len = num_separators + indx_len + attr_len + (int)v.length; if (key_len > max_key_length) { unsigned excess = key_len - max_key_length; frmt_len = v.length - excess; diff --git a/lib/ldb/tests/python/index.py b/lib/ldb/tests/python/index.py index e9eaaf059e1..1d66ee930d1 100755 --- a/lib/ldb/tests/python/index.py +++ b/lib/ldb/tests/python/index.py @@ -113,11 +113,14 @@ class MaxIndexKeyLengthTests(LdbBaseTest): super(MaxIndexKeyLengthTests, self).setUp() self.testdir = tempdir() self.filename = os.path.join(self.testdir, "key_len_test.ldb") - # Note that the maximum key length is set to 50 + # Note that the maximum key length is set to 54 + # This accounts for the 4 bytes added by the dn formatting + # a leading dn=, and a trailing zero terminator + # self.l = ldb.Ldb(self.url(), options=[ "modules:rdn_name", - "max_key_len_for_self_test:50"]) + "max_key_len_for_self_test:54"]) self.l.add({"dn": "@ATTRIBUTES", "uniqueThing": "UNIQUE_INDEX"}) self.l.add({"dn": "@INDEXLIST",