Skip to contentSkip to conversation

media: ccs: Clean up parsed CCS static data on parse failure

PATCH In review Sakari Ailus 2 participants 4 messages Dec 5, 2024

Subsystems (2)
lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

PATCH v3 1/1 media: ccs: Clean up parsed CCS static data on parse failure

ccs_data_parse() releases the allocated in-memory data structure when the parser fails, but it does not clean up parsed metadata that is there to help access the actual data. Do that, in order to return the data structure in a sane state.

Fixes a6b396f410b1 ("media: ccs: Add CCS static data parser library")

---
since v2:

- Properly clean up after all error cases.

 drivers/media/i2c/ccs/ccs-data.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/i2c/ccs/ccs-data.c +7 -5
index 9d42137f4799..2591dba51e17 100644
@@ -10,6 +10,7 @@
 #include <linux/limits.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 
 #include "ccs-data-defs.h"
 
@@ -948,15 +949,15 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, verbose);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = bin_backing_alloc(&bin);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, false);
 	if (rval)
-		goto out_free;
+		goto out_cleanup;
 
 	if (verbose && ccsdata->version)
 		print_ccs_data_version(dev, ccsdata->version);
@@ -965,15 +966,16 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 		rval = -EPROTO;
 		dev_dbg(dev, "parsing mismatch; base %p; now %p; end %p\n",
 			bin.base, bin.now, bin.end);
-		goto out_free;
+		goto out_cleanup;
 	}
 
 	ccsdata->backing = bin.base;
 
 	return 0;
 
-out_free:
+out_cleanup:
 	kvfree(bin.base);
+	memset(ccsdata, 0, sizeof(*ccsdata));
 
 	return rval;
 }
Signature · 1 line
2.39.5
#1 lore raw

Hi Sakari,

Quoted 52 lines · Sakari Ailus

ccs_data_parse() releases the allocated in-memory data structure when the parser fails, but it does not clean up parsed metadata that is there to help access the actual data. Do that, in order to return the data structure in a sane state.

Fixes a6b396f410b1 ("media: ccs: Add CCS static data parser library")

---
since v2:

- Properly clean up after all error cases.

 drivers/media/i2c/ccs/ccs-data.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/i2c/ccs/ccs-data.c +4 -3
index 9d42137f4799..2591dba51e17 100644
@@ -10,6 +10,7 @@
 #include <linux/limits.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 
 #include "ccs-data-defs.h"
 
@@ -948,15 +949,15 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, verbose);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = bin_backing_alloc(&bin);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, false);
 	if (rval)
-		goto out_free;
+		goto out_cleanup;
 
 	if (verbose && ccsdata->version)
 		print_ccs_data_version(dev, ccsdata->version);
@@ -965,15 +966,16 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 		rval = -EPROTO;
 		dev_dbg(dev, "parsing mismatch; base %p; now %p; end %p\n",
 			bin.base, bin.now, bin.end);

drivers/media/i2c/ccs/ccs-data.c int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data, · line 976

Quoted 10 lines
drivers/media/i2c/ccs/ccs-data.c +2 -2
-		goto out_free;
+		goto out_cleanup;
 	}
 
 	ccsdata->backing = bin.base;
 
 	return 0;
 
-out_free:
+out_cleanup:

Don't you think some kind of logging or at least a dev_dbg() would be helpful here to let the user know that ccs_data_parse() failed ?

Quoted 8 lines
drivers/media/i2c/ccs/ccs-data.c +1 -0
 	kvfree(bin.base);
+	memset(ccsdata, 0, sizeof(*ccsdata));
 
 	return rval;
 }
Signature · 1 line
2.39.5

--
Kind Regards
Mehdi Djait

author maintainer
#2 lore raw

Hi Mehdi,

Thanks for the review.

Quoted 61 lines · Mehdi Djait

Hi Sakari,

Quoted 58 lines · Sakari Ailus

ccs_data_parse() releases the allocated in-memory data structure when the parser fails, but it does not clean up parsed metadata that is there to help access the actual data. Do that, in order to return the data structure in a sane state.

Fixes a6b396f410b1 ("media: ccs: Add CCS static data parser library")

---
since v2:

- Properly clean up after all error cases.

 drivers/media/i2c/ccs/ccs-data.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/i2c/ccs/ccs-data.c +5 -4
