From: Willem Toorop Date: Mon, 19 Jan 2026 14:16:56 +0000 (+0100) Subject: reserve enough space for str in ldns_pkt2str_fmt X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a5a938f358e0037328f7091aac9f5604b8bf7ec;p=thirdparty%2Fldns.git reserve enough space for str in ldns_pkt2str_fmt Also, let ldns_pkt_print_fmt report the error if converting the host format to presentation format failed. --- diff --git a/Changelog b/Changelog index 6106b66b..bd5290c1 100644 --- a/Changelog +++ b/Changelog @@ -4,6 +4,8 @@ * Fix memory leak in ldns-notify * Prevent "null pointer pass as 2nd argument to memcpy" runtime error, when size is 0 + * Fix to reserve enough space to convert the largest packets to + presentation format in ldns_pkt2str_fmt. Thanks Peter Kästle 1.9.0 2025-12-04 * PR #246: Make ldns_calc_keytag() available for CDNSKEY RR diff --git a/host2str.c b/host2str.c index 9f2795af..efa1bf87 100644 --- a/host2str.c +++ b/host2str.c @@ -3374,24 +3374,33 @@ ldns_rr2str(const ldns_rr *rr) } char * -ldns_pkt2str_fmt(const ldns_output_format *fmt, const ldns_pkt *pkt) +ldns_pkt2str_fmt_status(const ldns_output_format *fmt, const ldns_pkt *pkt, + ldns_status *return_status) { char *result = NULL; - ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN); + ldns_status s; + ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN * 5); if (!tmp_buffer) { return NULL; } - if (ldns_pkt2buffer_str_fmt(tmp_buffer, fmt, pkt) - == LDNS_STATUS_OK) { + if ((s = ldns_pkt2buffer_str_fmt(tmp_buffer, fmt, pkt)) + == LDNS_STATUS_OK) { /* export and return string, destroy rest */ result = ldns_buffer_export2str(tmp_buffer); } - ldns_buffer_free(tmp_buffer); + if (return_status) + *return_status = s; return result; } +char * +ldns_pkt2str_fmt(const ldns_output_format *fmt, const ldns_pkt *pkt) +{ + return ldns_pkt2str_fmt_status(fmt, pkt, NULL); +} + char * ldns_pkt2str(const ldns_pkt *pkt) { @@ -3485,11 +3494,13 @@ void ldns_pkt_print_fmt(FILE *output, const ldns_output_format *fmt, const ldns_pkt *pkt) { - char *str = ldns_pkt2str_fmt(fmt, pkt); + ldns_status s = LDNS_STATUS_OK; + char *str = ldns_pkt2str_fmt_status(fmt, pkt, &s); if (str) { fprintf(output, "%s", str); } else { - fprintf(output, ";Unable to convert packet to string\n"); + fprintf(output, ";Unable to convert packet to string%s%s\n", + (s ? ": " : ""), (s ? ldns_get_errorstr_by_id(s) : "")); } LDNS_FREE(str); }