From patchwork Thu Sep 21 01:16:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Julien Fortin X-Patchwork-Id: 816629 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="EtcS15Rw"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xyJc56Rt2z9s4q for ; Thu, 21 Sep 2017 11:16:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751873AbdIUBQv (ORCPT ); Wed, 20 Sep 2017 21:16:51 -0400 Received: from mail-pf0-f180.google.com ([209.85.192.180]:52821 "EHLO mail-pf0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751636AbdIUBQt (ORCPT ); Wed, 20 Sep 2017 21:16:49 -0400 Received: by mail-pf0-f180.google.com with SMTP id p87so2407413pfj.9 for ; Wed, 20 Sep 2017 18:16:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5GIbe+p3j5EbQrGWa3zCz8eNF5f678WqaTBO6w4mCdk=; b=EtcS15RwfUSkihoBX+mJFDQincSeR2gawMG1VjOKd/QDHnBwNbvISVNDwS1Pjs8xih 4RKIILzxMTUM5f7FWxtq6MKiNCC4ck23bNd0PBBzLS67GjLUDiJ5eZIh8VwxPt6JaoeA fRcs+fQ5q2EEh0rA/JC2DPhLZ9YJC2iTHPlDE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=5GIbe+p3j5EbQrGWa3zCz8eNF5f678WqaTBO6w4mCdk=; b=LlB1JW+Yn9iWLi03TU7Fr4fj+wwUSK+9gzwIomkQdtZJGG8ymnikuLEno499kdaLI1 gCmBlNsWQNpYJHrqfmpBvbamYE/1KQW1W07cXa+RLBUJDqpWDlY9LhNHRsqmRajK3Kxa 3ikW/bTN0rT+K/nsKyC/Xkncu6h90YyEMzoeTIRxtIuz7dEOIc7QoRpgNzXLijn4inl4 xdkdOYhDSAo/EhP3D3oC6jPa8T8ftmyAS7pAuInO5+bhVG1L+aS4EsEGgCLcXyVOMG+J Fk455yhAa9xX1QtCOWb+Yn8CtDjig6Czrz+j6YeZ7w9gPz8V5IkeG2tbupwx55YFIlj0 EeGA== X-Gm-Message-State: AHPjjUj+T6+68CLrIXRQOdNXzEto+4Rio88mqILU/fOe5uvn8iaRI+5t EsvRlwaEzJCg9/+/tsgKMJIXM2c= X-Google-Smtp-Source: AOwi7QDkxMoTm7J/K/qMRKSEDhkvYOHBHRbWJP2zEuNZJr80v97DREUxycsBtuhIkBwcy5aGS8wkeg== X-Received: by 10.98.18.215 with SMTP id 84mr4050472pfs.42.1505956608979; Wed, 20 Sep 2017 18:16:48 -0700 (PDT) Received: from JFK.dana.cumulusnetworks.com (fw.cumulusnetworks.com. [216.129.126.126]) by smtp.googlemail.com with ESMTPSA id 5sm202837pfr.151.2017.09.20.18.16.47 (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 20 Sep 2017 18:16:48 -0700 (PDT) From: Julien Fortin X-Google-Original-From: Julien Fortin To: netdev@vger.kernel.org Cc: roopa@cumulusnetworks.com, nikolay@cumulusnetworks.com, dsa@cumulusnetworks.com, Julien Fortin Subject: [PATCH iproute2 v2] ip: ip_print: if no json obj is initialized default fp is stdout Date: Wed, 20 Sep 2017 18:16:45 -0700 Message-Id: <20170921011645.45555-1-julien@cumulusnetworks.com> X-Mailer: git-send-email 2.14.1 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Julien Fortin The ip monitor didn't call `new_json_obj` (even for in non json context), so the static FILE* _fp variable wasn't initialized, thus raising a SIGSEGV in ipaddress.c. This patch should fix this issue for good, new paths won't have to call `new_json_obj`. $ ip -t mon label link fmt=fmt@entry=0x45460d “%d: “, value=) at ip_print.c:132 at libnetlink.c:761 Traceback can be seen when running the following command in a new terminal. $ ip link add dev br0 type bridge Fixes: 6377572f ("ip: ip_print: add new API to print JSON or regular format output") Reported-by: David Ahern Reviewed-by: David Ahern Signed-off-by: Julien Fortin --- ip/ip_print.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ip/ip_print.c b/ip/ip_print.c index 4cd6a0bc..7eb4c6da 100644 --- a/ip/ip_print.c +++ b/ip/ip_print.c @@ -23,6 +23,11 @@ static FILE *_fp; #define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw) #define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY)) +static inline FILE *_get_fp(void) +{ + return _fp ? _fp : stdout; +}; + void new_json_obj(int json, FILE *fp) { if (json) { @@ -91,7 +96,7 @@ void open_json_array(enum output_type type, const char *str) jsonw_name(_jw, str); jsonw_start_array(_jw); } else if (_IS_FP_CONTEXT(type)) { - fprintf(_fp, "%s", str); + fprintf(_get_fp(), "%s", str); } } @@ -105,7 +110,7 @@ void close_json_array(enum output_type type, const char *str) jsonw_end_array(_jw); jsonw_pretty(_jw, true); } else if (_IS_FP_CONTEXT(type)) { - fprintf(_fp, "%s", str); + fprintf(_get_fp(), "%s", str); } } @@ -126,7 +131,7 @@ void close_json_array(enum output_type type, const char *str) else \ jsonw_##type_name##_field(_jw, key, value); \ } else if (_IS_FP_CONTEXT(t)) { \ - color_fprintf(_fp, color, fmt, value); \ + color_fprintf(_get_fp(), color, fmt, value); \ } \ } _PRINT_FUNC(int, int); @@ -149,7 +154,7 @@ void print_color_string(enum output_type type, else jsonw_string_field(_jw, key, value); } else if (_IS_FP_CONTEXT(type)) { - color_fprintf(_fp, color, fmt, value); + color_fprintf(_get_fp(), color, fmt, value); } } @@ -170,7 +175,7 @@ void print_color_bool(enum output_type type, else jsonw_bool(_jw, value); } else if (_IS_FP_CONTEXT(type)) { - color_fprintf(_fp, color, fmt, value ? "true" : "false"); + color_fprintf(_get_fp(), color, fmt, value ? "true" : "false"); } } @@ -189,7 +194,7 @@ void print_color_0xhex(enum output_type type, snprintf(b1, sizeof(b1), "%#x", hex); print_string(PRINT_JSON, key, NULL, b1); } else if (_IS_FP_CONTEXT(type)) { - color_fprintf(_fp, color, fmt, hex); + color_fprintf(_get_fp(), color, fmt, hex); } } @@ -208,7 +213,7 @@ void print_color_hex(enum output_type type, else jsonw_string(_jw, b1); } else if (_IS_FP_CONTEXT(type)) { - color_fprintf(_fp, color, fmt, hex); + color_fprintf(_get_fp(), color, fmt, hex); } } @@ -228,6 +233,6 @@ void print_color_null(enum output_type type, else jsonw_null(_jw); } else if (_IS_FP_CONTEXT(type)) { - color_fprintf(_fp, color, fmt, value); + color_fprintf(_get_fp(), color, fmt, value); } }