* [PATCH 0/2] Introduce l_memdup function
@ 2015-01-16 9:13 Jukka Rissanen
2015-01-16 9:13 ` [PATCH 1/2] util: Add l_memdup function for allocation and copying a memory buffer Jukka Rissanen
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Jukka Rissanen @ 2015-01-16 9:13 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 616 bytes --]
Hi,
this patch introduces new l_memdup() function in patch 1.
The new function is then used in patch 2. The genl.c was the only
place in the code base where such a change could be done (at least I
could not spot any other place having malloc+memcpy in the code after
quick review).
Cheers,
Jukka
Jukka Rissanen (2):
util: Add l_memdup function for allocation and copying a memory buffer
genl: Convert l_malloc + memcpy to use l_memdup
ell/genl.c | 4 +---
ell/util.c | 23 +++++++++++++++++++++++
ell/util.h | 2 ++
3 files changed, 26 insertions(+), 3 deletions(-)
--
1.8.3.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] util: Add l_memdup function for allocation and copying a memory buffer
2015-01-16 9:13 [PATCH 0/2] Introduce l_memdup function Jukka Rissanen
@ 2015-01-16 9:13 ` Jukka Rissanen
2015-01-16 9:13 ` [PATCH 2/2] genl: Convert l_malloc + memcpy to use l_memdup Jukka Rissanen
2015-01-16 16:50 ` [PATCH 0/2] Introduce l_memdup function Denis Kenzior
2 siblings, 0 replies; 4+ messages in thread
From: Jukka Rissanen @ 2015-01-16 9:13 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 1331 bytes --]
---
ell/util.c | 23 +++++++++++++++++++++++
ell/util.h | 2 ++
2 files changed, 25 insertions(+)
diff --git a/ell/util.c b/ell/util.c
index 97e70bc..3bf2e5d 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -103,6 +103,29 @@ LIB_EXPORT void *l_realloc(void *mem, size_t size)
}
/**
+ * l_memdup:
+ * @mem: pointer to memory you want to duplicate
+ * @size: memory size
+ *
+ * If for any reason the memory allocation fails, then execution will be
+ * halted via abort().
+ *
+ * In case @size is 0 then #NULL will be returned.
+ *
+ * Returns: pointer to duplicated memory buffer
+ **/
+LIB_EXPORT void *l_memdup(const void *mem, size_t size)
+{
+ void *ptr;
+
+ ptr = l_malloc(size);
+ if (ptr)
+ memcpy(ptr, mem, size);
+
+ return ptr;
+}
+
+/**
* l_free:
* @ptr: memory pointer
*
diff --git a/ell/util.h b/ell/util.h
index e4d674c..89000aa 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -157,6 +157,8 @@ static inline void l_put_be64(uint64_t val, void *dst)
#define L_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));
+void *l_memdup(const void *mem, size_t size)
+ __attribute__ ((warn_unused_result, malloc));
void l_free(void *ptr);
void *l_realloc(void *mem, size_t size)
--
1.8.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] genl: Convert l_malloc + memcpy to use l_memdup
2015-01-16 9:13 [PATCH 0/2] Introduce l_memdup function Jukka Rissanen
2015-01-16 9:13 ` [PATCH 1/2] util: Add l_memdup function for allocation and copying a memory buffer Jukka Rissanen
@ 2015-01-16 9:13 ` Jukka Rissanen
2015-01-16 16:50 ` [PATCH 0/2] Introduce l_memdup function Denis Kenzior
2 siblings, 0 replies; 4+ messages in thread
From: Jukka Rissanen @ 2015-01-16 9:13 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 696 bytes --]
Use new l_memdup() function instead of l_malloc() followed by
memcpy().
---
ell/genl.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/ell/genl.c b/ell/genl.c
index dca8e1b..d2c1784 100644
--- a/ell/genl.c
+++ b/ell/genl.c
@@ -257,13 +257,11 @@ static struct l_genl_msg *msg_create(const struct nlmsghdr *nlmsg)
goto done;
}
- msg->data = l_malloc(nlmsg->nlmsg_len);
+ msg->data = l_memdup(nlmsg, nlmsg->nlmsg_len);
msg->len = nlmsg->nlmsg_len;
msg->size = nlmsg->nlmsg_len;
- memcpy(msg->data, nlmsg, nlmsg->nlmsg_len);
-
if (msg->len >= GENL_HDRLEN) {
struct genlmsghdr *genlmsg = msg->data + NLMSG_HDRLEN;
--
1.8.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] Introduce l_memdup function
2015-01-16 9:13 [PATCH 0/2] Introduce l_memdup function Jukka Rissanen
2015-01-16 9:13 ` [PATCH 1/2] util: Add l_memdup function for allocation and copying a memory buffer Jukka Rissanen
2015-01-16 9:13 ` [PATCH 2/2] genl: Convert l_malloc + memcpy to use l_memdup Jukka Rissanen
@ 2015-01-16 16:50 ` Denis Kenzior
2 siblings, 0 replies; 4+ messages in thread
From: Denis Kenzior @ 2015-01-16 16:50 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 437 bytes --]
Hi Jukka,
On 01/16/2015 03:13 AM, Jukka Rissanen wrote:
> Hi,
>
> this patch introduces new l_memdup() function in patch 1.
>
> The new function is then used in patch 2. The genl.c was the only
> place in the code base where such a change could be done (at least I
> could not spot any other place having malloc+memcpy in the code after
> quick review).
>
> Cheers,
> Jukka
>
Both applied, thanks.
Regards,
-Denis
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-01-16 16:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-16 9:13 [PATCH 0/2] Introduce l_memdup function Jukka Rissanen
2015-01-16 9:13 ` [PATCH 1/2] util: Add l_memdup function for allocation and copying a memory buffer Jukka Rissanen
2015-01-16 9:13 ` [PATCH 2/2] genl: Convert l_malloc + memcpy to use l_memdup Jukka Rissanen
2015-01-16 16:50 ` [PATCH 0/2] Introduce l_memdup function Denis Kenzior
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.