netlink: return -ENOMEM when calloc fails
Subsystems (2)
PATCH 1/2 netlink: return -ENOMEM when calloc fails
--- netlink/features.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
netlink/features.c +5 -4
@@ -112,16 +112,17 @@ int dump_features(const struct nlattr *const *tb,unsigned int *feature_flags =NULL ;struct feature_results results;unsigned int i, j; -int ret; +int ret =0 ; ret =prepare_feature_results (tb, &results);if (ret <0 )return -EFAULT ; - ret = -ENOMEM ; feature_flags =calloc (results.count,sizeof (feature_flags[0 ])); -if (!feature_flags) +if (!feature_flags) { + ret = -ENOMEM ; goto out_free; + }/* map netdev features to legacy flags */ for (i =0 ; i < results.count; i++) {
@@ -184,7 +185,7 @@ int dump_features(const struct nlattr *const *tb, out_free:free (feature_flags); -return 0 ; +return ret; }int features_reply_cb (const struct nlmsghdr *nlhdr,void *data)
Signature · 1 line
2.26.2
PATCH 2/2 netlink: fix memory leak
Potentially allocated memory allocated for mask is not freed when the allocation for value fails.
--- netlink/parser.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
netlink/parser.c +3 -1
@@ -630,8 +630,10 @@ static int parse_numeric_bitset(struct nl_context *nlctx, uint16_t type, } value =calloc (nwords,sizeof (uint32_t )); -if (!value) +if (!value) { +free (mask);return -ENOMEM ; + } ret =__parse_num_string (arg, len1, value, force_hex1);if (ret <0 ) {parser_err_invalid_value (nlctx, arg);
Signature · 1 line
2.26.2
Michal Kubecek
AppliedRe: [PATCH ethtool 2/2] netlink: fix memory leak
Quoted 8 lines · Ivan Vecera
Potentially allocated memory allocated for mask is not freed when the allocation for value fails.
Fixes 81a30f416ec7 ("netlink: add bitset command line parser handlers")Cc Michal Kubecek <[email protected]>Signed-off-by Ivan Vecera <[email protected]>---
Applied, thank you.
Michal
Quoted 22 lines
netlink/parser.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)netlink/parser.c +3
-1@@ -630,8 +630,10 @@ static int parse_numeric_bitset(struct nl_context *nlctx, uint16_t type, } value =calloc (nwords,sizeof (uint32_t )); -if (!value) +if (!value) { +free (mask);return -ENOMEM ; + } ret =__parse_num_string (arg, len1, value, force_hex1);if (ret <0 ) {parser_err_invalid_value (nlctx, arg);Signature · 1 line
2.26.2
Quoted 44 lines · Ivan Vecera
Fixes f2c17e107900 ("netlink: add netlink handler for gfeatures (-k)")Cc Michal Kubecek <[email protected]>Signed-off-by Ivan Vecera <[email protected]>--- netlink/features.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)netlink/features.c +5
-4@@ -112,16 +112,17 @@ int dump_features(const struct nlattr *const *tb,unsigned int *feature_flags =NULL ;struct feature_results results;unsigned int i, j; -int ret; +int ret =0 ; ret =prepare_feature_results (tb, &results);if (ret <0 )return -EFAULT ; - ret = -ENOMEM ; feature_flags =calloc (results.count,sizeof (feature_flags[0 ])); -if (!feature_flags) +if (!feature_flags) { + ret = -ENOMEM ; goto out_free; + }/* map netdev features to legacy flags */ for (i =0 ; i < results.count; i++) {@@ -184,7 +185,7 @@ int dump_features(const struct nlattr *const *tb, out_free:free (feature_flags); -return 0 ; +return ret; }int features_reply_cb (const struct nlmsghdr *nlhdr,void *data)Signature · 1 line
2.26.2
The patch is correct but relying on ret staying zero through the whole function is rather fragile (it could break when adding more checks in the future) and it also isn't consistent with the way this is done in other functions.
AFAICS you could omit the first hunk and just add "ret = 0" above the out_free label.
Michal
Quoted 51 lines · On Mon, 28 Sep 2020 17:44:55 +0200 Michal Kubecek <[email protected]> wrote:
Quoted 44 lines · Ivan Vecera
Fixes f2c17e107900 ("netlink: add netlink handler for gfeatures (-k)")Cc Michal Kubecek <[email protected]>Signed-off-by Ivan Vecera <[email protected]>--- netlink/features.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)netlink/features.c +5
-4@@ -112,16 +112,17 @@ int dump_features(const struct nlattr *const *tb,unsigned int *feature_flags =NULL ;struct feature_results results;unsigned int i, j; -int ret; +int ret =0 ; ret =prepare_feature_results (tb, &results);if (ret <0 )return -EFAULT ; - ret = -ENOMEM ; feature_flags =calloc (results.count,sizeof (feature_flags[0 ])); -if (!feature_flags) +if (!feature_flags) { + ret = -ENOMEM ; goto out_free; + }/* map netdev features to legacy flags */ for (i =0 ; i < results.count; i++) {@@ -184,7 +185,7 @@ int dump_features(const struct nlattr *const *tb, out_free:free (feature_flags); -return 0 ; +return ret; }int features_reply_cb (const struct nlmsghdr *nlhdr,void *data)Signature · 1 line
2.26.2The patch is correct but relying on ret staying zero through the whole function is rather fragile (it could break when adding more checks in the future) and it also isn't consistent with the way this is done in other functions.
Quoted 4 lines
AFAICS you could omit the first hunk and just add "ret = 0" above the
out_freelabel.Michal
OK, will do.
Ivan