[PATCH 6/6] CLEANUP: chunk: Remove duplicated chunk_Xcat implementation

2021-11-08 Thread Tim Duesterhus
Delegate chunk_istcat, chunk_cat and chunk_strncat to the most generic
chunk_memcat.
---
 include/haproxy/chunk.h | 41 +
 1 file changed, 13 insertions(+), 28 deletions(-)

diff --git a/include/haproxy/chunk.h b/include/haproxy/chunk.h
index af9ef816b..05fd16121 100644
--- a/include/haproxy/chunk.h
+++ b/include/haproxy/chunk.h
@@ -107,28 +107,6 @@ static inline int chunk_cpy(struct buffer *chk, const 
struct buffer *src)
return 1;
 }
 
-/* appends chunk  after . Returns 0 in case of failure. */
-static inline int chunk_cat(struct buffer *chk, const struct buffer *src)
-{
-   if (unlikely(chk->data + src->data > chk->size))
-   return 0;
-
-   memcpy(chk->area + chk->data, src->area, src->data);
-   chk->data += src->data;
-   return 1;
-}
-
-/* appends ist  after . Returns 0 in case of failure. */
-static inline int chunk_istcat(struct buffer *chk, const struct ist src)
-{
-   if (unlikely(chk->data + src.len > chk->size))
-   return 0;
-
-   memcpy(chk->area + chk->data, src.ptr, src.len);
-   chk->data += src.len;
-   return 1;
-}
-
 /* copies memory area  into  for  bytes. Returns 0 in
  * case of failure. No trailing zero is added.
  */
@@ -158,6 +136,18 @@ static inline int chunk_memcat(struct buffer *chk, const 
char *src,
return 1;
 }
 
+/* appends ist  after . Returns 0 in case of failure. */
+static inline int chunk_istcat(struct buffer *chk, const struct ist src)
+{
+   return chunk_memcat(chk, istptr(src), istlen(src));
+}
+
+/* appends chunk  after . Returns 0 in case of failure. */
+static inline int chunk_cat(struct buffer *chk, const struct buffer *src)
+{
+   return chunk_memcat(chk, src->area, src->data);
+}
+
 /* copies str into  followed by a trailing zero. Returns 0 in
  * case of failure.
  */
@@ -218,12 +208,7 @@ static inline int chunk_strcat(struct buffer *chk, const 
char *str)
  */
 static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
 {
-   if (unlikely(chk->data + nb >= chk->size))
-   return 0;
-
-   memcpy(chk->area + chk->data, str, nb);
-   chk->data += nb;
-   return 1;
+   return chunk_memcat(chk, str, nb);
 }
 
 /* Adds a trailing zero to the current chunk and returns the pointer to the
-- 
2.33.1




Re: CLEANUP: chunk

2016-03-24 Thread Willy TARREAU
Hi David,

On Thu, Mar 24, 2016 at 09:16:19AM +, David CARLIER wrote:
> Here a cleanup patch for the chunk_dup function.
> hope it can be useful.

Good catch, thank you. I've just merged it. That reminds me that there's
still another one from you I need to check.

Cheers,
Willy




CLEANUP: chunk

2016-03-24 Thread David CARLIER
Hi all,

Here a cleanup patch for the chunk_dup function.
hope it can be useful.

Regards.
From 3d904193dc041bad266fd04f69b50a66b8429f54 Mon Sep 17 00:00:00 2001
From: David Carlier <devne...@gmail.com>
Date: Wed, 23 Mar 2016 17:50:57 +
Subject: [PATCH] CLEANUP: chunk: adding NULL check to chunk_dup allocation.

Avoiding harmful memcpy call if the allocation failed.
Resetting the size which avoids further harmful freeing
invalid pointer. Closer to the comment behavior description.
---
 include/common/chunk.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/include/common/chunk.h b/include/common/chunk.h
index b74c767..aac5282 100644
--- a/include/common/chunk.h
+++ b/include/common/chunk.h
@@ -177,6 +177,12 @@ static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
 		dst->size++;
 
 	dst->str = (char *)malloc(dst->size);
+	if (!dst->str) {
+		dst->len = 0;
+		dst->size = 0;
+		return NULL;
+	}
+
 	memcpy(dst->str, src->str, dst->len);
 	if (dst->len < dst->size)
 		dst->str[dst->len] = 0;
-- 
2.7.4