diff mbox series

[ethtool,1/2] fix memory leaks in do_sfeatures()

Message ID 20200925070527.1001190-1-ivecera@redhat.com
State Accepted
Delegated to: Michal Kubecek
Headers show
Series [ethtool,1/2] fix memory leaks in do_sfeatures() | expand

Commit Message

Ivan Vecera Sept. 25, 2020, 7:05 a.m. UTC
Memory blocks referenced by new_state and old_state are never freed.
For efeatures there is no need to check pointer as free() can be called
with NULL parameter.

Fixes: 6042804cf6ec ("Change -k/-K options to use ETHTOOL_{G,S}FEATURES")

Cc: Michal Kubecek <mkubecek@suse.cz>
Signed-off-by: Ivan Vecera <ivecera@redhat.com>
---
 ethtool.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Comments

Michal Kubecek Sept. 28, 2020, 3:37 p.m. UTC | #1
On Fri, Sep 25, 2020 at 09:05:26AM +0200, Ivan Vecera wrote:
> Memory blocks referenced by new_state and old_state are never freed.
> For efeatures there is no need to check pointer as free() can be called
> with NULL parameter.
> 
> Fixes: 6042804cf6ec ("Change -k/-K options to use ETHTOOL_{G,S}FEATURES")
> 
> Cc: Michal Kubecek <mkubecek@suse.cz>
> Signed-off-by: Ivan Vecera <ivecera@redhat.com>
> ---

Applied, thank you.

Michal
diff mbox series

Patch

diff --git a/ethtool.c b/ethtool.c
index ab9b4577cbce..f2d568722272 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -2392,9 +2392,10 @@  static int do_sfeatures(struct cmd_context *ctx)
 	int any_changed = 0, any_mismatch = 0;
 	u32 off_flags_wanted = 0;
 	u32 off_flags_mask = 0;
-	struct ethtool_sfeatures *efeatures;
+	struct ethtool_sfeatures *efeatures = NULL;
+	struct feature_state *old_state = NULL;
+	struct feature_state *new_state = NULL;
 	struct cmdline_info *cmdline_features;
-	struct feature_state *old_state, *new_state;
 	struct ethtool_value eval;
 	unsigned int i, j;
 	int err, rc;
@@ -2418,8 +2419,6 @@  static int do_sfeatures(struct cmd_context *ctx)
 		memset(efeatures->features, 0,
 		       FEATURE_BITS_TO_BLOCKS(defs->n_features) *
 		       sizeof(efeatures->features[0]));
-	} else {
-		efeatures = NULL;
 	}
 
 	/* Generate cmdline_info for legacy flags and kernel-named
@@ -2578,9 +2577,11 @@  static int do_sfeatures(struct cmd_context *ctx)
 	rc = 0;
 
 err:
+	free(new_state);
+	free(old_state);
 	free(defs);
-	if (efeatures)
-		free(efeatures);
+	free(efeatures);
+
 	return rc;
 }