Skip to contentSkip to conversation

auxdisplay: charlcd: Fix and clean up handling of x/y commands

RFC PATCH In review Miguel Ojeda 3 participants 4 messages Feb 27, 2018 Auxiliary display drivers

lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 1/1 auxdisplay: charlcd: Fix and clean up handling of x/y commands

The current version is not parsing multiple x/y commands as the code originally intended. On top of that, kstrtoul() expects NULL-terminated strings. Finally, the code does two passes over the string.

Some explanations about the supported syntax are added as well.

Cc Willy Tarreau <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Robert Abel <[email protected]>

---
Disregard v2, this is much more clear. I am too tired :)

This one and v2 are the loop versions, v1 is the kstrto*() version. I also added some more comments on top of the v2.

 drivers/auxdisplay/charlcd.c | 95 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 78 insertions(+), 17 deletions(-)
drivers/auxdisplay/charlcd.c +78 -17
index 642afd88870b..36930bce5a1f 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/atomic.h>
+#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
 #include <linux/miscdevice.h>
@@ -292,6 +293,79 @@ static int charlcd_init_display(struct charlcd *lcd)
 	return 0;
 }
 
+/*
+ * Parses an unsigned integer from a string, until a non-digit character
+ * is found. The empty string is not accepted. No overflow checks are done.
+ *
+ * Returns whether the parsing was successful. Only in that case
+ * the output parameters are written to.
+ *
+ * TODO: If the kernel adds an inplace version of kstrtoul(), this function
+ * could be easily replaced by that.
+ */
+static bool parse_n(const char *s, unsigned long *res, const char **next_s)
+{
+	if (!isdigit(*s))
+		return false;
+
+	*res = 0;
+	while (isdigit(*s)) {
+		*res = *res * 10 + (*s - '0');
+		++s;
+	}
+
+	*next_s = s;
+	return true;
+}
+
+/*
+ * Parses a movement command of the form "(.*);", where the group can be
+ * any number of subcommands of the form "(x|y)[0-9]+".
+ *
+ * Returns whether the command is valid. The position arguments are
+ * only written if the parsing was successful.
+ *
+ * For instance:
+ *   - ";"          returns (<original x>, <original y>).
+ *   - "x1;"        returns (1, <original y>).
+ *   - "y2x1;"      returns (1, 2).
+ *   - "x12y34x56;" returns (56, 34).
+ *   - ""           fails.
+ *   - "x"          fails.
+ *   - "x;"         fails.
+ *   - "x1"         fails.
+ *   - "xy12;"      fails.
+ *   - "x12yy12;"   fails.
+ *   - "xx"         fails.
+ */
+static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
+{
+	unsigned long new_x = *x;
+	unsigned long new_y = *y;
+
+	for (;;) {
+		if (!*s)
+			return false;
+
+		if (*s == ';')
+			break;
+
+		if (*s == 'x') {
+			if (!parse_n(s + 1, &new_x, &s))
+				return false;
+		} else if (*s == 'y') {
+			if (!parse_n(s + 1, &new_y, &s))
+				return false;
+		} else {
+			return false;
+		}
+	}
+
+	*x = new_x;
+	*y = new_y;
+	return true;
+}
+
 /*
  * These are the file operation function for user access to /dev/lcd
  * This function can also be called from inside the kernel, by
@@ -469,24 +543,11 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)
 	}
 	case 'x':	/* gotoxy : LxXXX[yYYY]; */
 	case 'y':	/* gotoxy : LyYYY[xXXX]; */
-		if (!strchr(esc, ';'))
-			break;
-
-		while (*esc) {
-			if (*esc == 'x') {
-				esc++;
-				if (kstrtoul(esc, 10, &priv->addr.x) < 0)
-					break;
-			} else if (*esc == 'y') {
-				esc++;
-				if (kstrtoul(esc, 10, &priv->addr.y) < 0)
-					break;
-			} else {
-				break;
-			}
-		}
+		/* If the command is valid, move to the new address */
+		if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
+			charlcd_gotoxy(lcd);
 
