Skip to contentSkip to conversation

lib: add sputchar() helper

PATCH Superseded Yury Norov 4 participants 7 messages Sep 4, 2021 – Sep 7, 2021

Subsystems (4)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

cover lib: add sputchar() helper

This series adds sputchar() helper and coccinelle script that I used to find candidates for cleanup.

Yury Norov (2):
lib/vsprintf: add sputchar()
coccinelle script

 include/linux/kernel.h                 |   8 +
 lib/string_helpers.c                   |  56 ++-----
 lib/vsprintf.c                         | 222 ++++++++-----------------
 scripts/coccinelle/misc/sputchar.cocci | 190 +++++++++++++++++++++
 4 files changed, 280 insertions(+), 196 deletions(-)
 create mode 100644 scripts/coccinelle/misc/sputchar.cocci
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH v2 1/2 lib: add sputchar() helper

There are 47 occurrences of the code snippet like this:

	if (buf < end)
	        *buf = ' ';
	++buf;

This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86.

v2: cleanup cases discovered with coccinelle script.

---
 include/linux/kernel.h |   8 ++
 lib/string_helpers.c   |  56 +++--------
 lib/vsprintf.c         | 222 +++++++++++++----------------------------
 3 files changed, 90 insertions(+), 196 deletions(-)
include/linux/kernel.h +8 -0
index 1b2f0a7e00d6..01941ed2f5aa 100644
@@ -283,6 +283,14 @@ extern char *bin2hex(char *dst, const void *src, size_t count);
 
 bool mac_pton(const char *s, u8 *mac);
 
