diff mbox series

[2/2] blobmsg: Don't do at run-time what can be done at compile-time

Message ID 20230414183706.1531155-1-philipp@redfish-solutions.com
State Accepted
Delegated to: Alexander Couzens
Headers show
Series [1/2] jshn.sh: Add pretty-printing to json_dump | expand

Commit Message

Philip Prindeville April 14, 2023, 6:37 p.m. UTC
From: Philip Prindeville <philipp@redfish-solutions.com>

Repeatedly calling a run-time function like strlen() on an
invariant value is inefficient, especially if that value can be
computed once (at initialization) or better yet, computed at
compile-time.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
---
 blobmsg_json.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/blobmsg_json.c b/blobmsg_json.c
index dce81e991ef7b83ac4906bca6f875369d3057f36..ec8b482c30c96a355aca58651632bc509a16bedf 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -151,15 +151,15 @@  static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
 
 static void add_separator(struct strbuf *s)
 {
-	const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
+	const char indent_chars[] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
 	size_t len;
 
 	if (!s->indent)
 		return;
 
 	len = s->indent_level + 1;
-	if (len > strlen(indent_chars))
-		len = strlen(indent_chars);
+	if (len > sizeof(indent_chars) - 1)
+		len = sizeof(indent_chars) - 1;
 
 	blobmsg_puts(s, indent_chars, len);
 }