-		charlcd_gotoxy(lcd);
+		/* Regardless of its validity, mark as processed */
 		processed = 1;
 		break;
 	}
Signature · 1 line
2.14.1
#1 lore raw
Quoted 6 lines · Miguel Ojeda

The current version is not parsing multiple x/y commands as the code originally intended. On top of that, kstrtoul() expects NULL-terminated strings. Finally, the code does two passes over the string.

Some explanations about the supported syntax are added as well.

Looks much better, thank you ;-)

Willy

#2 lore raw
In reply to Miguel Ojeda

On 27 Feb 2018 23:09, Miguel Ojeda wrote:> @@ -469,24 +543,11 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)

drivers/auxdisplay/charlcd.c static inline int handle_lcd_special_code(struct charlcd *lcd) · line 473

Quoted 5 lines
drivers/auxdisplay/charlcd.c +0 -2
 	}
 	case 'x':	/* gotoxy : LxXXX[yYYY]; */
 	case 'y':	/* gotoxy : LyYYY[xXXX]; */
-		if (!strchr(esc, ';'))
-			break;

Might want to keep this. It's in line with all other cases and prevents calling parse_xy with input that has no chance of being correct due to missing final ';'.

drivers/auxdisplay/charlcd.c static inline int handle_lcd_special_code(struct charlcd *lcd) · line 548

Quoted 3 lines
drivers/auxdisplay/charlcd.c +3 -0
+		/* If the command is valid, move to the new address */
+		if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
+			charlcd_gotoxy(lcd);

While not in the original code, the inputs are now not clamped to width and height.
That means for a two-line display ^[[Ly02; will actually end up on line y = 1, not y = 2 % 2 = 0, because the four-line display logic bumps the address up.
The same goes for going over lcd->width/lcd->bwidth in the x coordinate. For four-line displays that ends up going to the line y + 2, because the buffer is split in the middle.
The distinction between lcd->width and lcd->bwidth depends on whether it makes sense to put the cursor outside the visible area or not.

Regards,

Robert

#3 lore raw
Quoted 11 lines · Robert Abel

On 27 Feb 2018 23:09, Miguel Ojeda wrote:> @@ -469,24 +543,11 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)

Review

Quoted 5 lines
      }
      case 'x':       /* gotoxy : LxXXX[yYYY]; */
      case 'y':       /* gotoxy : LyYYY[xXXX]; */
(quoted diff) +0 -2
-             if (!strchr(esc, ';'))
-                     break;

Might want to keep this. It's in line with all other cases and prevents calling parse_xy with input that has no chance of being correct due to missing final ';'.

I agree performance is not that critical here and the string would probably be in cache anyway, but I would say it is better to fix parse_xy() if it is not doing what it specifies.

Quoted 5 lines

drivers/auxdisplay/charlcd.c static inline int handle_lcd_special_code(struct charlcd *lcd) · line 548

Quoted 4 lines
drivers/auxdisplay/charlcd.c +3 -0
+             /* If the command is valid, move to the new address */
+             if (parse_xy(esc, &priv->addr.x, &priv->addr.y))
+                     charlcd_gotoxy(lcd);
Quoted 10 lines

While not in the original code, the inputs are now not clamped to width and height.
That means for a two-line display ^[[Ly02; will actually end up on line y = 1, not y = 2 % 2 = 0, because the four-line display logic bumps the address up.
The same goes for going over lcd->width/lcd->bwidth in the x coordinate. For four-line displays that ends up going to the line y + 2, because the buffer is split in the middle.
The distinction between lcd->width and lcd->bwidth depends on whether it makes sense to put the cursor outside the visible area or not.

Don't worry, the RFC patch is meant to agree on a fix for the multiple x/y command code. The clamping and other stuff can still go in in another patch of yours :)

Cheers,
Miguel

Quoted 4 lines

Regards,

Robert