From 0d2d9b363730415490a11eeaadc554e81fa27b7b Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Tue, 6 Dec 2022 19:36:53 -0500 Subject: [PATCH] Fix ldns_rdf2buffer_str_wks not to use after free. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #201: endprotoent() needs not to be called until we’re finished with the protoent struct. (Found by running the test suite on OpenBSD.) --- host2str.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/host2str.c b/host2str.c index f8bce99d..deeeaf63 100644 --- a/host2str.c +++ b/host2str.c @@ -835,10 +835,6 @@ ldns_rdf2buffer_str_wks(ldns_buffer *output, const ldns_rdf *rdf) ldns_buffer_printf(output, "%u ", protocol_nr); } -#ifdef HAVE_ENDPROTOENT - endprotoent(); -#endif - for (current_service = 0; current_service < (ldns_rdf_size(rdf)-1)*8; current_service++) { if (ldns_get_bit(&(ldns_rdf_data(rdf)[1]), current_service)) { @@ -856,6 +852,11 @@ ldns_rdf2buffer_str_wks(ldns_buffer *output, const ldns_rdf *rdf) /* exit from loop before integer overflow */ if(current_service == 65535) { break; } } + +#ifdef HAVE_ENDPROTOENT + endprotoent(); +#endif + return ldns_buffer_status(output); }