Split dns_name_copy() into dns_name_copy() and dns_name_copynf()

The dns_name_copy() function followed two different semanitcs that was driven
whether the last argument was or wasn't NULL.  This commit splits the function
in two where now third argument to dns_name_copy() can't be NULL and
dns_name_copynf() doesn't have third argument.

(cherry picked from commit f7aef3738a)
This commit is contained in:
Ondřej Surý 2019-09-10 14:36:41 +02:00 committed by Mark Andrews
parent 660307283e
commit a1ef76cd78
3 changed files with 40 additions and 19 deletions

View file

@ -1240,16 +1240,19 @@ dns_name_settotextfilter(dns_name_totextfilter_t proc);
isc_result_t
dns_name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target);
void
dns_name_copynf(const dns_name_t *source, dns_name_t *dest);
/*%<
* Makes 'dest' refer to a copy of the name in 'source'. The data are
* either copied to 'target' or the dedicated buffer in 'dest'.
* Makes 'dest' refer to a copy of the name in 'source'. The data are either
* copied to 'target' or in case of dns_name_copyfixed the dedicated buffer in
* 'dest'.
*
* Requires:
* \li 'source' is a valid name.
*
* \li 'dest' is an initialized name with a dedicated buffer.
*
* \li 'target' is NULL or an initialized buffer.
* \li 'target' is an initialized buffer.
*
* \li Either dest has a dedicated buffer or target != NULL.
*

View file

@ -2456,50 +2456,46 @@ dns_name_fromstring2(dns_name_t *target, const char *src,
return (result);
}
static
isc_result_t
dns_name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target) {
name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target) {
unsigned char *ndata;
/*
* Make dest a copy of source.
*/
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(dest));
REQUIRE(target != NULL || dest->buffer != NULL);
if (target == NULL) {
target = dest->buffer;
isc_buffer_clear(dest->buffer);
}
REQUIRE(BINDABLE(dest));
/*
* Set up.
*/
if (target->length - target->used < source->length)
if (target->length - target->used < source->length) {
return (ISC_R_NOSPACE);
}
ndata = (unsigned char *)target->base + target->used;
dest->ndata = target->base;
if (source->length != 0)
if (source->length != 0) {
memmove(ndata, source->ndata, source->length);
}
dest->ndata = ndata;
dest->labels = source->labels;
dest->length = source->length;
if ((source->attributes & DNS_NAMEATTR_ABSOLUTE) != 0)
if ((source->attributes & DNS_NAMEATTR_ABSOLUTE) != 0) {
dest->attributes = DNS_NAMEATTR_ABSOLUTE;
else
} else {
dest->attributes = 0;
}
if (dest->labels > 0 && dest->offsets != NULL) {
if (source->offsets != NULL)
if (source->offsets != NULL) {
memmove(dest->offsets, source->offsets, source->labels);
else
} else {
set_offsets(dest, dest->offsets, NULL);
}
}
isc_buffer_add(target, dest->length);
@ -2507,6 +2503,27 @@ dns_name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target)
return (ISC_R_SUCCESS);
}
isc_result_t
dns_name_copy(const dns_name_t *source, dns_name_t *dest, isc_buffer_t *target)
{
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(dest));
REQUIRE(target != NULL);
return (name_copy(source, dest, target));
}
void
dns_name_copynf(const dns_name_t *source, dns_name_t *dest)
{
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(dest));
REQUIRE(dest->buffer != NULL);
isc_buffer_clear(dest->buffer);
RUNTIME_CHECK(name_copy(source, dest, dest->buffer) == ISC_R_SUCCESS);
}
void
dns_name_destroy(void) {
RUNTIME_CHECK(isc_once_do(&once, thread_key_mutex_init)

View file

@ -540,6 +540,7 @@ dns_name_clone
dns_name_compare
dns_name_concatenate
dns_name_copy
dns_name_copynf
dns_name_countlabels
dns_name_destroy
dns_name_digest