Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2 darr (dynamic-array) bugs #17648

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/darr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <zebra.h>
#include "darr.h"
#include "memory.h"
#include "printfrr.h"

DEFINE_MTYPE(LIB, DARR, "Dynamic Array");
DEFINE_MTYPE(LIB, DARR_STR, "Dynamic Array String");
Expand Down Expand Up @@ -70,7 +71,7 @@ char *__darr_in_vsprintf(char **sp, bool concat, const char *fmt, va_list ap)
*darr_append(*sp) = 0;
again:
va_copy(ap_copy, ap);
len = vsnprintf(darr_last(*sp), darr_avail(*sp) + 1, fmt, ap_copy);
len = vsnprintfrr(darr_last(*sp), darr_avail(*sp) + 1, fmt, ap_copy);
va_end(ap_copy);
if (len < 0)
darr_in_strcat(*sp, fmt);
Expand Down
24 changes: 12 additions & 12 deletions lib/darr.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt);
*/
#define darr_ensure_avail_mt(A, S, MT) \
({ \
ssize_t need = (ssize_t)(S) - \
(ssize_t)(darr_cap(A) - darr_len(A)); \
if (need > 0) \
_darr_resize_mt((A), darr_cap(A) + need, MT); \
ssize_t __dea_need = (ssize_t)(S) - \
(ssize_t)(darr_cap(A) - darr_len(A)); \
if (__dea_need > 0) \
_darr_resize_mt((A), darr_cap(A) + __dea_need, MT); \
(A); \
})
#define darr_ensure_avail(A, S) darr_ensure_avail_mt(A, S, MTYPE_DARR)
Expand All @@ -301,9 +301,9 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt);
#define darr_ensure_cap_mt(A, C, MT) \
({ \
/* Cast to avoid warning when C == 0 */ \
uint _c = (C) > 0 ? (C) : 1; \
if ((size_t)darr_cap(A) < _c) \
_darr_resize_mt((A), _c, MT); \
uint __dec_c = (C) > 0 ? (C) : 1; \
if ((size_t)darr_cap(A) < __dec_c) \
_darr_resize_mt((A), __dec_c, MT); \
(A); \
})
#define darr_ensure_cap(A, C) darr_ensure_cap_mt(A, C, MTYPE_DARR)
Expand Down Expand Up @@ -428,12 +428,12 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt);

#define _darr_append_n(A, N, Z, MT) \
({ \
uint __len = darr_len(A); \
darr_ensure_cap_mt(A, __len + (N), MT); \
_darr_len(A) = __len + (N); \
uint __da_len = darr_len(A); \
darr_ensure_cap_mt(A, __da_len + (N), MT); \
_darr_len(A) = __da_len + (N); \
if (Z) \
memset(&(A)[__len], 0, (N)*_darr_esize(A)); \
&(A)[__len]; \
memset(&(A)[__da_len], 0, (N)*_darr_esize(A)); \
&(A)[__da_len]; \
})
/**
* Extending the array's length by N.
Expand Down
16 changes: 8 additions & 8 deletions lib/mgmt_msg_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ extern int vmgmt_msg_native_send_error(struct msg_conn *conn,
*/
#define mgmt_msg_native_alloc_msg(msg_type, var_len, mem_type) \
({ \
uint8_t *buf = NULL; \
(msg_type *)darr_append_nz_mt(buf, \
uint8_t *__nam_buf = NULL; \
(msg_type *)darr_append_nz_mt(__nam_buf, \
sizeof(msg_type) + (var_len), \
mem_type); \
})
Expand Down Expand Up @@ -590,10 +590,10 @@ extern int vmgmt_msg_native_send_error(struct msg_conn *conn,
*/
#define mgmt_msg_native_append(msg, data, len) \
({ \
uint8_t **darrp = mgmt_msg_native_get_darrp(msg); \
uint8_t *p = darr_append_n(*darrp, len); \
memcpy(p, data, len); \
p; \
uint8_t **__na_darrp = mgmt_msg_native_get_darrp(msg); \
uint8_t *__na_p = darr_append_n(*__na_darrp, len); \
memcpy(__na_p, data, len); \
__na_p; \
})

/**
Expand All @@ -611,8 +611,8 @@ extern int vmgmt_msg_native_send_error(struct msg_conn *conn,
*/
#define mgmt_msg_native_add_str(msg, s) \
do { \
int __len = strlen(s) + 1; \
mgmt_msg_native_append(msg, s, __len); \
int __nas_len = strlen(s) + 1; \
mgmt_msg_native_append(msg, s, __nas_len); \
} while (0)

/**
Expand Down
Loading