From patchwork Wed Nov 12 13:59:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 419625 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D8A441400A0 for ; Wed, 10 Dec 2014 23:21:01 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 2098228C1F0; Wed, 10 Dec 2014 13:16:40 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, DATE_IN_PAST_96_XX, FREEMAIL_FROM,T_DKIM_INVALID autolearn=no version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 8F94128C1DF for ; Wed, 10 Dec 2014 13:16:23 +0100 (CET) X-policyd-weight: using cached result; rate:hard: -8.5 Received: from mail-pa0-f44.google.com (mail-pa0-f44.google.com [209.85.220.44]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Wed, 10 Dec 2014 13:16:16 +0100 (CET) Received: by mail-pa0-f44.google.com with SMTP id et14so2713268pad.17 for ; Wed, 10 Dec 2014 04:17:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Ce8/UxjVrCXYwUASmiB4B+1ZvZ7SBQcJO4qDrZOqISM=; b=ZG/8V3SAuixUWxF5hmymuvaoVE9FC0P6YJM1OX3HJR8saup3cQDK9wQorXBzXdcnOt P8ZphWJDsqjJwx5xyowGxsj5ddeGZW02KRU1o3ML0vEHVNmSuM+VoaWH+4w7W9C1KxIy d04u5KgcXLF9Acag34muyAq52cqi8ac26rZCtL6Egy5Vet/iZsJZw9zmHyvqWOikDSE2 jEOIpcA09FitZ7ywm/O3eXRHIGN+ZRomHFo4/PKWXfxXLkMIiLGeSndrEWcLNNGUXWgs EtsOtAMVZ1HeXvgDDjB4bnsK1t8sYwvCzsCDwOhnEie8OkPpEGriICHjKmrdYw8zTv9H pWYg== X-Received: by 10.68.227.104 with SMTP id rz8mr6836001pbc.4.1418213879291; Wed, 10 Dec 2014 04:17:59 -0800 (PST) Received: from debian.lan ([103.29.140.56]) by mx.google.com with ESMTPSA id hc10sm4063709pbd.78.2014.12.10.04.17.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Dec 2014 04:17:58 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Wed, 12 Nov 2014 21:59:20 +0800 Message-Id: <1415800763-14311-8-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1415800763-14311-1-git-send-email-yszhou4tech@gmail.com> References: <1415800763-14311-1-git-send-email-yszhou4tech@gmail.com> Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH v2 07/10] jshn: add error handling and fix memory leak in jshn_format(). X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 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" Though currently jshn is more a one-shot data transformation tool and won't leak much memory in its lifetime, people may use it as example code, so do it right. Signed-off-by: Yousong Zhou --- jshn.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/jshn.c b/jshn.c index 431e1b3..69cb06f 100644 --- a/jshn.c +++ b/jshn.c @@ -258,18 +258,31 @@ static int jshn_format(bool no_newline, bool indent) { json_object *obj; const char *output; + char *blobmsg_output = NULL; + int ret = -1; + + if (!(obj = json_object_new_object())) + return -1; - obj = json_object_new_object(); jshn_add_objects(obj, "J_V", false); - output = json_object_to_json_string(obj); + if (!(output = json_object_to_json_string(obj))) + goto out; + if (indent) { blob_buf_init(&b, 0); - blobmsg_add_json_from_string(&b, output); - output = blobmsg_format_json_indent(b.head, 1, 0); + if (!blobmsg_add_json_from_string(&b, output)) + goto out; + if (!(blobmsg_output = blobmsg_format_json_indent(b.head, 1, 0))) + goto out; + output = blobmsg_output; } fprintf(stdout, "%s%s", output, no_newline ? "" : "\n"); + free(blobmsg_output); + ret = 0; + +out: json_object_put(obj); - return 0; + return ret; } static int usage(const char *progname)