From patchwork Fri Apr 20 17:15:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 902066 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@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; dmarc=none (p=none dis=none) header.from=networkplumber.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=networkplumber-org.20150623.gappssmtp.com header.i=@networkplumber-org.20150623.gappssmtp.com header.b="QGX+lhF9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40SMtt3H5mz9s1t for ; Sat, 21 Apr 2018 03:15:34 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753270AbeDTRPb (ORCPT ); Fri, 20 Apr 2018 13:15:31 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:37218 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752618AbeDTRPZ (ORCPT ); Fri, 20 Apr 2018 13:15:25 -0400 Received: by mail-pl0-f65.google.com with SMTP id f7-v6so5613916plr.4 for ; Fri, 20 Apr 2018 10:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=g2W93TznZJJ+10fAas7IQwDVUDLM6sqr0Sf/cS03RkA=; b=QGX+lhF9VWrSFTSTVhizQeJ4cnX6HGDmnnog1LM2WCICWTtTDfGaytCHG5cUo5pjoc CwsoqmPjgxekWxX1ujjwpweiQk+AHzmLlC2Hzl00YH7c1dMjjQsrXOtN7SO+8jOqsOC+ fbZJtCQQuapXBIvzguB03cpok8aFWxLbHTZnYqbBwcu78+hZlAfAhC4cuWioatiJT7Hr uoqHDhkBfI6cYf26zAzV0NQi/eN4R1L/VZDsi2fbdKuaarMoB/kP4Sre9ly49UKw2MwO 81fUBTcns/fGResn2L8+uw4Z4Gc6nVN1SwcfMqyAjhmrDoHTH5DvfLA/QeHgPFLXJzpn VelQ== 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:in-reply-to :references; bh=g2W93TznZJJ+10fAas7IQwDVUDLM6sqr0Sf/cS03RkA=; b=ZbfzaYlIFnBI+OD+j7O3NPO9Rx6/R7EX757Tnpg/Livox/CcNabRy4irprrfUpkmko nXhM3T6S94lXWyhxScOkXDvheZu0DR4n/ZzeCTcBtJCTgOCMEA8FA2TtBEHnixrn/8qo tzso7zw4fmrWHz45PGAZfnDz0HfVnkwSHTfFqwVkE5iRl2V/MUgqPpgwyJ5rvamUCIr0 HDLuuFLjwkG2qNWLHIO9HJ94h5a4phsm0jfy/co0vmjlM2fMW88YFrDDU/1sKbte69cp lvmCI6v2fKGmr9VwusXzd9ODaRiDiWJqsBWdc/5izo8i8Oxlz3U0LGwpbTophNTPM6j6 d1ig== X-Gm-Message-State: ALQs6tDsMcT/i6yubAfnOJrI709EoUyAK8KpJVVv3OM76qzrzhvgRRC/ XrNGW6ZlPVynug6CsnIx/PT/+A== X-Google-Smtp-Source: AIpwx4/i+AUYijBAXgitB5zqUzH+OtMCLLbJibuOhjlRZiRZ3Sp3s+5bwoTABEhWzy4yUv8/UzfXpQ== X-Received: by 2002:a17:902:7489:: with SMTP id h9-v6mr10873281pll.278.1524244524729; Fri, 20 Apr 2018 10:15:24 -0700 (PDT) Received: from xeon-e3.lan (204-195-71-95.wavecable.com. [204.195.71.95]) by smtp.gmail.com with ESMTPSA id m4sm16755482pfk.151.2018.04.20.10.15.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Apr 2018 10:15:23 -0700 (PDT) From: Stephen Hemminger To: alin.nastac@gmail.com Cc: netdev@vger.kernel.org, Stephen Hemminger Subject: [RFC iproute 2/5] json: make json print_uint take unsigned int Date: Fri, 20 Apr 2018 10:15:16 -0700 Message-Id: <20180420171519.8028-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180420171519.8028-1-stephen@networkplumber.org> References: <20180420171519.8028-1-stephen@networkplumber.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org It is a source of possible bugs that json print handler print_uint was doing implict conversion to 64 bit than printing with the format specifier which often had only a unsigned format value. Instead introduce wider range of print stubs for unsigned integer types. No color versions of these necessary. Signed-off-by: Stephen Hemminger --- include/json_print.h | 16 +++++++++++++--- lib/json_print.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index 2ca7830adbd6..3d400ecf8f50 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -59,12 +59,22 @@ _PRINT_FUNC(int, int); _PRINT_FUNC(bool, bool); _PRINT_FUNC(null, const char*); _PRINT_FUNC(string, const char*); -_PRINT_FUNC(uint, uint64_t); -_PRINT_FUNC(hu, unsigned short); _PRINT_FUNC(hex, unsigned int); _PRINT_FUNC(0xhex, unsigned int); -_PRINT_FUNC(lluint, unsigned long long int); _PRINT_FUNC(float, double); #undef _PRINT_FUNC +#define _PRINT_FUNC(type_name, type) \ + void print_##type_name(enum output_type t, \ + const char *key, \ + const char *fmt, \ + type value); + +_PRINT_FUNC(hu, unsigned short); +_PRINT_FUNC(uint, unsigned int); +_PRINT_FUNC(luint, unsigned long int); +_PRINT_FUNC(uint64, uint64_t); +_PRINT_FUNC(lluint, unsigned long long int); +#undef _PRINT_FUNC + #endif /* _JSON_PRINT_H_ */ diff --git a/lib/json_print.c b/lib/json_print.c index bda7293350c3..696d8c01d3e6 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -116,12 +116,39 @@ void close_json_array(enum output_type type, const char *str) } \ } _PRINT_FUNC(int, int); -_PRINT_FUNC(hu, unsigned short); -_PRINT_FUNC(uint, uint64_t); -_PRINT_FUNC(lluint, unsigned long long int); _PRINT_FUNC(float, double); #undef _PRINT_FUNC +static void json_print_uint64(const char *key, uint64_t val) +{ + if (key) + jsonw_uint_field(_jw, key, val); + else + jsonw_uint(_jw, val); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#define PRINT_FUNC(type_name, type) \ + void print_##type_name(enum output_type t, \ + const char *key, \ + const char *fmt, \ + type value) \ + { \ + if (_IS_JSON_CONTEXT(t)) \ + json_print_uint64(key, value); \ + else if (_IS_FP_CONTEXT(t)) \ + printf(fmt, value); \ + } + +PRINT_FUNC(hu, unsigned short); +PRINT_FUNC(uint, unsigned int); +PRINT_FUNC(lluint, unsigned long long int); +PRINT_FUNC(luint, unsigned long int); +PRINT_FUNC(uint64, uint64_t); +#undef PRINT_FUNC +#pragma GCC diagnostic pop + void print_color_string(enum output_type type, enum color_attr color, const char *key,