From d8ff87989f616291dcb998670958fd42029796e5 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Thu, 22 Feb 2024 01:11:01 -0600 Subject: some stuff --- src/nbt/list.c | 80 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 25 deletions(-) (limited to 'src/nbt/list.c') diff --git a/src/nbt/list.c b/src/nbt/list.c index df12844..7c5ed7c 100644 --- a/src/nbt/list.c +++ b/src/nbt/list.c @@ -31,49 +31,79 @@ nbt_tag_t *nbt_list_get(nbt_tag_t *list, nbt_size_t idx) return list->value.nbt_list.ptags[idx]; } -#define NBT__APPEND_BODY(_list, _tag, _ex) \ -{ \ - NBT__CHECK(list, -1); \ - if (!tag) return -1; \ +int nbt_list__check_tag(nbt_tag_t *list, nbt_tag_t *tag) +{ + if (!tag) return -1; + nbt_type_t lt = nbt_list_type(list); + + if (lt != NBT_TAG_END && lt != nbt_tag_type(tag)) return -1; + + return 0; +} + +#define NBT__APPEND_BODY(_list, _tag, _ex, _err) \ + NBT__CHECK(list, -1); \ + if (nbt_list__check_tag(_list, _tag) < 0) \ + goto _err; \ \ - int ret = nbt_list_reserve_more(list, 1); \ - if (ret < 0) return ret; \ + int ret = nbt_list_reserve_more(list, 1); \ + if (ret < 0) goto _err; \ \ list->value.nbt_list.ptags[list->value.nbt_list.len++] = _ex(tag); \ - return 0; \ -} + return 0; #define NBT__LEAVE_ALONE(_t) _t int nbt_list_append(nbt_tag_t *list, nbt_tag_t *tag) - NBT__APPEND_BODY(list, tag, nbt_incref) +{ + NBT__APPEND_BODY(list, tag, nbt_incref, error) + +error: + return -1; +} int nbt_list_append_move(nbt_tag_t *list, nbt_tag_t *tag) - NBT__APPEND_BODY(list, tag, NBT__LEAVE_ALONE) +{ + NBT__APPEND_BODY(list, tag, NBT__LEAVE_ALONE, error) + +error: + nbt_decref(tag); + return -1; +} -#define NBT__INSERT_BODY(_list, _tag, _at, _ex) \ -{ \ - NBT__CHECK(list, -1); \ - if (!tag) return -1; \ +#define NBT__INSERT_BODY(_list, _tag, _at, _ex, _err) \ + NBT__CHECK(list, -1); \ + if (nbt_list__check_tag(_list, _tag) < 0) \ + goto _err; \ \ - int ret = nbt_list_reserve_more(list, 1); \ - if (ret < 0) return ret; \ + int ret = nbt_list_reserve_more(list, 1); \ + if (ret < 0) goto _err; \ \ - memmove(list->value.nbt_list.ptags + _at + 1, \ - list->value.nbt_list.ptags + _at, \ - list->value.nbt_list.len - _at); \ + memmove(list->value.nbt_list.ptags + _at + 1, \ + list->value.nbt_list.ptags + _at, \ + list->value.nbt_list.len - _at); \ \ - list->value.nbt_list.ptags[at] = _ex(tag); \ + list->value.nbt_list.ptags[at] = _ex(tag); \ \ - ++list->value.nbt_list.len; \ - return 0; \ -} + ++list->value.nbt_list.len; \ + return 0; int nbt_list_insert(nbt_tag_t *list, nbt_tag_t *tag, size_t at) - NBT__INSERT_BODY(list, tag, at, nbt_incref) +{ + NBT__INSERT_BODY(list, tag, at, nbt_incref, error) + +error: + return -1; +} int nbt_list_insert_move(nbt_tag_t *list, nbt_tag_t *tag, size_t at) - NBT__INSERT_BODY(list, tag, at, NBT__LEAVE_ALONE) +{ + NBT__INSERT_BODY(list, tag, at, NBT__LEAVE_ALONE, error) + +error: + nbt_decref(tag); + return -1; +} int nbt_list_remove(nbt_tag_t *list, nbt_size_t idx) { -- cgit v1.2.3-70-g09d2