+static inline char *sputchar(char *buf, const char *end, char c)
+{
+	if (buf < end)
+		*buf = c;
+
+	return buf + 1;
+}
+
 /*
  * General tracing related utility functions - trace_printk(),
  * tracing_on/tracing_off and tracing_start()/tracing_stop
lib/string_helpers.c +14 -42

86 lines not on this page · raw message

lib/vsprintf.c +68 -154

363 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw
Quoted 7 lines · Yury Norov

There are 47 occurrences of the code snippet like this:

	if (buf < end)
	        *buf = ' ';
	++buf;

This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86.

I think this patch does little to improve readability.

Perhaps make it void and use something like

	sputchar(*buf++, end, <whateverchar>);

Though the sputchar name doesn't seems particularly intelligible.

lib/string_helpers.c

Quoted 12 lines
include/linux/kernel.h +8 -0
+static inline char *sputchar(char *buf, const char *end, char c)
+{
+	if (buf < end)
+		*buf = c;
+
+	return buf + 1;
+}
+

 /*
  * General tracing related utility functions - trace_printk(),
  * tracing_on/tracing_off and tracing_start()/tracing_stop

lib/string_helpers.c +0 -0

[]

lib/string_helpers.c static bool escape_space(unsigned char c, char **dst, char *end) · line 339

Quoted 12 lines
lib/string_helpers.c +2 -6
@@ -335,12 +335,8 @@ static bool escape_space(unsigned char c, char **dst, char *end)
  		return false;
  	}
 
-	if (out < end)
-		*out = '\\';
-	++out;
-	if (out < end)
-		*out = to;
-	++out;
+	out = sputchar(out, end, '\\');
+	out = sputchar(out, end, to);

etc...

author
#3 lore raw
Quoted 6 lines · Joe Perches
Quoted 5 lines · Yury Norov

There are 47 occurrences of the code snippet like this:

	if (buf < end)
	        *buf = ' ';
	++buf;
Quoted 8 lines
Quoted 2 lines

This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86.

I think this patch does little to improve readability.

Perhaps make it void and use something like

	sputchar(*buf++, end, <whateverchar>);

It's better, thank you.

Quoted 1 line

Though the sputchar name doesn't seems particularly intelligible.

I'm OK with any better name.

Thanks,
Yury

Quoted 28 lines

lib/string_helpers.c

Quoted 12 lines
include/linux/kernel.h +8 -0
+static inline char *sputchar(char *buf, const char *end, char c)
+{
+	if (buf < end)
+		*buf = c;
+
+	return buf + 1;
+}
+

 /*
  * General tracing related utility functions - trace_printk(),
  * tracing_on/tracing_off and tracing_start()/tracing_stop

lib/string_helpers.c +0 -0

[]

lib/string_helpers.c static bool escape_space(unsigned char c, char **dst, char *end) · line 339

Quoted 12 lines
lib/string_helpers.c +2 -6
@@ -335,12 +335,8 @@ static bool escape_space(unsigned char c, char **dst, char *end)
  		return false;
  	}
 
-	if (out < end)
-		*out = '\\';
-	++out;
-	if (out < end)
-		*out = to;
-	++out;
+	out = sputchar(out, end, '\\');
+	out = sputchar(out, end, to);

etc...

#4 lore raw
In reply to Yury Norov
Quoted 5 lines · Yury Norov

There are 47 occurrences of the code snippet like this:

	if (buf < end)
	        *buf = ' ';
	++buf;
Quoted 8 lines

This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86.

v2: cleanup cases discovered with coccinelle script.

---
 include/linux/kernel.h |   8 ++

Sorry, but 47 cases, mostly in one .c file, is not enough justification for adding yet another piece of random code to the-dumping-ground-which-is-kernel.h, especially since this helper is very specific to the needs of the vsnprintf() implementation, so extremely unlikely to find other users.

I'm also not a fan of the sputchar name - it's unreadable at first glance, and when you figure out it's "a _s_tring version of putchar", that doesn't help, because its semantics are nothing like the stdio putchar.

Rasmus

maintainer
#5 lore raw
Quoted 15 lines · Rasmus Villemoes
Quoted 5 lines · Yury Norov

There are 47 occurrences of the code snippet like this:

	if (buf < end)
	        *buf = ' ';
	++buf;
Quoted 8 lines

This patch adds a helper function sputchar() to replace opencoding. It adds a lot to readability, and also saves 43 bytes of text on x86.

v2: cleanup cases discovered with coccinelle script.

---
 include/linux/kernel.h |   8 ++
Quoted 5 lines

Sorry, but 47 cases, mostly in one .c file, is not enough justification for adding yet another piece of random code to the-dumping-ground-which-is-kernel.h, especially since this helper is very specific to the needs of the vsnprintf() implementation, so extremely unlikely to find other users.

What about putting it into include/linux/string_helpers.h ?

Or create include/linux/vsprintf.h ?

Quoted 3 lines

I'm also not a fan of the sputchar name - it's unreadable at first glance, and when you figure out it's "a _s_tring version of putchar", that doesn't help, because its semantics are nothing like the stdio putchar.

I do not like the name either.

What about vsprintf_putc(buf, end, c) or vsp_putc(buf, end, c)?

Well, the ugly thing are the three parameters. Which brings an idea of

	struct vsprintf_buffer {       // or struct vsp_buf
		char *buf,
		char *end,
	}

and using vprintf_putc(vsp_buf, c) or vsp_putc(vsp_buf, c).

The change would look like:

From 32119545392f560be42c7042721811a3177fb1dc Mon Sep 17 00:00:00 2001
From: Petr Mladek <[email protected]>
Date: Tue, 7 Sep 2021 11:31:22 +0200
Subject: [PATCH] vsprintf: Sample use of struct vsp_buf

This is just partial replacement of [buf,end] couple with struct vsp_buf.

The intention is to see how the code would look like. It does not compile.

---
 lib/vsprintf.c | 85 +++++++++++++++++++++-----------------------------
 1 file changed, 35 insertions(+), 50 deletions(-)
lib/vsprintf.c +35 -50
index 3bcb7be03f93..963c9212141d 100644
@@ -446,8 +446,9 @@ static_assert(sizeof(struct printf_spec) == 8);
 #define PRECISION_MAX ((1 << 15) - 1)
 
 static noinline_for_stack
-char *number(char *buf, char *end, unsigned long long num,
-	     struct printf_spec spec)
+strcut vsp_buf *number(struct vsp_buf *vsp_buf,
+		       unsigned long long num,
+		       struct printf_spec spec)
 {
 	/* put_dec requires 2-byte alignment of the buffer. */
 	char tmp[3 * sizeof(num)] __aligned(2);
