mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-30 22:30:11 +01:00
platform/netlink: cleanup nla_memcpy()
- use size_t arguments for the memory sizes. While sizes from netlink API currently are int typed and inherrently limited, use the more appropriate data type. - rename the arguments. The "count" is really the size of the destination buffer. - return how many bytes we wanted to write (like g_strlcpy()). That makes more sense than how many bytes we actually wrote because previously, we could not detect truncation. Anyway, none of the callers cared about the return-value either way.
This commit is contained in:
parent
b080146cc6
commit
82e31b2816
2 changed files with 40 additions and 8 deletions
|
|
@ -420,18 +420,33 @@ nla_strlcpy (char *dst,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
nla_memcpy (void *dest, const struct nlattr *src, int count)
|
||||
size_t
|
||||
nla_memcpy (void *dst, const struct nlattr *nla, size_t dstsize)
|
||||
{
|
||||
int minlen;
|
||||
size_t len;
|
||||
int srclen;
|
||||
|
||||
if (!src)
|
||||
if (!nla)
|
||||
return 0;
|
||||
|
||||
minlen = NM_MIN (count, (int) nla_len (src));
|
||||
memcpy (dest, nla_data (src), minlen);
|
||||
srclen = nla_len (nla);
|
||||
|
||||
return minlen;
|
||||
if (srclen <= 0) {
|
||||
nm_assert (srclen == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
len = NM_MIN ((size_t) srclen, dstsize);
|
||||
if (len > 0) {
|
||||
/* there is a crucial difference between nla_strlcpy() and nla_memcpy().
|
||||
* The former always write @dstsize bytes (akin to strncpy()), here, we only
|
||||
* write the bytes that we actually have (leaving the remainder undefined). */
|
||||
memcpy (dst,
|
||||
nla_data (nla),
|
||||
len);
|
||||
}
|
||||
|
||||
return srclen;
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
|||
|
|
@ -195,7 +195,24 @@ nla_get_string (const struct nlattr *nla)
|
|||
|
||||
size_t nla_strlcpy (char *dst, const struct nlattr *nla, size_t dstsize);
|
||||
|
||||
int nla_memcpy (void *dest, const struct nlattr *src, int count);
|
||||
size_t nla_memcpy (void *dst, const struct nlattr *nla, size_t dstsize);
|
||||
|
||||
#define nla_memcpy_checked_size(dst, nla, dstsize) \
|
||||
G_STMT_START { \
|
||||
void *const _dst = (dst); \
|
||||
const struct nlattr *const _nla = (nla); \
|
||||
const size_t _dstsize = (dstsize); \
|
||||
size_t _srcsize; \
|
||||
\
|
||||
/* assert that, if @nla is given, that it has the exact expected
|
||||
* size. This implies that the caller previously verified the length
|
||||
* of the attribute (via minlen/maxlen at nla_parse()). */ \
|
||||
\
|
||||
if (_nla) { \
|
||||
_srcsize = nla_memcpy (_dst, _nla, _dstsize); \
|
||||
nm_assert (_srcsize == _dstsize); \
|
||||
} \
|
||||
} G_STMT_END
|
||||
|
||||
int nla_put (struct nl_msg *msg, int attrtype, int datalen, const void *data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue