Skip to contentSkip to conversation

genl: Make iovec parameter const

PATCH Applied Ossama Othman 2 participants 6 messages Oct 11, 2017 – Oct 12, 2017

lore
Share & tools
lore
#0 lore raw

cover genl: Make iovec parameter const

This set of patches makes the iovec parameter of l_genl_msg_append_attrv() function const since it isn't modified by that function. This allows a pointer to a const struct iovec to be passed as an argument without casting away the const. Of course, a pointer to a non-const iovec may still be passed without a cast as well. A new unit test for l_genl_msg_append_attrv() is also added.

Ossama Othman (2):
genl: Make iovec parameter const
unit: Added test for l_genl_msg_append_attrv()

 ell/genl.c           |  3 ++-
 ell/genl.h           |  2 +-
 unit/test-genl-msg.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 2 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/2 genl: Make iovec parameter const

Make the iovec parameter of l_genl_msg_append_attrv() const since it isn't modified by the function. This allows a pointer to a const struct iovec to be passed as an argument without casting away the const.

---
 ell/genl.c | 3 ++-
 ell/genl.h | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
ell/genl.c +2 -1
index aa6266f..c04a47f 100644
@@ -797,7 +797,8 @@ LIB_EXPORT bool l_genl_msg_append_attr(struct l_genl_msg *msg, uint16_t type,
 }
 
 LIB_EXPORT bool l_genl_msg_append_attrv(struct l_genl_msg *msg, uint16_t type,
-					struct iovec *iov, size_t iov_len)
+					const struct iovec *iov,
+					size_t iov_len)
 {
 	struct nlattr *nla;
 	size_t len = 0;
ell/genl.h +1 -1
index 8597c8f..bddf1d4 100644
@@ -72,7 +72,7 @@ int l_genl_msg_get_error(struct l_genl_msg *msg);
 bool l_genl_msg_append_attr(struct l_genl_msg *msg, uint16_t type,
 					uint16_t len, const void *data);
 bool l_genl_msg_append_attrv(struct l_genl_msg *msg, uint16_t type,
-					struct iovec *iov, size_t iov_len);
+				const struct iovec *iov, size_t iov_len);
 bool l_genl_msg_enter_nested(struct l_genl_msg *msg, uint16_t type);
 bool l_genl_msg_leave_nested(struct l_genl_msg *msg);
 
Signature · 1 line
2.11.0
#2 lore raw

PATCH v2 2/2 unit: Added test for l_genl_msg_append_attrv()

---
 unit/test-genl-msg.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
unit/test-genl-msg.c +47 -0
index 49de707..1dd62be 100644
@@ -366,6 +366,50 @@ static void build_libnl_nested(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void test_append_attrv(const void *data)
+{
+	static const int num_blocks = 3;
+	unsigned char * const payload = set_station_request;
+	const size_t total_len = L_ARRAY_SIZE(set_station_request);
+	const size_t block_len = total_len / num_blocks;
+	const struct iovec iov[] = {
+	    {
+		.iov_base = payload,
+		.iov_len  = block_len
+	    },
+	    {
+		.iov_base = payload + block_len,
+		.iov_len  = block_len
+	    },
+	    {
+		.iov_base = payload + block_len * 2,
+		.iov_len  = total_len - block_len * 2
+	    }
+	};
+
+	struct l_genl_msg *msg;
+	const uint16_t type = 2;
+	struct l_genl_attr attr;
+	uint16_t attr_type;
+	uint16_t attr_len;
+	const void *attr_payload;
+
+	assert(L_ARRAY_SIZE(iov) == num_blocks);
+
+	msg = l_genl_msg_new_sized(1, NLA_HDRLEN + NLA_ALIGN(total_len));
+	assert(msg);
+
+	assert(l_genl_msg_append_attrv(msg, type, iov, L_ARRAY_SIZE(iov)));
+	assert(l_genl_attr_init(&attr, msg));
+	assert(l_genl_attr_next(&attr, &attr_type, &attr_len, &attr_payload));
+
+	assert(type == attr_type);
+	assert(total_len == attr_len);
+	assert(memcmp(payload, attr_payload, total_len) == 0);
+
+	l_genl_msg_unref(msg);
+}
+
 int main(int argc, char *argv[])
 {
 	bool little_endian;
@@ -396,6 +440,9 @@ int main(int argc, char *argv[])
 	l_test_add("Build libnl-generated Example with Nesting",
 				build_libnl_nested, NULL);
 
+	l_test_add("Test l_genl_msg_append_attrv",
+				test_append_attrv, NULL);
+
 done:
 	return l_test_run();
 }
Signature · 1 line
2.11.0
#3 lore raw

Re: [PATCH v2 2/2] unit: Added test for l_genl_msg_append_attrv()

Hi Ossama,

Quoted 4 lines · Ossama Othman
---
  unit/test-genl-msg.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 47 insertions(+)

Applied, thanks.

Regards,
-Denis

#4 lore raw
In reply to Ossama Othman

Hi Ossama,

Quoted 9 lines · Ossama Othman

Make the iovec parameter of l_genl_msg_append_attrv() const since it isn't modified by the function. This allows a pointer to a const struct iovec to be passed as an argument without casting away the const.

---
  ell/genl.c | 3 ++-
  ell/genl.h | 2 +-
  2 files changed, 3 insertions(+), 2 deletions(-)

Applied, thanks.

Regards,
-Denis

#5 lore raw

PATCH 2/2 unit: Added test for l_genl_msg_append_attrv()

In reply to Ossama Othman
---
 unit/test-genl-msg.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
unit/test-genl-msg.c +47 -0
index 49de707..50068e9 100644
@@ -366,6 +366,50 @@ static void build_libnl_nested(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void test_append_attrv(const void *data)
+{
+	static const int num_blocks = 3;
+	unsigned char * const payload = set_station_request;
+	const size_t total_len = L_ARRAY_SIZE(set_station_request);
+	const size_t block_len = total_len / num_blocks;
+	const struct iovec iov[] = {
+	    {
+		.iov_base = payload,
+		.iov_len  = block_len
+	    },
+	    {
+		.iov_base = payload + block_len,
+		.iov_len  = block_len
+	    },
+	    {
+		.iov_base = payload + block_len * 2,
+		.iov_len  = total_len - block_len * 2
+	    }
+	};
+
+	struct l_genl_msg *msg;
+	const uint16_t type = 2;
+	struct l_genl_attr attr;
+	uint16_t attr_type;
+	uint16_t attr_len;
+	const void *attr_payload;
+
+	assert(L_ARRAY_SIZE(iov) == num_blocks);
+
+	msg = l_genl_msg_new_sized(1, NLA_HDRLEN + NLA_ALIGN(total_len));
+	assert(msg);
+
+	assert(l_genl_msg_append_attrv(msg, type, iov, L_ARRAY_SIZE(iov)));
+	assert(l_genl_attr_init(&attr, msg));
+	assert(l_genl_attr_next(&attr, &attr_type, &attr_len, &attr_payload));
+
+        assert(type == attr_type);
+        assert(total_len == attr_len);
+        assert(memcmp(payload, attr_payload, total_len) == 0);
+
+	l_genl_msg_unref(msg);
+}
+
 int main(int argc, char *argv[])
 {
 	bool little_endian;
@@ -396,6 +440,9 @@ int main(int argc, char *argv[])
 	l_test_add("Build libnl-generated Example with Nesting",
 				build_libnl_nested, NULL);
 
+	l_test_add("Test l_genl_msg_append_attrv",
+				test_append_attrv, NULL);
+
 done:
 	return l_test_run();
 }
Signature · 1 line
2.11.0