[PATCH v1] string: add sanity check to the strcmp() and strncmp()

Oleksij Rempel o.rempel at pengutronix.de
Thu Aug 13 07:39:38 EDT 2020


A relatively big portion of barebox init sequence is running without
configured exception vector. As result we may not detect some NULL
pointer dereferences (as on iMX6) or just silently freeze (as on stm32).

So, add sanity check to detect this kind of issues as early as possible.

Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
---
 lib/string.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/string.c b/lib/string.c
index 717b59aa50..7548fd3581 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -208,6 +208,8 @@ int strcmp(const char * cs,const char * ct)
 {
 	register signed char __res;
 
+	BUG_ON(!cs || !ct);
+
 	while (1) {
 		if ((__res = *cs - *ct++) != 0 || !*cs++)
 			break;
@@ -229,6 +231,8 @@ int strncmp(const char * cs, const char * ct, size_t count)
 {
 	register signed char __res = 0;
 
+	BUG_ON(!cs || !ct);
+
 	while (count) {
 		if ((__res = *cs - *ct++) != 0 || !*cs++)
 			break;
-- 
2.28.0




More information about the barebox mailing list