Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp838135ybi; Fri, 12 Jul 2019 05:29:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfSevWxpr9BU9f5dyqnajunovZfK87JtoMZZcs9PV4uX+ZbZbi5WmdLd8/XEsBC71Sf/P3 X-Received: by 2002:a17:902:820c:: with SMTP id x12mr11298401pln.216.1562934582678; Fri, 12 Jul 2019 05:29:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562934582; cv=none; d=google.com; s=arc-20160816; b=G2i+VI22H+GeeA1szv/Ider0IW86BTIkTK8SigOw4UJwIZbvepGndz9nkYOg0RV7fI N1SGkPwxQuYApuHzPiw20vjx3a6GrSPW91bVp2NLG+N7xdZS8ccEEXmP6z/bAamUmrBK iKnjWxrw+AHG8bGScZi41NIWYn5iewR+0XPcRaFK9w+UuUwxpzWPdVrn6nT/dC4q4XZA stYunTP5AXbZ06tk1CKX4ArRT0fD1yMXfT4pV+HxkuAGdCTyg9sHnbkumOvffx0d9dco CXscZc2ZFkbd8vDfrlHFyUoALusymi8kUmYlDktbsYyyDw2Zs3iRe3/Whw+hQkj9nhvj IPGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=o230cT0a1GEkCeSpjwQqOPIhKfQ+XfL/V+2tILHG6V0=; b=nN1unQ897b0fO3qJ4X8au2h3OD0Fnha/v/MPHelUEB8DXG7UMa7E/iPyOQry+ohlU2 yD/phK+UjO996PtVC+scrWlOD6S6FEpdPQxEEVYoVQJ4Tc7Be+ixzSv9qK2xQ2g1Redp a3/SXXSRxLtAq110jfOr3v510gWSr/gXRyy/SQdvkmUBiKG+23pGtg2DDdGhOAc8n5mO kbs5rXC7OE5ZPGXk6d1LrAIrFCcJbFUgCuWRuCYzC2lqgfTYOIfxnONHu4ELqv97zLpg 51JUCPSYocTpd2uMk0+67pKQz1FaqIz5B/+Wwcf3HM5JZ8+3UXOx8NCIsqhJYJVVczfY Ml7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Bf58cF4C; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f37si7816704pjg.61.2019.07.12.05.29.27; Fri, 12 Jul 2019 05:29:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Bf58cF4C; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728162AbfGLM14 (ORCPT + 99 others); Fri, 12 Jul 2019 08:27:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:41182 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728720AbfGLM1z (ORCPT ); Fri, 12 Jul 2019 08:27:55 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 18CD821019; Fri, 12 Jul 2019 12:27:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562934475; bh=29XoS77K21tgMNHk4T910ZD4vgXDxrf+LXa6VRmNcJU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bf58cF4C+isX2SguepMfmLrM8M7J7SAWA25J2ZzDkISH8+PvkaO3CmGt0QA+7M3DU oqSYnrzXnLzBGUdUAIRhH0IBAc+x5BaxWJaXOytiryDXVIueekNJlzhbyYY/rk1oEw sbNV6WIH5biS3QYz/8LeGaXiAXnEARKGqgzv/MGY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Quentin Monnet , Krzesimir Nowak , Andrii Nakryiko , Daniel Borkmann , Sasha Levin Subject: [PATCH 5.1 030/138] tools: bpftool: Fix JSON output when lookup fails Date: Fri, 12 Jul 2019 14:18:14 +0200 Message-Id: <20190712121629.852592026@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190712121628.731888964@linuxfoundation.org> References: <20190712121628.731888964@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 1884c066579a7a274dd981a4d9639ca63db66a23 ] In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions") one case of error reporting was special cased, so it could report a lookup error for a specific key when dumping the map element. What the code forgot to do is to wrap the key and value keys into a JSON object, so an example output of pretty JSON dump of a sockhash map (which does not support looking up its values) is: [ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" ], "value": { "error": "Operation not supported" }, "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" ], "value": { "error": "Operation not supported" } ] Note the key-value pairs inside the toplevel array. They should be wrapped inside a JSON object, otherwise it is an invalid JSON. This commit fixes this, so the output now is: [{ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00" ], "value": { "error": "Operation not supported" } },{ "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01" ], "value": { "error": "Operation not supported" } } ] Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions") Cc: Quentin Monnet Signed-off-by: Krzesimir Nowak Acked-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- tools/bpf/bpftool/map.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 994a7e0d16fb..14f581b562bd 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -713,12 +713,14 @@ static int dump_map_elem(int fd, void *key, void *value, return 0; if (json_output) { + jsonw_start_object(json_wtr); jsonw_name(json_wtr, "key"); print_hex_data_json(key, map_info->key_size); jsonw_name(json_wtr, "value"); jsonw_start_object(json_wtr); jsonw_string_field(json_wtr, "error", strerror(lookup_errno)); jsonw_end_object(json_wtr); + jsonw_end_object(json_wtr); } else { if (errno == ENOENT) print_entry_plain(map_info, key, NULL); -- 2.20.1