mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-29 15:00:10 +01:00
glib-aux: create nm_strvarray_get_idxnull_or_greturn macro
This commit adds a getter macro which checks the bounds of a StrvArray when getting elements, while allowing access at `len` - returning NULL. https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1765
This commit is contained in:
parent
cd84dae191
commit
1f3779fceb
1 changed files with 14 additions and 0 deletions
|
|
@ -3019,6 +3019,20 @@ nm_strvarray_get_idx(GArray *array, guint idx)
|
|||
return nm_g_array_index(array, const char *, idx);
|
||||
}
|
||||
|
||||
/* nm_strvarray_get_idxnull_or_greturn() permits access at `len`,
|
||||
* returning NULL. If the access is out of bounds, the assertion
|
||||
* will fail (and also return NULL). */
|
||||
#define nm_strvarray_get_idxnull_or_greturn(arr, idx) \
|
||||
({ \
|
||||
GArray *_arr = (arr); \
|
||||
gsize _idx = (idx); \
|
||||
guint _len = nm_g_array_len(_arr); \
|
||||
\
|
||||
g_return_val_if_fail(_idx <= _len, NULL); \
|
||||
\
|
||||
_idx == _len ? NULL : nm_strvarray_get_idx(_arr, _idx); \
|
||||
})
|
||||
|
||||
static inline const char *const *
|
||||
nm_strvarray_get_strv_non_empty(GArray *arr, guint *length)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue