wget-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

wget2 | wget_buffer_init: seeding initial data (#599)


From: Avinash Sonawane (@rootkea)
Subject: wget2 | wget_buffer_init: seeding initial data (#599)
Date: Thu, 28 Apr 2022 01:36:53 +0000


Avinash Sonawane created an issue: https://gitlab.com/gnuwget/wget2/-/issues/599



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`"

1. But we don't seem to be doing that. We're always emptying the data. 
`*buf->data = 0;` and `buf->length = 0;`
2. If we do want to fill the buffer with the provided `data` then:
  1. we should ask the user to zero-terminate the data before calling 
`wget_buffer_init()`
  2. we'll be needing the length of the `data` along with the `size` as `length 
of data <= size of data - 1`. We can calculate length using `strlen()` in 
`wget_buffer_init()` (preferred) or can get it from the user via an extra 
parameter.
3. What should happen if there is valid `0`-terminated `data` but passed `size` 
is `0`?
I think, in this case we should allocate the appropriate amount of memory for 
`buf->data` and copy the contents of `data` in it. WDYT?
4. Why do we set `buf->size` to one less than the actual buffer size? Why not 
set `buf->size` to the actual buffer size?

-- 
Reply to this email directly or view it on GitLab: 
https://gitlab.com/gnuwget/wget2/-/issues/599
You're receiving this email because of your account on gitlab.com.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]