]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
allow NULL compression context in dns_name_towire()
authorEvan Hunt <each@isc.org>
Sat, 22 Feb 2025 07:37:49 +0000 (23:37 -0800)
committerEvan Hunt <each@isc.org>
Tue, 25 Feb 2025 20:53:25 +0000 (12:53 -0800)
passing NULL as the compression context to dns_name_towire()
copies the uncompressed name data directly into the target buffer.

lib/dns/include/dns/name.h
lib/dns/name.c
tests/dns/name_test.c

index cf452104e00066ae257cd154e788838117e2df7b..07265f5ae5b57d4df79f6cf3ce35775bc18f3404 100644 (file)
@@ -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.
index 2641b536ac48cf5d96cc96bc674adb75c01ae47f..ba6e5693e60e9a0f146f5131d5bd4d347c2d1be1 100644 (file)
@@ -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);
index 0b39ba2fe50aa22badbbd6e045fe5860e50202bf..d598447ad3ab305f65174182f0e0bf1a448f29ef 100644 (file)
@@ -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);