blobmsg_json: simplify add_separator and fix thread-safety
authorMatthias Schiffer <mschiffer@universe-factory.net>
Wed, 6 Apr 2016 15:40:22 +0000 (17:40 +0200)
committerJo-Philipp Wich <jo@mein.io>
Wed, 6 Apr 2016 15:43:47 +0000 (17:43 +0200)
The hard-coded length limits are replaced with strlen to make the code more
robust.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
blobmsg_json.c

index 2d1d80dd501c3935b50c1d72b33ad7af6026d761..571394840181e1453deb67aa5e9045c0f868f61a 100644 (file)
@@ -135,21 +135,17 @@ static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
 
 static void add_separator(struct strbuf *s)
 {
-       static char indent_chars[17] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
-       int indent;
-       char *start;
+       const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
+       int len;
 
        if (!s->indent)
                return;
 
-       indent = s->indent_level;
-       if (indent > 16)
-               indent = 16;
+       len = s->indent_level + 1;
+       if (len > strlen(indent_chars))
+               len = strlen(indent_chars);
 
-       start = &indent_chars[sizeof(indent_chars) - indent - 1];
-       *start = '\n';
-       blobmsg_puts(s, start, indent + 1);
-       *start = '\t';
+       blobmsg_puts(s, indent_chars, len);
 }