@@ -506,68 +507,52 @@ char *number(char *buf, char *end, unsigned long long num,
 	/* printing 100 using %2d gives "100", not "00" */
 	if (i > precision)
 		precision = i;
+
 	/* leading space padding */
 	field_width -= precision;
 	if (!(spec.flags & (ZEROPAD | LEFT))) {
-		while (--field_width >= 0) {
-			if (buf < end)
-				*buf = ' ';
-			++buf;
-		}
+		while (--field_width >= 0)
+			vsp_putc(vsp_buf, ' ');
 	}
+
 	/* sign */
-	if (sign) {
-		if (buf < end)
-			*buf = sign;
-		++buf;
-	}
+	if (sign)
+		vsp_putc(vsp_buf, sign);
+
 	/* "0x" / "0" prefix */
 	if (need_pfx) {
-		if (spec.base == 16 || !is_zero) {
-			if (buf < end)
-				*buf = '0';
-			++buf;
-		}
+		if (spec.base == 16 || !is_zero)
+			vsp_putc(vps_buf, '0');
 		if (spec.base == 16) {
-			if (buf < end)
-				*buf = ('X' | locase);
-			++buf;
-		}
+			vsp_putc(vps_buf, 'X' | locase);
 	}
+
 	/* zero or space padding */
 	if (!(spec.flags & LEFT)) {
 		char c = ' ' + (spec.flags & ZEROPAD);
 
-		while (--field_width >= 0) {
-			if (buf < end)
-				*buf = c;
-			++buf;
-		}
+		while (--field_width >= 0)
+			vsp_putc(vps_buf, c);
 	}
+
 	/* hmm even more zero padding? */
-	while (i <= --precision) {
-		if (buf < end)
-			*buf = '0';
-		++buf;
-	}
+	while (i <= --precision)
+		vsp_putc(vps_buf, '0');
+
 	/* actual digits of result */
-	while (--i >= 0) {
-		if (buf < end)
-			*buf = tmp[i];
-		++buf;
-	}
+	while (--i >= 0)
+		vsp_putc(vps_buf, tmp[i]);
+
 	/* trailing space padding */
-	while (--field_width >= 0) {
-		if (buf < end)
-			*buf = ' ';
-		++buf;
-	}
+	while (--field_width >= 0)
+		vsp_putc(vps_buf, ' ');
 
 	return buf;
 }
 
 static noinline_for_stack
-char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
+	struct vsp_buf* *special_hex_number(struct vsp_buf *vsp_buf,
+					    unsigned long long num, int size)
 {
 	struct printf_spec spec;
 
@@ -577,7 +562,7 @@ char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
 	spec.base = 16;
 	spec.precision = -1;
 
-	return number(buf, end, num, spec);
+	return number(vsp_buf, num, spec);
 }
 
 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
@@ -646,14 +631,14 @@ static char *string_nocheck(char *buf, char *end, const char *s,
 	return widen_string(buf, len, end, spec);
 }
 
