Skip to contentSkip to conversation

Use standard C string APIs in FDT helper

PATCH Superseded Abner Chang 4 participants 11 messages Jul 23, 2020 – Jul 25, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This keeps the consistency with lidfdt implementation and utilizing libfdt_evn.h for the different build environment even fdlhelp.c is not part of standard fdt library.
- In libfdt_evn.h, a macro overrides strncmp with sbi_strncmp.
- For edk2 firmware solution, similar macro is used to repalce strncmp with EDK2 string API.
- Use strncmp instead of strcmp, which is much safer.

Abner Chang (3):
Use standard C string APIs in FDT helper
Use standard C string APIs in FDT helper
Use standard C string APIs in FDT helper

 include/sbi/sbi_string.h      | 2 +-
 lib/sbi/sbi_string.c          | 4 ++--
 lib/utils/fdt/fdt_helper.c    | 2 +-
 lib/utils/libfdt/libfdt_env.h | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h +1 -1
index 338075f..5e7304f 100644
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c +2 -2
index 38b700b..c29120a 100644
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
 	/* search first diff or end of string */
-	for (; *a == *b && *a != '\0'; a++, b++)
+	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
 		;
 
 	return *a - *b;
Signature · 1 line
2.25.0
#7 lore raw

PATCH 2/3 Use standard C string APIs in FDT helper

In reply to Abner Chang

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add a macro to replace strncmp with sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/libfdt/libfdt_env.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/libfdt/libfdt_env.h +1 -1
index 7fcfe01..a9be6a8 100644
@@ -31,9 +31,9 @@
 #define strchr		sbi_strchr
 #define strrchr		sbi_strrchr
 #define strcpy		sbi_strcpy
-#define strcmp		sbi_strcmp
 #define strlen		sbi_strlen
 #define strnlen		sbi_strnlen
+#define strncmp		sbi_strncmp
 
 typedef uint16_t FDT_BITWISE fdt16_t;
 typedef uint32_t FDT_BITWISE fdt32_t;
Signature · 1 line
2.25.0
#9 lore raw

PATCH 3/3 Use standard C string APIs in FDT helper

In reply to Abner Chang

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/fdt/fdt_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/fdt/fdt_helper.c +1 -1
index 78077f7..aec73a0 100644
@@ -123,7 +123,7 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32 *hartid)
 	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
 	if (!prop || !len)
 		return SBI_EINVAL;
-	if (sbi_strcmp(prop, "cpu"))
+	if (strncmp (prop, "cpu", strlen ("cpu")))
 		return SBI_EINVAL;
 
 	val = fdt_getprop(fdt, cpu_offset, "reg", &len);
Signature · 1 line
2.25.0
#2 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

In reply to Abner Chang
Quoted 54 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h index renamed from include/sbi/sbi_string.h +0 -0

338075f..5e7304f 100644

include/sbi/sbi_string.h +1 -1
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c index 38b700b..c29120a renamed from lib/sbi/sbi_string.c +0 -0

100644

lib/sbi/sbi_string.c +2 -3
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
 	/* search first diff or end of string */
-	for (; *a == *b && *a != '\0'; a++, b++)
+	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
 		;
 
 	return *a - *b;
--
Quoted 1 line

2.25.0

I think we should keep both sbi_strcmp() and sbi_strncmp().

Otherwise looks good to me.

Regards,
Anup

#8 lore raw

PATCH 2/3 Use standard C string APIs in FDT helper

In reply to Abner Chang
Quoted 38 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 2/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add a macro to replace strncmp with sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/libfdt/libfdt_env.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/libfdt/libfdt_env.h index renamed from lib/utils/libfdt/libfdt_env.h +0 -0

7fcfe01..a9be6a8 100644

lib/utils/libfdt/libfdt_env.h +1 -2
@@ -31,9 +31,9 @@
 #define strchr		sbi_strchr
 #define strrchr		sbi_strrchr
 #define strcpy		sbi_strcpy
-#define strcmp		sbi_strcmp
 #define strlen		sbi_strlen
 #define strnlen		sbi_strnlen
+#define strncmp		sbi_strncmp
 
 typedef uint16_t FDT_BITWISE fdt16_t;
 typedef uint32_t FDT_BITWISE fdt32_t;
--
Quoted 1 line

2.25.0

Looks good to me.

Regards,
Anup

#10 lore raw

