Skip to contentSkip to conversation

lib/string: fix coding style issues

RFC PATCH In review Samyak 2 participants 2 messages Feb 14

Subsystems (2)
lore
Share & tools
lore

Samyak

author
#0 lore raw

RFC PATCH 1/1 lib/string: fix coding style issues

- Add spaces around binary operators
- remove spaces after casts
- Add blank line after variable declarations
- Fix constant on left side of a conditional expression (0 < count -> count > 0)
- Remove un-needed braces around single statements

---
This is my first linux kernel patch. I have compiled and tested the changes on QEMU. I would appreciate any feedback. Thanks.

 lib/string.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
lib/string.c +16 -11
index b632c71df..8f5582d9a 100644
@@ -127,12 +127,13 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
 	 */
 	if ((long)src & (sizeof(long) - 1)) {
 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
+
 		if (limit < max)
 			max = limit;
 	}
 #else
 	/* If src or dest is unaligned, don't do word-at-a-time. */
-	if (((long) dest | (long) src) & (sizeof(long) - 1))
+	if (((long)dest | (long)src) & (sizeof(long) - 1))
 		max = 0;
 #endif
 #endif
@@ -150,23 +151,24 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
 		unsigned long c, data;
 
 #ifdef CONFIG_DCACHE_WORD_ACCESS
-		c = load_unaligned_zeropad(src+res);
+		c = load_unaligned_zeropad(src + res);
 #else
-		c = read_word_at_a_time(src+res);
+		c = read_word_at_a_time(src + res);
 #endif
 		if (has_zero(c, &data, &constants)) {
 			data = prep_zero_mask(c, data, &constants);
 			data = create_zero_mask(data);
-			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
+			*(unsigned long *)(dest + res) = c &
+							 zero_bytemask(data);
 			return res + find_zero(data);
 		}
 		count -= sizeof(unsigned long);
 		if (unlikely(!count)) {
 			c &= ALLBUTLAST_BYTE_MASK;
-			*(unsigned long *)(dest+res) = c;
+			*(unsigned long *)(dest + res) = c;
 			return -E2BIG;
 		}
-		*(unsigned long *)(dest+res) = c;
+		*(unsigned long *)(dest + res) = c;
 		res += sizeof(unsigned long);
 		max -= sizeof(unsigned long);
 	}
@@ -261,7 +263,7 @@ size_t strlcat(char *dest, const char *src, size_t count)
 	dest += dsize;
 	count -= dsize;
 	if (len >= count)
-		len = count-1;
+		len = count - 1;
 	__builtin_memcpy(dest, src, len);
 	dest[len] = 0;
 	return res;
@@ -380,6 +382,7 @@ char *strnchrnul(const char *s, size_t count, int c)
 char *strrchr(const char *s, int c)
 {
 	const char *last = NULL;
+
 	do {
 		if (*s == (char)c)
 			last = s;
@@ -679,6 +682,7 @@ __visible int memcmp(const void *cs, const void *ct, size_t count)
 	if (count >= sizeof(unsigned long)) {
 		const unsigned long *u1 = cs;
 		const unsigned long *u2 = ct;
+
 		do {
 			if (get_unaligned(u1) != get_unaligned(u2))
 				break;
@@ -690,9 +694,10 @@ __visible int memcmp(const void *cs, const void *ct, size_t count)
 		ct = u2;
 	}
 #endif
-	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+	for (su1 = cs, su2 = ct; count > 0; ++su1, ++su2, count--) {
 		if ((res = *su1 - *su2) != 0)
 			break;
+	}
 	return res;
 }
 EXPORT_SYMBOL(memcmp);
@@ -737,7 +742,7 @@ void *memscan(void *addr, int c, size_t size)
 		p++;
 		size--;
 	}
-  	return (void *)p;
+	return (void *)p;
 }
 EXPORT_SYMBOL(memscan);
 #endif
@@ -805,10 +810,10 @@ EXPORT_SYMBOL(strnstr);
 void *memchr(const void *s, int c, size_t n)
 {
 	const unsigned char *p = s;
+
 	while (n-- != 0) {
-        	if ((unsigned char)c == *p++) {
+		if ((unsigned char)c == *p++)
 			return (void *)(p - 1);
-		}
 	}
 	return NULL;
 }
Signature · 1 line
2.53.0
#1 lore raw
Quoted 7 lines · Samyak

- Add spaces around binary operators
- remove spaces after casts
- Add blank line after variable declarations
- Fix constant on left side of a conditional expression (0 < count -> count > 0)
- Remove un-needed braces around single statements

Quoted 6 lines

---
This is my first linux kernel patch. I have compiled and tested the changes on QEMU. I would appreciate any feedback. Thanks.

 lib/string.c | 27 ++++++++++++++++-----------

If you want to get involved in kernel work, please do coding style changes in drivers/staging/ which is there to learn on how to do this. Only after doing this a lot, and getting experience, should you venture out of drivers/staging/

Also, many subsystems do not want coding style fixes for obvious reasons (i.e. they change over time and keeping the whole codebase up to date like this would be constant churn.)

Hint, this patch would need to be broken up anyway, you should only do "one logical thing" per patch. And "fix all coding style issues" is not one thing :)

thanks,

greg k-h