From e1f0aed03434e9f49208da8c7f617855665d6bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Tue, 30 Oct 2018 13:33:25 +0100 Subject: [PATCH] Fix isc_buffer_copyregion() for auto-reallocated buffers While isc_buffer_copyregion() calls isc_buffer_reserve() to ensure the target buffer will have enough available space to append the contents of the source region to it, the variables used for subsequently checking available space are not updated accordingly after that call. This prevents isc_buffer_copyregion() from working as expected for auto-reallocated buffers: ISC_R_NOSPACE will be returned if enough space is not already available in the target buffer before it is reallocated. Fix by calling isc_buffer_used() and isc_buffer_availablelength() directly instead of assigning their return values to local variables. --- lib/isc/buffer.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/isc/buffer.c b/lib/isc/buffer.c index 5eb2620c70..987795be12 100644 --- a/lib/isc/buffer.c +++ b/lib/isc/buffer.c @@ -514,27 +514,24 @@ isc_buffer_dup(isc_mem_t *mctx, isc_buffer_t **dstp, const isc_buffer_t *src) { isc_result_t isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r) { - unsigned char *base; - unsigned int available; isc_result_t result; REQUIRE(ISC_BUFFER_VALID(b)); REQUIRE(r != NULL); - /* - * XXXDCL - */ - base = isc_buffer_used(b); - available = isc_buffer_availablelength(b); if (ISC_UNLIKELY(b->autore)) { result = isc_buffer_reserve(&b, r->length); - if (result != ISC_R_SUCCESS) + if (result != ISC_R_SUCCESS) { return (result); + } } - if (r->length > available) + + if (r->length > isc_buffer_availablelength(b)) { return (ISC_R_NOSPACE); + } + if (r->length > 0U) { - memmove(base, r->base, r->length); + memmove(isc_buffer_used(b), r->base, r->length); b->used += r->length; }