]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ndr/util push_dns_string: avoid unnecessary tallocs
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Sat, 25 Apr 2020 02:56:05 +0000 (14:56 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Thu, 16 Apr 2026 00:54:44 +0000 (00:54 +0000)
We know the components are all less than 64 bytes long.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14378

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andreas Schneider <asn@samba.org>
librpc/ndr/ndr_dns_utils.c

index 68d7800173ee761cd21162b89a52aa790aebd03b..9e901f4f12aac05694e5666311647882dd940dd1 100644 (file)
@@ -238,7 +238,11 @@ enum ndr_err_code ndr_push_dns_string_list(struct ndr_push *ndr,
 
        while (s && *s) {
                enum ndr_err_code ndr_err;
-               char *compname;
+               /*
+                * MAX_COMP_LEN + 1 for the length byte, + 1 extra for the
+                * NBT trailing-dot case where complen is incremented to 64.
+                */
+               uint8_t compname[MAX_COMP_LEN + 2] = {0, };
                size_t complen;
                uint32_t offset;
 
@@ -293,12 +297,6 @@ enum ndr_err_code ndr_push_dns_string_list(struct ndr_push *ndr,
                        complen++;
                }
 
-               compname = talloc_asprintf(ndr, "%c%*.*s",
-                                               (unsigned char)complen,
-                                               (unsigned char)complen,
-                                               (unsigned char)complen, s);
-               NDR_ERR_HAVE_NO_MEMORY(compname);
-
                /* remember the current component + the rest of the string
                 * so it can be reused later
                 */
@@ -308,9 +306,14 @@ enum ndr_err_code ndr_push_dns_string_list(struct ndr_push *ndr,
                }
 
                /* push just this component into the blob */
-               NDR_CHECK(ndr_push_bytes(ndr, (const uint8_t *)compname,
-                                        complen+1));
-               talloc_free(compname);
+               if (unlikely(complen > MAX_COMP_LEN)) {
+                       return ndr_push_error(ndr, NDR_ERR_STRING,
+                                             "component length is %zu, should be < %zu",
+                                             complen, sizeof(compname));
+               }
+               compname[0] = complen;
+               memcpy(compname + 1, s, complen);
+               NDR_CHECK(ndr_push_bytes(ndr, compname, complen + 1));
 
                s += complen;
                if (*s == '.') {