Skip to contentSkip to conversation

Fixing bug that would segmentation fault

PATCH Superseded Luiz Sampaio 2 participants 10 messages Nov 7, 2021 – Nov 24, 2021 Auxiliary display drivers

lore
Share & tools
lore
#0 lore raw

cover Fixing bug that would segmentation fault

This series of patches consists of one patch fixing a simple coding style issue and one patch fixing a bug that would cause segmentation fault. Basically, there was a pointer that was being dereferenced without testing if the pointer exists. This patch adds a protection, returning EFAULT in case the pointer is NULL.

Luiz Sampaio (2):
auxdisplay: charlcd: fixing coding style issue
auxdisplay: charlcd: checking for pointer reference before
dereferencing

 drivers/auxdisplay/charlcd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/2 auxdisplay: charlcd: fixing coding style issue

Removing 'int' from 'unsigned long int' declaration, which is unnecessary.

---
 drivers/auxdisplay/charlcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/auxdisplay/charlcd.c +1 -1
index 304accde365c..cca3b600c0ba 100644
@@ -37,7 +37,7 @@ struct charlcd_priv {
 	bool must_clear;
 
 	/* contains the LCD config state */
-	unsigned long int flags;
+	unsigned long flags;
 
 	/* Current escape sequence and it's length or -1 if outside */
 	struct {
Signature · 1 line
2.33.1
#2 lore raw

Re: [PATCH 1/2] auxdisplay: charlcd: fixing coding style issue

Quoted 2 lines · Luiz Sampaio

Removing 'int' from 'unsigned long int' declaration, which is unnecessary.

Not sure if this is a rule, but it is good to be consistent with the rest of auxdisplay. Thanks!

Cheers,
Miguel

#3 lore raw

PATCH 2/2 auxdisplay: charlcd: checking for pointer reference before dereferencing

In reply to Luiz Sampaio

Check if the pointer lcd->ops->init_display exists before dereferencing it. If a driver called charlcd_init() without defining the ops, this would return segmentation fault, as happened to me when implementing a charlcd driver. Checking the pointer before dereferencing protects from segmentation fault.

---
 drivers/auxdisplay/charlcd.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/auxdisplay/charlcd.c +3 -0
index cca3b600c0ba..47363fb2fe94 100644
@@ -578,6 +578,9 @@ static int charlcd_init(struct charlcd *lcd)
 	 * Since charlcd_init_display() needs to write data, we have to
 	 * enable mark the LCD initialized just before.
 	 */
+	if (!lcd->ops->init_display)
+		return -EFAULT;
+
 	ret = lcd->ops->init_display(lcd);
 	if (ret)
 		return ret;
Signature · 1 line
2.33.1
#4 lore raw

Re: [PATCH 2/2] auxdisplay: charlcd: checking for pointer reference before dereferencing

Quoted 6 lines · Luiz Sampaio

Check if the pointer lcd->ops->init_display exists before dereferencing it. If a driver called charlcd_init() without defining the ops, this would return segmentation fault, as happened to me when implementing a charlcd driver. Checking the pointer before dereferencing protects from segmentation fault.

It can't hurt -- thanks! I think EINVAL makes more sense here, also we could use WARN_ON:

    if (WARN_ON(!lcd->ops->init_display))
        return -EINVAL;

Cheers,
Miguel

#5 lore raw

v2 cover Fixing bug that would segmentation fault

This series of patches consists of one patch fixing a simple coding style issue and one patch fixing a bug that would cause segmentation fault. Basically, there was a pointer that was being dereferenced without testing if the pointer exists. This patch adds a protection, returning EFAULT in case the pointer is NULL.

Changes in v2:
- Changed return to -EINVAL and using WARN_ON as suggested
- Note in response for Miguel's comment: for the first patch, I ran the script './scripts/checkpatch.pl --file --terse' to see with the file
had any coding style issue. That was when I was suggested to remove 'int' from 'unsigned long' declaration

Luiz Sampaio (2):
auxdisplay: charlcd: fixing coding style issue
auxdisplay: charlcd: checking for pointer reference before
dereferencing

 drivers/auxdisplay/charlcd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.33.1
#6 lore raw

PATCH v2 1/2 auxdisplay: charlcd: fixing coding style issue

Removing 'int' from 'unsigned long int' declaration, which is unnecessary.

---
 drivers/auxdisplay/charlcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/auxdisplay/charlcd.c +1 -1
index 304accde365c..cca3b600c0ba 100644
@@ -37,7 +37,7 @@ struct charlcd_priv {
 	bool must_clear;
 
 	/* contains the LCD config state */
-	unsigned long int flags;
+	unsigned long flags;
 
 	/* Current escape sequence and it's length or -1 if outside */
 	struct {
Signature · 1 line
2.33.1
Applied
#7 lore raw

Re: [PATCH v2 1/2] auxdisplay: charlcd: fixing coding style issue

Quoted 4 lines · Luiz Sampaio

Removing 'int' from 'unsigned long int' declaration, which is unnecessary.

Queued up, thanks!

Cheers,
Miguel

#8 lore raw

PATCH v2 2/2 auxdisplay: charlcd: checking for pointer reference before dereferencing

In reply to Luiz Sampaio

Check if the pointer lcd->ops->init_display exists before dereferencing it. If a driver called charlcd_init() without defining the ops, this would return segmentation fault, as happened to me when implementing a charlcd driver. Checking the pointer before dereferencing protects from segmentation fault.

---
 drivers/auxdisplay/charlcd.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/auxdisplay/charlcd.c +3 -0
index cca3b600c0ba..6d309e4971b6 100644
@@ -578,6 +578,9 @@ static int charlcd_init(struct charlcd *lcd)
 	 * Since charlcd_init_display() needs to write data, we have to
 	 * enable mark the LCD initialized just before.
 	 */
+	if (WARN_ON(!lcd->ops->init_display))
+		return -EINVAL;
+
 	ret = lcd->ops->init_display(lcd);
 	if (ret)
 		return ret;
Signature · 1 line
2.33.1
Applied
#9 lore raw

Re: [PATCH v2 2/2] auxdisplay: charlcd: checking for pointer reference before dereferencing

Quoted 8 lines · Luiz Sampaio

Check if the pointer lcd->ops->init_display exists before dereferencing it. If a driver called charlcd_init() without defining the ops, this would return segmentation fault, as happened to me when implementing a charlcd driver. Checking the pointer before dereferencing protects from segmentation fault.

Queued up, thanks!

Cheers,
Miguel