Skip to content

Commit

Permalink
net: buf: linearize: Never return -ENOMEM, just do what user asked to
Browse files Browse the repository at this point in the history
Don't try to find "errors" in the values of dst_len and len params
passed to net_buf_linearize(). Instead, do what entails with the
common sense from the values passed in, specifically:

1. Never read more than dst_len (or it would lead to buffer
overflow).
2. It's absolutely ok to read than specified by "len" param, that's
why this function returns number of bytes read in the first place.

The motivation for this change is that it's not useful with its
current behavior. For example, a number of Ethernet drivers linearize
a packet to send, but each does it with its own duplicated adhoc
routine, because net_buf_linearize() would just return error for the
natural use of:

net_buf_linearize(buf, sizeof(buf), pkt->frags, 0, sizeof(buf));

Signed-off-by: Paul Sokolovsky <[email protected]>
  • Loading branch information
pfalcon authored and jukkar committed Jan 8, 2019
1 parent 188643e commit 71e2e31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
21 changes: 11 additions & 10 deletions include/net/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1300,21 +1300,22 @@ struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
#endif

/**
* @brief Copy len bytes from src starting from offset to dst buffer
* @brief Copy bytes from net_buf chain starting at offset to linear buffer
*
* This routine assumes that dst is large enough to store @a len bytes
* starting from offset at src.
* Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
* offset in it, to a linear buffer @a dst. Return number of bytes actually
* copied, which may be less than requested, if net_buf chain doesn't have
* enough data, or destination buffer is too small.
*
* @param dst Destination buffer
* @param dst_len Destination buffer max length
* @param src Source buffer that may be fragmented
* @param offset Starting point to copy from
* @param dst_len Destination buffer length
* @param src Source net_buf chain
* @param offset Starting offset to copy from
* @param len Number of bytes to copy
* @return number of bytes copied if everything is ok
* @return -ENOMEM on error
* @return number of bytes actually copied
*/
int net_buf_linearize(void *dst, size_t dst_len,
struct net_buf *src, size_t offset, size_t len);
size_t net_buf_linearize(void *dst, size_t dst_len,
struct net_buf *src, size_t offset, size_t len);

/**
* @typedef net_buf_allocator_cb
Expand Down
12 changes: 3 additions & 9 deletions subsys/net/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,16 +674,14 @@ struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag)
return next_frag;
}

int net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src,
size_t offset, size_t len)
size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src,
size_t offset, size_t len)
{
struct net_buf *frag;
size_t to_copy;
size_t copied;

if (dst_len < (size_t)len) {
return -ENOMEM;
}
len = min(len, dst_len);

frag = src;

Expand Down Expand Up @@ -712,10 +710,6 @@ int net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src,
offset = 0;
}

if (len > 0) {
return -ENOMEM;
}

return copied;
}

Expand Down

0 comments on commit 71e2e31

Please sign in to comment.