Re: ober_get_writebuf return correct length

2022-01-20 Thread Martijn van Duren
Forgot to mention, I checked all the instances of ober_get_writebuf I
could find and they either don't use it for the actual length, or ber
has been freshly initialised just before. So there's no problem here in
the known consumers.

On Thu, 2022-01-20 at 18:50 +0100, Martijn van Duren wrote:
> While reading through ber.c I noticed that ober_get_writebuf can return
> the wrong length when called multiple times on the same ber instance.
> 
> This is because ober_get_writebuf uses br_wend to calculate the length,
> while ober_write_elements uses that to determine the size of the buffer.
> ober_write_elements uses br_wptr to determine how much has been written.
> So use that pointer instead.
> 
> $ cat test.c
> #include 
> #include 
> #include 
> 
> int
> main(int argc, char *argv[])
> {
> struct ber_element *root;
> struct ber ber;
> void *buf;
> 
> bzero(, sizeof(ber));
> 
> root = ober_printf_elements(NULL, "{}");
> printf("%zd\n", ober_write_elements(, root));
> printf("%zd\n", ober_get_writebuf(, ));
> 
> ober_free_elements(root);
> root = ober_printf_elements(NULL, "{d}", (int)1);
> printf("%zd\n", ober_write_elements(, root));
> printf("%zd\n", ober_get_writebuf(, ));
> 
> ober_free_elements(root);
> root = ober_printf_elements(NULL, "{}");
> printf("%zd\n", ober_write_elements(, root));
> printf("%zd\n", ober_get_writebuf(, ));
> }
> $ CFLAGS='-lutil' make test && ./test
> cc -lutil   -o test test.c 
> 2
> 2
> 5
> 5
> 2
> 5
> 
> OK?
> 
> martijn@
> 
> Index: ber.c
> ===
> RCS file: /cvs/src/lib/libutil/ber.c,v
> retrieving revision 1.23
> diff -u -p -r1.23 ber.c
> --- ber.c 21 Oct 2021 08:17:33 -  1.23
> +++ ber.c 20 Jan 2022 17:48:27 -
> @@ -831,7 +831,7 @@ ober_get_writebuf(struct ber *b, void **
>   if (b->br_wbuf == NULL)
>   return -1;
>   *buf = b->br_wbuf;
> - return (b->br_wend - b->br_wbuf);
> + return (b->br_wptr - b->br_wbuf);
>  }
>  
>  /*
> 



ober_get_writebuf return correct length

2022-01-20 Thread Martijn van Duren
While reading through ber.c I noticed that ober_get_writebuf can return
the wrong length when called multiple times on the same ber instance.

This is because ober_get_writebuf uses br_wend to calculate the length,
while ober_write_elements uses that to determine the size of the buffer.
ober_write_elements uses br_wptr to determine how much has been written.
So use that pointer instead.

$ cat test.c
#include 
#include 
#include 

int
main(int argc, char *argv[])
{
struct ber_element *root;
struct ber ber;
void *buf;

bzero(, sizeof(ber));

root = ober_printf_elements(NULL, "{}");
printf("%zd\n", ober_write_elements(, root));
printf("%zd\n", ober_get_writebuf(, ));

ober_free_elements(root);
root = ober_printf_elements(NULL, "{d}", (int)1);
printf("%zd\n", ober_write_elements(, root));
printf("%zd\n", ober_get_writebuf(, ));

ober_free_elements(root);
root = ober_printf_elements(NULL, "{}");
printf("%zd\n", ober_write_elements(, root));
printf("%zd\n", ober_get_writebuf(, ));
}
$ CFLAGS='-lutil' make test && ./test
cc -lutil   -o test test.c 
2
2
5
5
2
5

OK?

martijn@

Index: ber.c
===
RCS file: /cvs/src/lib/libutil/ber.c,v
retrieving revision 1.23
diff -u -p -r1.23 ber.c
--- ber.c   21 Oct 2021 08:17:33 -  1.23
+++ ber.c   20 Jan 2022 17:48:27 -
@@ -831,7 +831,7 @@ ober_get_writebuf(struct ber *b, void **
if (b->br_wbuf == NULL)
return -1;
*buf = b->br_wbuf;
-   return (b->br_wend - b->br_wbuf);
+   return (b->br_wptr - b->br_wbuf);
 }
 
 /*