Skip to contentSkip to conversation

auxdisplay: lcd2s: Fix lcd2s_redefine_char() feature

PATCH In review Andy Shevchenko 3 participants 10 messages Feb 23, 2022 – Mar 3, 2022 Auxiliary display drivers

lore
Share & tools
lore
#1 lore raw

PATCH 2/3 auxdisplay: lcd2s: Fix memory leak in ->remove()

In reply to Andy Shevchenko

Once allocated the struct lcd2s_data is never freed. Fix the memory leak by switching to devm_kzalloc().

Fixes 8c9108d014c5 ("auxdisplay: add a driver for lcd2s character display")
Cc Lars Poeschel <[email protected]>
---
 drivers/auxdisplay/lcd2s.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/auxdisplay/lcd2s.c +7 -11
index ea9d75ad4f16..7b9cfbbaf007 100644
@@ -298,6 +298,10 @@ static int lcd2s_i2c_probe(struct i2c_client *i2c,
 			I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
 		return -EIO;
 
+	lcd2s = devm_kzalloc(&i2c->dev, sizeof(*lcd2s), GFP_KERNEL);
+	if (!lcd2s)
+		return -ENOMEM;
+
 	/* Test, if the display is responding */
 	err = lcd2s_i2c_smbus_write_byte(i2c, LCD2S_CMD_DISPLAY_OFF);
 	if (err < 0)
@@ -307,12 +311,6 @@ static int lcd2s_i2c_probe(struct i2c_client *i2c,
 	if (!lcd)
 		return -ENOMEM;
 
-	lcd2s = kzalloc(sizeof(struct lcd2s_data), GFP_KERNEL);
-	if (!lcd2s) {
-		err = -ENOMEM;
-		goto fail1;
-	}
-
 	lcd->drvdata = lcd2s;
 	lcd2s->i2c = i2c;
 	lcd2s->charlcd = lcd;
@@ -321,24 +319,22 @@ static int lcd2s_i2c_probe(struct i2c_client *i2c,
 	err = device_property_read_u32(&i2c->dev, "display-height-chars",
 			&lcd->height);
 	if (err)
-		goto fail2;
+		goto fail1;
 
 	err = device_property_read_u32(&i2c->dev, "display-width-chars",
 			&lcd->width);
 	if (err)
-		goto fail2;
+		goto fail1;
 
 	lcd->ops = &lcd2s_ops;
 
 	err = charlcd_register(lcd2s->charlcd);
 	if (err)
-		goto fail2;
+		goto fail1;
 
 	i2c_set_clientdata(i2c, lcd2s);
 	return 0;
 
-fail2:
-	kfree(lcd2s);
 fail1:
 	kfree(lcd);
 	return err;
Signature · 1 line
2.34.1
#0 lore raw

PATCH 1/3 auxdisplay: lcd2s: Fix lcd2s_redefine_char() feature

It seems that the lcd2s_redefine_char() has never been properly tested. The buffer is filled by DEF_CUSTOM_CHAR command followed by the character number (from 0 to 7), but immediately after that these bytes are got rewritten by the decoded hex stream.

Fix the index to fill the buffer after the command and number.

Fixes 8c9108d014c5 ("auxdisplay: add a driver for lcd2s character display")
Cc Lars Poeschel <[email protected]>
---
 drivers/auxdisplay/lcd2s.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/auxdisplay/lcd2s.c +1 -1
index 38ba08628ccb..ea9d75ad4f16 100644
@@ -238,7 +238,7 @@ static int lcd2s_redefine_char(struct charlcd *lcd, char *esc)
 	if (buf[1] > 7)
 		return 1;
 
-	i = 0;
+	i = 2;
 	shift = 0;
 	value = 0;
 	while (*esc && i < LCD2S_CHARACTER_SIZE + 2) {
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/3 auxdisplay: lcd2s: Use proper API to free the instance of charlcd object

While it might work, the current approach is fragile in a few ways:
- whenever members in the structure are shuffled, the pointer will be wrong
- the resource freeing may include more than covered by kfree()

Fix this by using charlcd_free() call instead of kfree().

Fixes 8c9108d014c5 ("auxdisplay: add a driver for lcd2s character display")
Cc Lars Poeschel <[email protected]>
---
 drivers/auxdisplay/lcd2s.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/auxdisplay/lcd2s.c +2 -2
index 7b9cfbbaf007..2578b2d45439 100644
@@ -336,7 +336,7 @@ static int lcd2s_i2c_probe(struct i2c_client *i2c,
 	return 0;
 
 fail1:
-	kfree(lcd);
+	charlcd_free(lcd2s->charlcd);
 	return err;
 }
 
@@ -345,7 +345,7 @@ static int lcd2s_i2c_remove(struct i2c_client *i2c)
 	struct lcd2s_data *lcd2s = i2c_get_clientdata(i2c);
 
 	charlcd_unregister(lcd2s->charlcd);
-	kfree(lcd2s->charlcd);
+	charlcd_free(lcd2s->charlcd);
 	return 0;
 }
 
Signature · 1 line
2.34.1
#5 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

It seems that the lcd2s_redefine_char() has never been properly tested. The buffer is filled by DEF_CUSTOM_CHAR command followed by the character number (from 0 to 7), but immediately after that these bytes are got rewritten by the decoded hex stream.

Fix the index to fill the buffer after the command and number.

Any comments on this, please? There are fixes and seems auxdisplay subsystem is a bit lagging... Should we worry about?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer Reviewed-by on v1 1/3
#6 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

It seems that the lcd2s_redefine_char() has never been properly tested. The buffer is filled by DEF_CUSTOM_CHAR command followed by the character number (from 0 to 7), but immediately after that these bytes are got rewritten by the decoded hex stream.

Fix the index to fill the buffer after the command and number.

Fixes 8c9108d014c5 ("auxdisplay: add a driver for lcd2s character display")
Cc Lars Poeschel <[email protected]>

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#7 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko

It seems that the lcd2s_redefine_char() has never been properly tested. The buffer is filled by DEF_CUSTOM_CHAR command followed by the character number (from 0 to 7), but immediately after that these bytes are got rewritten by the decoded hex stream.

Fix the index to fill the buffer after the command and number.

Thanks Andy, queued.

Cheers,
Miguel

#4 lore raw

Re: [PATCH v1 3/3] auxdisplay: lcd2s: Use proper API to free the instance of charlcd object

In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

While it might work, the current approach is fragile in a few ways:
- whenever members in the structure are shuffled, the pointer will be wrong
- the resource freeing may include more than covered by kfree()

Fix this by using charlcd_free() call instead of kfree().

Fixes 8c9108d014c5 ("auxdisplay: add a driver for lcd2s character display")
Cc Lars Poeschel <[email protected]>

Thanks Andy, queued.

Cheers,
Miguel

#9 lore raw
Quoted 6 lines · Miguel Ojeda
Quoted 2 lines · Miguel Ojeda

Thanks Andy, queued.

By the way, I fixed a typo in the commit message.

Thank you, Miguel!

Signature · 2 lines
With Best Regards,
Andy Shevchenko