diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index cf452104e0..07265f5ae5 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -772,7 +772,8 @@ dns_name_towire(const dns_name_t *name, dns_compress_t *cctx, isc_buffer_t *target); /*%< * Convert 'name' into wire format, compressing it as specified by the - * compression context 'cctx', and storing the result in 'target'. + * compression context 'cctx' (or leaving it uncompressed if 'cctx' is + * NULL), and storing the result in 'target'. * * Notes: * \li If compression is permitted, then the cctx table may be updated. diff --git a/lib/dns/name.c b/lib/dns/name.c index 2641b536ac..ba6e5693e6 100644 --- a/lib/dns/name.c +++ b/lib/dns/name.c @@ -1453,13 +1453,22 @@ dns_name_towire(const dns_name_t *name, dns_compress_t *cctx, /* * Convert 'name' into wire format, compressing it as specified by the - * compression context 'cctx', and storing the result in 'target'. + * compression context 'cctx' (or without compressing if 'cctx' + * is NULL), and storing the result in 'target'. */ REQUIRE(DNS_NAME_VALID(name)); - REQUIRE(cctx != NULL); REQUIRE(ISC_BUFFER_VALID(target)); + if (cctx == NULL) { + if (isc_buffer_availablelength(target) < name->length) { + return ISC_R_NOSPACE; + } + memmove(isc_buffer_used(target), name->ndata, name->length); + isc_buffer_add(target, name->length); + return ISC_R_SUCCESS; + } + compress = !name->attributes.nocompress && dns_compress_getpermitted(cctx); multi = compress && dns_compress_getmultiuse(cctx); diff --git a/tests/dns/name_test.c b/tests/dns/name_test.c index 0b39ba2fe5..d598447ad3 100644 --- a/tests/dns/name_test.c +++ b/tests/dns/name_test.c @@ -158,17 +158,23 @@ compress_test(const dns_name_t *name1, const dns_name_t *name2, ISC_R_SUCCESS); } else { /* Owner name compression */ - dns_compress_setmultiuse(cctx, true); + if (cctx != NULL) { + dns_compress_setmultiuse(cctx, true); + } assert_int_equal(dns_name_towire(name1, cctx, &source), ISC_R_SUCCESS); - dns_compress_setmultiuse(cctx, true); + if (cctx != NULL) { + dns_compress_setmultiuse(cctx, true); + } assert_int_equal(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS); assert_int_equal(dns_name_towire(name2, cctx, &source), ISC_R_SUCCESS); - dns_compress_setmultiuse(cctx, true); + if (cctx != NULL) { + dns_compress_setmultiuse(cctx, true); + } assert_int_equal(dns_name_towire(name3, cctx, &source), ISC_R_SUCCESS); } @@ -254,6 +260,12 @@ ISC_RUN_TEST_IMPL(compression) { r.length = sizeof(plain3); dns_name_fromregion(&name4, &r); + /* Test 0: no compression context */ + compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain, + sizeof(plain), NULL, DNS_DECOMPRESS_NEVER, true); + compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain, + sizeof(plain), NULL, DNS_DECOMPRESS_NEVER, false); + /* Test 1: off, rdata */ permitted = false; dns_compress_init(&cctx, mctx, 0);