[ldns-users] ldns_buffer2str asserting after ldns_buffer_copy

Willem Toorop willem at nlnetlabs.nl
Wed Aug 22 09:53:30 UTC 2012


Hi Karel,

The behaviour of those ldns_buffer functions is indeed non-intuitive and
was erroneous at some places as well.

The problem with your example program was that ldns_buffer2str used the
function ldns_buffer_export which would fix the buffer. Though, this was
not necessary at all since ldns_buffer2str would return a copy of the
data in the buffer. I have fixed ldns_buffer2str to use
ldns_buffer_begin i.s.o. ldns_buffer_export and created a new function
ldns_buffer_export2str that actually returns the exported buffer data
(null-terminated of course) and does not return a copy.

The problem with ldns_buffer_copy is that it returns a flipped
destination buffer. Flipped buffers are buffers that are ready for
sequential reading. The ldns library does not use this modus operandi,
and nor do the example tools, but unbound does.

The buffer handling functions in ldns (like ldns_buffer2str) do not
expect buffers to be flipped. It is thus not recommended to use
ldns_buffer_flip and ldns_buffer_copy (expect when your only going to
use the ldns_buffer_read_* functions). In stead of using
ldns_buffer_copy you could use:

	ldns_buffer_clear(b1);
	ldns_buffer_write(b1, ldns_buffer_begin(b2),
		ldns_buffer_position(b2));

I also updated the documentation of ldns_buffer_copy and ldns_buffer2str
to make them more clear.

Updates are in revision 3711.

Best regards,

-- Willem

Op 20-07-12 14:43, Karel Slany schreef:
> Hello again,
> 
> I'm writing some scripts for automated checking of pyLDNS basic
> functionality. I have encountered strange behaviour of the
> ldns_buffer_copy and ldns_buffer2str function.
> 
> ldns_buffer: ./buffer.c:82: ldns_buffer_reserve: Assertion
> `!buffer->_fixed' failed.
> 
> I am not sure whether this is a bug or I am missing something. But I
> haven't found any clues in the documentation telling me why this
> shouldn't work.
> 
> I've rewritten the code to C, in order to make it more understandable:
> 
> #include <stdio.h>
> #include <ldns/ldns.h>
> 
> int main(void)
> {
> 	ldns_buffer *b1, *b2;
> 	char *str;
> 
> 	b1 = ldns_buffer_new(1024);
> 	b2 = ldns_buffer_new(10);
> 
> 	ldns_buffer_printf(b1, "%s", "abc");
> 	ldns_buffer_printf(b2, "%s", "def");
> 
> 	str = ldns_buffer2str(b1);
> 	fprintf(stdout, "b1: %s\n", str);
> 	LDNS_FREE(str);
> 
> 	str = ldns_buffer2str(b2);
> 	fprintf(stdout, "b2: %s\n", str);
> 	LDNS_FREE(str);
> 
> 	ldns_buffer_copy(b1, b2);
> 
> 	str = ldns_buffer2str(b1); /* Asserts here. */
> 	fprintf(stdout, "b1: %s\n", str);
> 	LDNS_FREE(str);
> 
> 	str = ldns_buffer2str(b2);
> 	fprintf(stdout, "b2: %s\n", str);
> 	LDNS_FREE(str);
> 
> 	ldns_buffer_free(b1);
> 	ldns_buffer_free(b2);
> }
> 
> The comment shows the position, where the code is asserting.
> 
> I've tested ldns 1.6.13 and also the latest SVN (revision 3707), both
> behave the same.
> 
> K.
> _______________________________________________
> ldns-users mailing list
> ldns-users at open.nlnetlabs.nl
> http://open.nlnetlabs.nl/mailman/listinfo/ldns-users
> 




More information about the ldns-users mailing list