-static char *err_ptr(char *buf, char *end, void *ptr,
+static struct vsp_buf *err_ptr(struct vsp_buf *vsp_buf, void *ptr,
 		     struct printf_spec spec)
 {
 	int err = PTR_ERR(ptr);
 	const char *sym = errname(err);
 
 	if (sym)
-		return string_nocheck(buf, end, sym, spec);
+		return string_nocheck(vsp_buf, sym, spec);
 
 	/*
 	 * Somebody passed ERR_PTR(-1234) or some other non-existing
@@ -662,7 +647,7 @@ static char *err_ptr(char *buf, char *end, void *ptr,
 	 */
 	spec.flags |= SIGN;
 	spec.base = 10;
-	return number(buf, end, err, spec);
+	return number(vsp_buf, err, spec);
 }
 
 /* Be careful: error messages must fit into the given buffer. */
@@ -720,9 +705,9 @@ char *string(char *buf, char *end, const char *s,
 	return string_nocheck(buf, end, s, spec);
 }
 
-static char *pointer_string(char *buf, char *end,
-			    const void *ptr,
-			    struct printf_spec spec)
+static vsp_buf *pointer_string(struct vsp_buf *vsp_buf,
+			       const void *ptr,
+			       struct printf_spec spec)
 {
 	spec.base = 16;
 	spec.flags |= SMALL;
@@ -731,7 +716,7 @@ static char *pointer_string(char *buf, char *end,
 		spec.flags |= ZEROPAD;
 	}
 
-	return number(buf, end, (unsigned long int)ptr, spec);
+	return number(vsp_buf, (unsigned long int)ptr, spec);
 }
 
 /* Make pointers available for printing early in the boot sequence. */
Signature · 1 line
2.26.2
author
#6 lore raw

PATCH 2/2 coccinelle: add script for sputchar()

In reply to Yury Norov

This script find 47 candidates for sputchar() replacement, none of them is false-positive.

---
One test case is commented-out because it causes spatch crash. Coccinelle is installed from ubuntu deb package.

yury:linux$ spatch --version
spatch version 1.0.8 compiled with OCaml version 4.11.1
Flags passed to the configure script: --prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --enable-ocaml --enable-python --enable-opt OCaml scripting support: yes
Python scripting support: yes
Syntax of regular expressions: PCRE

 scripts/coccinelle/misc/sputchar.cocci | 190 +++++++++++++++++++++++++
 1 file changed, 190 insertions(+)
 create mode 100644 scripts/coccinelle/misc/sputchar.cocci
scripts/coccinelle/misc/sputchar.cocci new file +190 -0
new file mode 100644
index 000000000000..23fb7546252f
@@ -0,0 +1,190 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Check for opencoded sputchar() implementation.
+///
+// Confidence: High
+// Copyright: (C) 2021 Yury Norov
+// Options: --no-includes --include-headers
+//
+// Keywords: sputchar
+//
+
+virtual patch
+virtual org
+virtual report
+virtual context
+
+@rpostfix depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if ((buf) < (end)) {
+*		*(buf) = (c);
+*	}
+*	(buf)++;@p
+	...>
+}
+
+@rprefix depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if ((buf) < (end))
+*		*(buf) = (c);
+*	++(buf);@p
+	...>
+}
+
+@rinc1 depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if ((buf) < (end)) {
+*		*(buf) = (c);
+*	}
+*	(buf) += 1;@p
+	...>
+}
+
+@rinc2 depends on !patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+*	if ((buf) < (end)) {
+*		*(buf) = (c);
+*	}
+*	(buf) = (buf) + 1;@p
+	...>
+}
+
+@ppostfix depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+-	if ((buf) < (end)) {
+-		*(buf) = (c);
+-	}
+-	(buf)++;@p
++	buf = sputchar(buf, end, c);
+	...>
+}
+
+// @pprefix depends on patch@
+// identifier func;
+// expression buf, end, c;
+// position p;
+// @@
+//
+// func(...)
+// {
+// 	<...
+// -	if ((buf) < (end)) {
+// -		*(buf) = (c);
+// -	}
+// -	++(buf);
+// +	buf = sputchar(buf, end, c);
+// 	...>
+// }
+
+@pinc1 depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+-	if ((buf) < (end)) {
+-		*(buf) = (c);
+-	}
+-	(buf) += 1;
++	buf = sputchar(buf, end, c);
+	...>
+}
+
+@pinc2 depends on patch@
+identifier func;
+expression buf, end, c;
+position p;
+@@
+
+func(...)
+{
+	<...
+-	if ((buf) < (end)) {
+-		*(buf) = (c);
+-	}
+-	(buf) = (buf) + 1;
++	buf = sputchar(buf, end, c);
+	...>
+}
+
+@script:python depends on report@
+p << rpostfix.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rpostfix.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+
+@script:python depends on report@
+p << rprefix.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rprefix.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on report@
+p << rinc1.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
+@script:python depends on org@
+p << rinc1.p;
+@@
+
+for p0 in p:
+	coccilib.report.print_report(p0, "WARNING opportunity for sputchar()")
+
Signature · 1 line
2.30.2