From 6a3602a30613bbd23a959200ba120ce28811c11b Mon Sep 17 00:00:00 2001 From: Jonathan Rico Date: Thu, 15 Aug 2024 13:34:39 +0200 Subject: [PATCH] net: buf: Clear `user_data` on allocation For two reasons: - prevent exposing the previous user's info to the new user - make NULL checks on user_data work Since we don't really have a field that specifies what part of the user_data array is valid, we have to rely on other checks. Such a check, if user_data contains a callback, is comparing against NULL before calling said callback. Signed-off-by: Jonathan Rico --- include/zephyr/net/buf.h | 2 +- subsys/net/buf.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/zephyr/net/buf.h b/include/zephyr/net/buf.h index 9b1d5af16b3..88152bc9905 100644 --- a/include/zephyr/net/buf.h +++ b/include/zephyr/net/buf.h @@ -1049,7 +1049,7 @@ struct net_buf { /** @endcond */ }; - /** System metadata for this buffer. */ + /** System metadata for this buffer. Cleared on allocation. */ uint8_t user_data[] __net_buf_align; }; diff --git a/subsys/net/buf.c b/subsys/net/buf.c index 8267943dc01..6f0ee42f5c2 100644 --- a/subsys/net/buf.c +++ b/subsys/net/buf.c @@ -338,6 +338,7 @@ success: buf->flags = 0U; buf->frags = NULL; buf->size = size; + memset(buf->user_data, 0, buf->user_data_size); net_buf_reset(buf); #if defined(CONFIG_NET_BUF_POOL_USAGE)