PATCH 3/3 Use standard C string APIs in FDT helper

In reply to Abner Chang
Quoted 34 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 3/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/fdt/fdt_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/fdt/fdt_helper.c index renamed from lib/utils/fdt/fdt_helper.c +0 -0

78077f7..aec73a0 100644

lib/utils/fdt/fdt_helper.c +1 -1
@@ -123,7 +123,7 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32
 *hartid)
 	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
 	if (!prop || !len)
 		return SBI_EINVAL;
-	if (sbi_strcmp(prop, "cpu"))
+	if (strncmp (prop, "cpu", strlen ("cpu")))
 		return SBI_EINVAL;
 
Quoted 3 lines

val = fdt_getprop(fdt, cpu_offset, "reg", &len);
--
2.25.0

Looks good to me.

Regards,
Anup

#3 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

In reply to Anup Patel
Quoted 65 lines

-----Original Message-----
From: Anup Patel [mailto:Anup.Patel at wdc.com]
Sent: Friday, July 24, 2020 12:37 PM
To: Chang, Abner (HPS SW/FW Technologist) <[email protected]>; opensbi at lists.infradead.org
Cc: Atish Patra <[email protected]>; Schaefer, Daniel (DualStudy) <[email protected]>
Subject: RE: [PATCH 1/3] Use standard C string APIs in FDT helper

Quoted 5 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup

Patel

Quoted 46 lines

