udebug: add more checks for uninitialized buffers

Simplifies and fixes error handling

Signed-off-by: Felix Fietkau <nbd@nbd.name>
This commit is contained in:
Felix Fietkau 2023-12-04 12:02:52 +01:00
parent 40acbe3463
commit d27acfe416

View file

@ -460,11 +460,11 @@ int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size)
buf->head_size = head_size; buf->head_size = head_size;
buf->data_size = size; buf->data_size = size;
buf->ring_size = entries; buf->ring_size = entries;
buf->fd = fd;
if (__udebug_buf_map(buf)) if (__udebug_buf_map(buf))
goto err_close; goto err_close;
buf->fd = fd;
buf->hdr->ring_size = entries; buf->hdr->ring_size = entries;
buf->hdr->data_size = size; buf->hdr->data_size = size;
@ -545,8 +545,12 @@ void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len
uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len) uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len)
{ {
struct udebug_hdr *hdr = buf->hdr; struct udebug_hdr *hdr = buf->hdr;
struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head); struct udebug_ptr *ptr;
if (!hdr)
return 0;
ptr = udebug_ring_ptr(hdr, hdr->head);
if (len) if (len)
ptr->len -= len; ptr->len -= len;
@ -556,8 +560,12 @@ uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len)
void udebug_entry_set_length(struct udebug_buf *buf, uint16_t len) void udebug_entry_set_length(struct udebug_buf *buf, uint16_t len)
{ {
struct udebug_hdr *hdr = buf->hdr; struct udebug_hdr *hdr = buf->hdr;
struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head); struct udebug_ptr *ptr;
if (!hdr)
return;
ptr = udebug_ring_ptr(hdr, hdr->head);
ptr->len = len; ptr->len = len;
} }
@ -608,10 +616,15 @@ out:
void udebug_entry_add(struct udebug_buf *buf) void udebug_entry_add(struct udebug_buf *buf)
{ {
struct udebug_hdr *hdr = buf->hdr; struct udebug_hdr *hdr = buf->hdr;
struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head); struct udebug_ptr *ptr;
uint32_t notify; uint32_t notify;
uint8_t *data; uint8_t *data;
if (!hdr)
return;
ptr = udebug_ring_ptr(hdr, hdr->head);
/* ensure strings are always 0-terminated */ /* ensure strings are always 0-terminated */
data = udebug_buf_ptr(buf, ptr->start + ptr->len); data = udebug_buf_ptr(buf, ptr->start + ptr->len);
*data = 0; *data = 0;
@ -685,6 +698,9 @@ __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf, int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
const struct udebug_buf_meta *meta) const struct udebug_buf_meta *meta)
{ {
if (!buf->hdr)
return -1;
list_add_tail(&buf->list, &ctx->local_rings); list_add_tail(&buf->list, &ctx->local_rings);
buf->ctx = ctx; buf->ctx = ctx;
buf->meta = meta; buf->meta = meta;