From patchwork Wed Apr 6 15:40:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 607077 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (caladan.dune.hu [78.24.191.180]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qg90y3ntRz9sCy for ; Thu, 7 Apr 2016 01:40:49 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id B0B8EB803ED; Wed, 6 Apr 2016 17:40:33 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on arrakis.dune.hu X-Spam-Level: ** X-Spam-Status: No, score=2.4 required=5.0 tests=RDNS_NONE autolearn=no autolearn_force=no version=3.4.1 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP; Wed, 6 Apr 2016 17:40:33 +0200 (CEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 4AA7CB80387 for ; Wed, 6 Apr 2016 17:40:32 +0200 (CEST) X-policyd-weight: using cached result; rate: -6.1 Received: from chaos.universe-factory.net (unknown [37.72.148.22]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Wed, 6 Apr 2016 17:40:32 +0200 (CEST) Received: from avalon.neoraider.dn42 (unknown [IPv6:fd1b:c28a:2fd6::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by chaos.universe-factory.net (Postfix) with ESMTPSA id 924941835DF for ; Wed, 6 Apr 2016 17:40:31 +0200 (CEST) From: Matthias Schiffer To: openwrt-devel@lists.openwrt.org Date: Wed, 6 Apr 2016 17:40:22 +0200 Message-Id: X-Mailer: git-send-email 2.8.0 Subject: [OpenWrt-Devel] [PATCH libubox] blobmsg_json: simplify add_separator and fix thread-safety X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" The hard-coded length limits are replaced with strlen to make the code more robust. Signed-off-by: Matthias Schiffer --- blobmsg_json.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/blobmsg_json.c b/blobmsg_json.c index 2d1d80d..5713948 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -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); }