Skip to content

net: buf: Pre-initialize user_data on net_buf_alloc() #77088

@jori-nordic

Description

@jori-nordic

Introduction

We can initialize a net_buf's user_data member with a known pattern.
Just like the CONFIG_INIT_STACKS option.

Problem description

It is not clear who owns user_data.
Is it the module that allocates it, the API that consumes it, something else?

With a known (non-null) initialized value, it will be possible to perform some checks and avoid head-scratching debug sessions. We've had some of those sessions in Bluetooth/L2CAP.

Proposed change

Call memset(buf->user_data, 0xAA, buf->user_data_size) in net_buf_alloc() right before returning buf.
Can be locked behind a kconfig, just like the stacks option.

Concerns and Unresolved Questions

Other users (maybe out of tree too) could break due to NULL checks on user_data not working anymore.
E.g. the pattern of casting cb from user_data and having a block like this one:

if (cb) {
  cb(some_param);
}

Alternatives

  • APIs that consume net_bufs enforce clearing by the application

Metadata

Metadata

Assignees

Labels

RFCRequest For Comments: want input from the communityarea: Networking Buffersnet_buf/net_buf_simple API & implementation

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions