From 7fdd88e1be34f2e663752cdeb49bbde393d98bf4 Mon Sep 17 00:00:00 2001 From: Nick Porter Date: Mon, 21 Mar 2022 12:29:20 +0000 Subject: [PATCH] v4: Two minor changes to berval handling (#4427) * Allocate the correct length when extracting from bervals * Add fr_ldap_berval_to_value_str_shallow() --- src/lib/ldap/base.h | 16 ++++++++++++++++ src/lib/ldap/util.c | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/ldap/base.h b/src/lib/ldap/base.h index c7c5fcf9c10..e63303a8a5f 100644 --- a/src/lib/ldap/base.h +++ b/src/lib/ldap/base.h @@ -556,6 +556,22 @@ static inline void fr_ldap_berval_to_value_shallow(fr_value_box_t *value, struct fr_value_box_memdup_shallow(value, NULL, (uint8_t *)berval->bv_val, berval->bv_len, true); } +/** Inline function to copy pointer from a berval to a string value box + * + * Useful for printing contents of bervals known to contain strings in DEBUG output since + * they are not NULL terminated. + * + * @note This results in a shallow copy of the berval, so if the berval is freed + * the value box becomes invalidated. + * + * @param[out] value to write berval value to. + * @param[in] berval top copy pointer / length from. + */ +static inline void fr_ldap_berval_to_value_str_shallow(fr_value_box_t *value, struct berval *berval) +{ + fr_value_box_bstrndup_shallow(value, NULL, berval->bv_val, berval->bv_len, true); +} + /** Compare two ldap trunk structures on connection URI / DN * * @param[in] one first connection to compare. diff --git a/src/lib/ldap/util.c b/src/lib/ldap/util.c index 23e16050e31..a59f3917f7b 100644 --- a/src/lib/ldap/util.c +++ b/src/lib/ldap/util.c @@ -375,7 +375,7 @@ uint8_t *fr_ldap_berval_to_bin(TALLOC_CTX *ctx, struct berval const *in) { uint8_t *out; - out = talloc_array(ctx, uint8_t, in->bv_len + 1); + out = talloc_array(ctx, uint8_t, in->bv_len); if (!out) return NULL; memcpy(out, in->bv_val, in->bv_len); -- 2.47.3