<[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h index renamed from include/sbi/sbi_string.h +0 -0

338075f..5e7304f 100644

include/sbi/sbi_string.h +1 -1
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c index renamed from lib/sbi/sbi_string.c +0 -0

38b700b..c29120a
100644

lib/sbi/sbi_string.c +2 -2
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
 	/* search first diff or end of string */
-	for (; *a == *b && *a != '\0'; a++, b++)
+	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
 		;
 
Quoted 5 lines
Quoted 3 lines
lib/sbi/sbi_string.c +0 -1
 	return *a - *b;
--

2.25.0

I think we should keep both sbi_strcmp() and sbi_strncmp().

Hi Anup, do you think we should prevent people to use sbi_strcmp()?

Thanks
Abner

Quoted 7 lines

Otherwise looks good to me.

Regards,
Anup

#4 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

Quoted 66 lines · Abner (HPS SW/FW Technologist)
Quoted 66 lines

-----Original Message-----
From: Anup Patel [mailto:Anup.Patel at wdc.com]
Sent: Friday, July 24, 2020 12:37 PM
To: Chang, Abner (HPS SW/FW Technologist) <[email protected]>; opensbi at lists.infradead.org
Cc: Atish Patra <[email protected]>; Schaefer, Daniel (DualStudy) <[email protected]>
Subject: RE: [PATCH 1/3] Use standard C string APIs in FDT helper

Quoted 5 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup

Patel

Quoted 46 lines

<[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h index renamed from include/sbi/sbi_string.h +0 -0

338075f..5e7304f 100644

include/sbi/sbi_string.h +1 -1
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c index renamed from lib/sbi/sbi_string.c +0 -0

38b700b..c29120a
100644

lib/sbi/sbi_string.c +2 -2
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
    /* search first diff or end of string */
-   for (; *a == *b && *a != '\0'; a++, b++)
+   for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
            ;
 
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
lib/sbi/sbi_string.c +0 -1
    return *a - *b;
--

2.25.0

I think we should keep both sbi_strcmp() and sbi_strncmp().

Hi Anup, do you think we should prevent people to use sbi_strcmp()?

The only reason we may want to keep the sbi_strcmp() just for completeness sake. In case, some of the libfdt functions use it in some revision.

If we decide to keep it, we should put a big comment on top sbi_strcmp clearly mentioning that
it is not recommended to use sbi_strcmp at all.

Quoted 15 lines

Thanks
Abner

Quoted 7 lines

Otherwise looks good to me.

Regards,
Anup

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 2 lines
Regards,
Atish
#5 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

Quoted 101 lines · Atish Patra
Quoted 66 lines · Abner (HPS SW/FW Technologist)
Quoted 66 lines

-----Original Message-----
From: Anup Patel [mailto:Anup.Patel at wdc.com]
Sent: Friday, July 24, 2020 12:37 PM
To: Chang, Abner (HPS SW/FW Technologist) <[email protected]>; opensbi at lists.infradead.org
Cc: Atish Patra <[email protected]>; Schaefer, Daniel (DualStudy) <[email protected]>
Subject: RE: [PATCH 1/3] Use standard C string APIs in FDT helper

Quoted 5 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup

Patel

Quoted 46 lines

<[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h index renamed from include/sbi/sbi_string.h +0 -0

338075f..5e7304f 100644

include/sbi/sbi_string.h +1 -1
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c index renamed from lib/sbi/sbi_string.c +0 -0

38b700b..c29120a
100644

lib/sbi/sbi_string.c +2 -2
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
    /* search first diff or end of string */
-   for (; *a == *b && *a != '\0'; a++, b++)
+   for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
            ;
 
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
lib/sbi/sbi_string.c +0 -1
    return *a - *b;
--

2.25.0

I think we should keep both sbi_strcmp() and sbi_strncmp().

Hi Anup, do you think we should prevent people to use sbi_strcmp()?

The only reason we may want to keep the sbi_strcmp() just for completeness sake. In case, some of the libfdt functions use it in some revision.

If we decide to keep it, we should put a big comment on top sbi_strcmp clearly mentioning that
it is not recommended to use sbi_strcmp at all.

Quoted 7 lines

Thanks
Abner

Quoted 5 lines

Otherwise looks good to me.

Quoted 8 lines
Quoted 2 lines

Regards,
Anup

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Quoted 3 lines

--
Regards,
Atish

For the entire series:

Signature · 2 lines
Regards,
Atish
#6 lore raw

PATCH 1/3 Use standard C string APIs in FDT helper

In reply to Atish Patra
Quoted 84 lines

-----Original Message-----
From: Atish Patra [mailto:atishp at atishpatra.org]
Sent: Saturday, July 25, 2020 1:21 PM
To: Chang, Abner (HPS SW/FW Technologist) <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Atish Patra <[email protected]>; Schaefer, Daniel (DualStudy) <[email protected]>
Subject: Re: [PATCH 1/3] Use standard C string APIs in FDT helper

Quoted 71 lines · Abner (HPS SW/FW Technologist)
Quoted 71 lines

-----Original Message-----
From: Anup Patel [mailto:Anup.Patel at wdc.com]
Sent: Friday, July 24, 2020 12:37 PM
To: Chang, Abner (HPS SW/FW Technologist) <[email protected]>; opensbi at lists.infradead.org
Cc: Atish Patra <[email protected]>; Schaefer, Daniel (DualStudy) <[email protected]>
Subject: RE: [PATCH 1/3] Use standard C string APIs in FDT helper

Quoted 5 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 23 July 2020 07:24
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup

Patel

Quoted 48 lines

<[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h | 2 +-
 lib/sbi/sbi_string.c     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/sbi/sbi_string.h +1 -1
index 338075f..5e7304f 100644
@@ -12,7 +12,7 @@
 
 #include <sbi/sbi_types.h>
 
-int sbi_strcmp(const char *a, const char *b);
+int sbi_strncmp(const char *a, const char *b, size_t count);
 
 size_t sbi_strlen(const char *str);
 
lib/sbi/sbi_string.c index renamed from lib/sbi/sbi_string.c +0 -0

38b700b..c29120a
100644

lib/sbi/sbi_string.c +2 -3
@@ -14,10 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
-int sbi_strcmp(const char *a, const char *b)
+int sbi_strncmp(const char *a, const char *b, size_t count)
 {
    /* search first diff or end of string */
-   for (; *a == *b && *a != '\0'; a++, b++)
+   for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
            ;
 
    return *a - *b;
--
Quoted 1 line

2.25.0

I think we should keep both sbi_strcmp() and sbi_strncmp().

Quoted 8 lines
Quoted 2 lines

Hi Anup, do you think we should prevent people to use sbi_strcmp()?

The only reason we may want to keep the sbi_strcmp() just for completeness sake.
In case, some of the libfdt functions use it in some revision.

If we decide to keep it, we should put a big comment on top sbi_strcmp clearly mentioning that it is not recommended to use sbi_strcmp at all.

Understand. I will keep it and resend the patch.
Thanks

Quoted 22 lines
Quoted 8 lines

Thanks
Abner

Quoted 5 lines

Otherwise looks good to me.

Quoted 8 lines
Quoted 2 lines

Regards,
Anup

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
Regards,
Atish