index 9d42137f4799..2591dba51e17 100644
@@ -10,6 +10,7 @@
 #include <linux/limits.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 
 #include "ccs-data-defs.h"
 
@@ -948,15 +949,15 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, verbose);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = bin_backing_alloc(&bin);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, false);
 	if (rval)
-		goto out_free;
+		goto out_cleanup;
 
 	if (verbose && ccsdata->version)
 		print_ccs_data_version(dev, ccsdata->version);
@@ -965,15 +966,16 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 		rval = -EPROTO;
 		dev_dbg(dev, "parsing mismatch; base %p; now %p; end %p\n",
 			bin.base, bin.now, bin.end);
-		goto out_free;
+		goto out_cleanup;
 	}
 
 	ccsdata->backing = bin.base;
 
Quoted 7 lines

drivers/media/i2c/ccs/ccs-data.c int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data, · line 976

Quoted 4 lines
drivers/media/i2c/ccs/ccs-data.c +1 -1
 	return 0;
 
-out_free:
+out_cleanup:

Don't you think some kind of logging or at least a dev_dbg() would be helpful here to let the user know that ccs_data_parse() failed ?

Good question. I think it's a good idea to print a warning, probably on warning level. I alsoI think it should be a separate patch.

Could you write one? :-)

Quoted 6 lines
Quoted 6 lines
drivers/media/i2c/ccs/ccs-data.c +1 -0
 	kvfree(bin.base);
+	memset(ccsdata, 0, sizeof(*ccsdata));
 
 	return rval;
 }
Signature · 3 lines
Kind regards,

Sakari Ailus
Reviewed-by
#3 lore raw

Hi Sakari,

Quoted 74 lines · Sakari Ailus

Hi Mehdi,

Thanks for the review.

Quoted 61 lines · Mehdi Djait

Hi Sakari,

Quoted 58 lines · Sakari Ailus

ccs_data_parse() releases the allocated in-memory data structure when the parser fails, but it does not clean up parsed metadata that is there to help access the actual data. Do that, in order to return the data structure in a sane state.

Fixes a6b396f410b1 ("media: ccs: Add CCS static data parser library")

---
since v2:

- Properly clean up after all error cases.

 drivers/media/i2c/ccs/ccs-data.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/i2c/ccs/ccs-data.c +5 -4
index 9d42137f4799..2591dba51e17 100644
@@ -10,6 +10,7 @@
 #include <linux/limits.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 
 #include "ccs-data-defs.h"
 
@@ -948,15 +949,15 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, verbose);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = bin_backing_alloc(&bin);
 	if (rval)
-		return rval;
+		goto out_cleanup;
 
 	rval = __ccs_data_parse(&bin, ccsdata, data, len, dev, false);
 	if (rval)
-		goto out_free;
+		goto out_cleanup;
 
 	if (verbose && ccsdata->version)
 		print_ccs_data_version(dev, ccsdata->version);
@@ -965,15 +966,16 @@ int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data,
 		rval = -EPROTO;
 		dev_dbg(dev, "parsing mismatch; base %p; now %p; end %p\n",
 			bin.base, bin.now, bin.end);
-		goto out_free;
+		goto out_cleanup;
 	}
 
 	ccsdata->backing = bin.base;
 
Quoted 7 lines

drivers/media/i2c/ccs/ccs-data.c int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data, · line 976

Quoted 4 lines
drivers/media/i2c/ccs/ccs-data.c +1 -1
 	return 0;
 
-out_free:
+out_cleanup:

Don't you think some kind of logging or at least a dev_dbg() would be helpful here to let the user know that ccs_data_parse() failed ?

Quoted 4 lines

Good question. I think it's a good idea to print a warning, probably on warning level. I alsoI think it should be a separate patch.

Could you write one? :-)

Yes, I will take care of it.

With that

Quoted 6 lines
Quoted 6 lines

drivers/media/i2c/ccs/ccs-data.c int ccs_data_parse(struct ccs_data_container *ccsdata, const void *data, · line 980

Quoted 6 lines
drivers/media/i2c/ccs/ccs-data.c +1 -0
 	kvfree(bin.base);
+	memset(ccsdata, 0, sizeof(*ccsdata));
 
 	return rval;

--
Kind Regards
Mehdi Djait