util: switch iobuf to uint8_t to avoid sign issues

char is signed or unsigned, so let's avoid this by using uint8_t.
This commit is contained in:
Peter Hutterer 2023-08-23 10:16:38 +10:00
parent 397ee6d79c
commit a223ce86da
3 changed files with 23 additions and 24 deletions

View file

@ -340,7 +340,7 @@ brei_marshal_message(struct brei_context *brei,
size_t message_len = iobuf_len(buf) + sizeof(struct brei_header);
uint32_t header[4] = {0, 0, message_len, opcode};
memcpy(header, &id, sizeof(id));
iobuf_prepend(buf, (const char *)header, sizeof(header));
iobuf_prepend(buf, header, sizeof(header));
return brei_result_new_success(steal(&buf));
}

View file

@ -123,7 +123,7 @@ xsend_with_fd(int fd, const void *buf, size_t len, int *fds)
struct iobuf {
size_t sz;
size_t len;
char *data;
uint8_t *data;
int fds[32];
};
@ -131,7 +131,7 @@ struct iobuf *
iobuf_new(size_t size)
{
struct iobuf *buf = malloc(sizeof(*buf));
char *data = malloc(size);
uint8_t *data = malloc(size);
assert(buf);
assert(data);
@ -182,7 +182,7 @@ iobuf_pop(struct iobuf *buf, size_t nbytes)
* The returned pointer only valid in the immediate scope, any iobuf
* function may invalidate the pointer.
*/
const char *
const uint8_t *
iobuf_data(struct iobuf *buf)
{
return buf->data;
@ -194,7 +194,7 @@ iobuf_data(struct iobuf *buf)
* The returned pointer only valid in the immediate scope, any iobuf
* function may invalidate the pointer.
*/
const char *
const uint8_t *
iobuf_data_end(struct iobuf *buf)
{
return buf->data + buf->len;
@ -216,7 +216,7 @@ iobuf_take_fd(struct iobuf *buf)
static inline void
iobuf_resize(struct iobuf *buf, size_t to_size)
{
char *newdata = realloc(buf->data, to_size);
uint8_t *newdata = realloc(buf->data, to_size);
assert(newdata);
buf->data = newdata;
@ -235,10 +235,10 @@ iobuf_extend(struct iobuf *buf, size_t extra)
* Remove the data bytes from the buffer. The caller must free() the data.
* The buffer state is the same as iobuf_new() after this call.
*/
char *
uint8_t *
iobuf_take_data(struct iobuf *buf)
{
char *data = buf->data;
uint8_t *data = buf->data;
buf->data = NULL;
buf->len = 0;
@ -252,7 +252,7 @@ iobuf_take_data(struct iobuf *buf)
* size it is resized automatically.
*/
void
iobuf_append(struct iobuf *buf, const char *data, size_t len)
iobuf_append(struct iobuf *buf, const void *data, size_t len)
{
if (len == 0)
return;
@ -296,7 +296,7 @@ iobuf_append_f32(struct iobuf *buf, float data)
* Prepend the given data to the buffer.
*/
void
iobuf_prepend(struct iobuf *buf, const char *data, size_t len)
iobuf_prepend(struct iobuf *buf, const void *data, size_t len)
{
if (len == 0)
return;
@ -498,7 +498,7 @@ MUNIT_TEST(test_iobuf_append_prepend)
munit_assert_size(buf->sz, ==, 10);
/* we don't have a trailing \0 */
const char *bufdata = iobuf_data(buf);
const uint8_t *bufdata = iobuf_data(buf);
munit_assert_char(bufdata[0], ==, 'f');
munit_assert_char(bufdata[1], ==, 'o');
munit_assert_char(bufdata[2], ==, 'o');
@ -530,7 +530,7 @@ MUNIT_TEST(test_iobuf_append_prepend)
munit_assert_size(iobuf_len(buf), ==, expected_size);
munit_assert_size(buf->sz, ==, expected_size);
/* now we have a trailing \0 */
munit_assert_string_equal(iobuf_data(buf), "barfoodata forcing resize");
munit_assert_string_equal((const char *)iobuf_data(buf), "barfoodata forcing resize");
/* and again with prepending */
const char prepend_data2[] = "second resize";
@ -539,7 +539,7 @@ MUNIT_TEST(test_iobuf_append_prepend)
munit_assert_size(iobuf_len(buf), ==, expected_size);
munit_assert_size(buf->sz, ==, expected_size);
munit_assert_string_equal(iobuf_data(buf), "second resizebarfoodata forcing resize");
munit_assert_string_equal((const char *)iobuf_data(buf), "second resizebarfoodata forcing resize");
return MUNIT_OK;
}
@ -555,7 +555,7 @@ MUNIT_TEST(test_iobuf_append_values)
munit_assert_size(iobuf_len(buf), ==, expected_size);
munit_assert_size(buf->sz, ==, 10);
const char *bufdata = iobuf_data(buf);
const uint8_t *bufdata = iobuf_data(buf);
munit_assert_int(bufdata[0], ==, 0xff);
munit_assert_int(bufdata[1], ==, 0xff);
munit_assert_int(bufdata[2], ==, 0xff);
@ -599,7 +599,7 @@ MUNIT_TEST(test_iobuf_prepend_empty_buffer)
munit_assert_size(buf->sz, ==, 10);
/* we don't have a trailing \0 */
const char *bufdata = iobuf_data(buf);
const uint8_t *bufdata = iobuf_data(buf);
munit_assert_char(bufdata[0], ==, 'f');
munit_assert_char(bufdata[1], ==, 'o');
munit_assert_char(bufdata[2], ==, 'o');
@ -618,7 +618,7 @@ MUNIT_TEST(test_iobuf_pop)
munit_assert_size(iobuf_len(buf), ==, 3);
/* we don't have a trailing \0 */
const char *bufdata = iobuf_data(buf);
const uint8_t *bufdata = iobuf_data(buf);
munit_assert_char(bufdata[0], ==, 'b');
munit_assert_char(bufdata[1], ==, 'a');
munit_assert_char(bufdata[2], ==, 'r');
@ -640,8 +640,7 @@ MUNIT_TEST(test_iobuf_append_short)
munit_assert_size(buf->len, ==, 4);
munit_assert_size(iobuf_len(buf), ==, 4);
munit_assert_size(buf->sz, ==, 10);
munit_assert_string_equal(iobuf_data(buf),
"foo");
munit_assert_string_equal((const char *)iobuf_data(buf), "foo");
return MUNIT_OK;
}
@ -671,7 +670,7 @@ MUNIT_TEST(test_iobuf_append_fd)
/* so we can do strcmp */
const char nullbyte = '\0';
iobuf_append(buf, &nullbyte, 1);
munit_assert_string_equal(iobuf_data(buf), "foob");
munit_assert_string_equal((const char *)iobuf_data(buf), "foob");
/* read when there's nothing waiting */
int blocking_read = iobuf_append_from_fd(buf, rd);

View file

@ -266,7 +266,7 @@ iobuf_len(struct iobuf *buf);
* The returned pointer only valid in the immediate scope, any iobuf
* function may invalidate the pointer.
*/
const char *
const uint8_t *
iobuf_data(struct iobuf *buf);
/**
@ -275,7 +275,7 @@ iobuf_data(struct iobuf *buf);
* The returned pointer only valid in the immediate scope, any iobuf
* function may invalidate the pointer.
*/
const char *
const uint8_t *
iobuf_data_end(struct iobuf *buf);
/**
@ -289,7 +289,7 @@ iobuf_take_fd(struct iobuf *buf);
* Remove the data bytes from the buffer. The caller must free() the data.
* The buffer state is the same as iobuf_new() after this call.
*/
char *
uint8_t *
iobuf_take_data(struct iobuf *buf);
/**
@ -303,7 +303,7 @@ iobuf_pop(struct iobuf *buf, size_t nbytes);
* size it is resized automatically.
*/
void
iobuf_append(struct iobuf *buf, const char *data, size_t len);
iobuf_append(struct iobuf *buf, const void *data, size_t len);
/**
* Append one 32-bit value to the buffer. If the data exceeds the current buffer
@ -331,7 +331,7 @@ iobuf_append_f32(struct iobuf *buf, float data);
* size it is resized automatically.
*/
void
iobuf_prepend(struct iobuf *buf, const char *data, size_t len);
iobuf_prepend(struct iobuf *buf, const void *data, size_t len);
/**
* Append a file descriptor to the buffer. The file descriptor is dup()ed.