wget_buffer_init: seeding initial data
Hello!
I'm looking closely at wget_buffer_init()
and I have few queries where docs and code don't match.
Before we begin, I've made this small obvious refactoring change which doesn't change the existing behavior at all:
@@ -155,7 +155,6 @@ int wget_buffer_init(wget_buffer *buf, char *data, size_t size)
if (data && likely(size)) {
buf->size = size - 1;
buf->data = data;
- *buf->data = 0; // always 0 terminate data to allow string functions
buf->release_data = 0;
} else {
if (!size)
@@ -165,10 +164,10 @@ int wget_buffer_init(wget_buffer *buf, char *data, size_t size)
buf->error = 1;
return WGET_E_MEMORY;
}
- *buf->data = 0; // always 0 terminate data to allow string functions
buf->release_data = 1;
}
+ *buf->data = 0; // always 0 terminate data to allow string functions
buf->error = 0;
buf->release_buf = 0;
buf->length = 0;
Okay, so the docs say "You may provide some data
to fill the buffer with it." and "If an existing buffer is provided in buf
, it will be initialized with the provided data
"
- But we don't seem to be doing that. We're always emptying the data.
*buf->data = 0;
andbuf->length = 0;
- If we do want to fill the buffer with the provided
data
then:- we should ask the user to zero-terminate the data before calling
wget_buffer_init()
- we'll be needing the length of the
data
along with thesize
aslength of data <= size of data - 1
. We can calculate length usingstrlen()
inwget_buffer_init()
(preferred) or can get it from the user via an extra parameter.
- we should ask the user to zero-terminate the data before calling
- What should happen if there is valid
0
-terminateddata
but passedsize
is0
? I think, in this case we should allocate the appropriate amount of memory forbuf->data
and copy the contents ofdata
in it. WDYT? - Why do we set
buf->size
to one less than the actual buffer size? Why not setbuf->size
to the actual buffer size?