You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.8 KiB
158 lines
5.8 KiB
2 months ago
|
From 7275fc782f822451b2cba5414037e1b0a1a59bf5 Mon Sep 17 00:00:00 2001
|
||
|
From: Phil Sutter <psutter@redhat.com>
|
||
|
Date: Wed, 8 May 2024 22:39:41 +0200
|
||
|
Subject: [PATCH] obj: Return value on setters
|
||
|
|
||
|
JIRA: https://issues.redhat.com/browse/RHEL-28515
|
||
|
Upstream Status: libnftnl commit 691f90223712426a2babdb55d7e5526b7310ca6e
|
||
|
|
||
|
commit 691f90223712426a2babdb55d7e5526b7310ca6e
|
||
|
Author: Phil Sutter <phil@nwl.cc>
|
||
|
Date: Thu Mar 14 16:54:55 2024 +0100
|
||
|
|
||
|
obj: Return value on setters
|
||
|
|
||
|
Similar to other setters, let callers know if memory allocation fails.
|
||
|
Though return value with all setters, as all of them may be used to set
|
||
|
object type-specific attributes which may fail (e.g. if NFTNL_OBJ_TYPE
|
||
|
was not set before).
|
||
|
|
||
|
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
||
|
|
||
|
Signed-off-by: Phil Sutter <psutter@redhat.com>
|
||
|
---
|
||
|
include/libnftnl/object.h | 14 ++++++-------
|
||
|
src/object.c | 41 +++++++++++++++++++++++----------------
|
||
|
2 files changed, 31 insertions(+), 24 deletions(-)
|
||
|
|
||
|
diff --git a/include/libnftnl/object.h b/include/libnftnl/object.h
|
||
|
index 4b2d90f..e235fdf 100644
|
||
|
--- a/include/libnftnl/object.h
|
||
|
+++ b/include/libnftnl/object.h
|
||
|
@@ -123,14 +123,14 @@ void nftnl_obj_free(const struct nftnl_obj *ne);
|
||
|
|
||
|
bool nftnl_obj_is_set(const struct nftnl_obj *ne, uint16_t attr);
|
||
|
void nftnl_obj_unset(struct nftnl_obj *ne, uint16_t attr);
|
||
|
-void nftnl_obj_set_data(struct nftnl_obj *ne, uint16_t attr, const void *data,
|
||
|
- uint32_t data_len);
|
||
|
+int nftnl_obj_set_data(struct nftnl_obj *ne, uint16_t attr, const void *data,
|
||
|
+ uint32_t data_len);
|
||
|
void nftnl_obj_set(struct nftnl_obj *ne, uint16_t attr, const void *data) __attribute__((deprecated));
|
||
|
-void nftnl_obj_set_u8(struct nftnl_obj *ne, uint16_t attr, uint8_t val);
|
||
|
-void nftnl_obj_set_u16(struct nftnl_obj *ne, uint16_t attr, uint16_t val);
|
||
|
-void nftnl_obj_set_u32(struct nftnl_obj *ne, uint16_t attr, uint32_t val);
|
||
|
-void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val);
|
||
|
-void nftnl_obj_set_str(struct nftnl_obj *ne, uint16_t attr, const char *str);
|
||
|
+int nftnl_obj_set_u8(struct nftnl_obj *ne, uint16_t attr, uint8_t val);
|
||
|
+int nftnl_obj_set_u16(struct nftnl_obj *ne, uint16_t attr, uint16_t val);
|
||
|
+int nftnl_obj_set_u32(struct nftnl_obj *ne, uint16_t attr, uint32_t val);
|
||
|
+int nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val);
|
||
|
+int nftnl_obj_set_str(struct nftnl_obj *ne, uint16_t attr, const char *str);
|
||
|
const void *nftnl_obj_get_data(const struct nftnl_obj *ne, uint16_t attr,
|
||
|
uint32_t *data_len);
|
||
|
const void *nftnl_obj_get(const struct nftnl_obj *ne, uint16_t attr);
|
||
|
diff --git a/src/object.c b/src/object.c
|
||
|
index a1a00d8..30e5ee8 100644
|
||
|
--- a/src/object.c
|
||
|
+++ b/src/object.c
|
||
|
@@ -77,8 +77,8 @@ static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
|
||
|
};
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_data);
|
||
|
-void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
|
||
|
- const void *data, uint32_t data_len)
|
||
|
+int nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
|
||
|
+ const void *data, uint32_t data_len)
|
||
|
{
|
||
|
if (attr < NFTNL_OBJ_MAX)
|
||
|
nftnl_assert_validate(data, nftnl_obj_validate, attr, data_len);
|
||
|
@@ -87,15 +87,19 @@ void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
|
||
|
case NFTNL_OBJ_TABLE:
|
||
|
xfree(obj->table);
|
||
|
obj->table = strdup(data);
|
||
|
+ if (!obj->table)
|
||
|
+ return -1;
|
||
|
break;
|
||
|
case NFTNL_OBJ_NAME:
|
||
|
xfree(obj->name);
|
||
|
obj->name = strdup(data);
|
||
|
+ if (!obj->name)
|
||
|
+ return -1;
|
||
|
break;
|
||
|
case NFTNL_OBJ_TYPE:
|
||
|
obj->ops = nftnl_obj_ops_lookup(*((uint32_t *)data));
|
||
|
if (!obj->ops)
|
||
|
- return;
|
||
|
+ return -1;
|
||
|
break;
|
||
|
case NFTNL_OBJ_FAMILY:
|
||
|
memcpy(&obj->family, data, sizeof(obj->family));
|
||
|
@@ -112,16 +116,19 @@ void nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
|
||
|
|
||
|
obj->user.data = malloc(data_len);
|
||
|
if (!obj->user.data)
|
||
|
- return;
|
||
|
+ return -1;
|
||
|
memcpy(obj->user.data, data, data_len);
|
||
|
obj->user.len = data_len;
|
||
|
break;
|
||
|
default:
|
||
|
- if (obj->ops)
|
||
|
- obj->ops->set(obj, attr, data, data_len);
|
||
|
- break;
|
||
|
+ if (!obj->ops)
|
||
|
+ return -1;
|
||
|
+
|
||
|
+ if (obj->ops->set(obj, attr, data, data_len) < 0)
|
||
|
+ return -1;
|
||
|
}
|
||
|
obj->flags |= (1 << attr);
|
||
|
+ return 0;
|
||
|
}
|
||
|
|
||
|
void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data) __visible;
|
||
|
@@ -131,33 +138,33 @@ void nftnl_obj_set(struct nftnl_obj *obj, uint16_t attr, const void *data)
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_u8);
|
||
|
-void nftnl_obj_set_u8(struct nftnl_obj *obj, uint16_t attr, uint8_t val)
|
||
|
+int nftnl_obj_set_u8(struct nftnl_obj *obj, uint16_t attr, uint8_t val)
|
||
|
{
|
||
|
- nftnl_obj_set_data(obj, attr, &val, sizeof(uint8_t));
|
||
|
+ return nftnl_obj_set_data(obj, attr, &val, sizeof(uint8_t));
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_u16);
|
||
|
-void nftnl_obj_set_u16(struct nftnl_obj *obj, uint16_t attr, uint16_t val)
|
||
|
+int nftnl_obj_set_u16(struct nftnl_obj *obj, uint16_t attr, uint16_t val)
|
||
|
{
|
||
|
- nftnl_obj_set_data(obj, attr, &val, sizeof(uint16_t));
|
||
|
+ return nftnl_obj_set_data(obj, attr, &val, sizeof(uint16_t));
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_u32);
|
||
|
-void nftnl_obj_set_u32(struct nftnl_obj *obj, uint16_t attr, uint32_t val)
|
||
|
+int nftnl_obj_set_u32(struct nftnl_obj *obj, uint16_t attr, uint32_t val)
|
||
|
{
|
||
|
- nftnl_obj_set_data(obj, attr, &val, sizeof(uint32_t));
|
||
|
+ return nftnl_obj_set_data(obj, attr, &val, sizeof(uint32_t));
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_u64);
|
||
|
-void nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val)
|
||
|
+int nftnl_obj_set_u64(struct nftnl_obj *obj, uint16_t attr, uint64_t val)
|
||
|
{
|
||
|
- nftnl_obj_set_data(obj, attr, &val, sizeof(uint64_t));
|
||
|
+ return nftnl_obj_set_data(obj, attr, &val, sizeof(uint64_t));
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_set_str);
|
||
|
-void nftnl_obj_set_str(struct nftnl_obj *obj, uint16_t attr, const char *str)
|
||
|
+int nftnl_obj_set_str(struct nftnl_obj *obj, uint16_t attr, const char *str)
|
||
|
{
|
||
|
- nftnl_obj_set_data(obj, attr, str, strlen(str) + 1);
|
||
|
+ return nftnl_obj_set_data(obj, attr, str, strlen(str) + 1);
|
||
|
}
|
||
|
|
||
|
EXPORT_SYMBOL(nftnl_obj_get_data);
|