From patchwork Sun Oct 8 14:38:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 823007 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 (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="IhsTxaFA"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3y95bq62mSz9t3t for ; Mon, 9 Oct 2017 01:39:03 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753650AbdJHOjB (ORCPT ); Sun, 8 Oct 2017 10:39:01 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:37287 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753623AbdJHOi7 (ORCPT ); Sun, 8 Oct 2017 10:38:59 -0400 Received: by mail-wm0-f67.google.com with SMTP id r68so5118985wmr.4 for ; Sun, 08 Oct 2017 07:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=dpDRxoHOmSFg74nibsNy54aWydqy9HZx9UJwncAIDR8=; b=IhsTxaFAUcebOHimF8bUEKJ0H01uHCkSJP9HQrSB1dvC0RvqwSSiMGWpPNMr21qqEo 19zjt09/AA0SzeSy07M6LZz1h+ojIorna5QphOk0sj1O7kiQ8WXr8hrviYgez4TGJXFG GZOHKhohVrXVhmVXmHMi23dnHl/MA5QRisG7+1lh7QiqpnsdhQiNFGuwfr9wkhU05Ot/ Wihw4XnWSIXvzND65ciKj+LUiD7M5fXEs9x4rYyp7hvtFYE+Wbm2U5mhE8JlhErXgprw tMv5CqtHF8Sro4t1OUz1hkt8AalryJRRWRJMv0yJtjBDQPtObI8c62/2ErYIkSMDLXWe 0NSA== 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; bh=dpDRxoHOmSFg74nibsNy54aWydqy9HZx9UJwncAIDR8=; b=dUF2W44/9ghecBxkXtIzIoC9vp15gCAW+vs0OIgfGQCtkijnSrNE0sPCQa8qYonw1Z 5UVF0UhYbtowCRWeZSdnXN8UB8u7B4/PxK1VzsOgFVM2OQ6XhVSrldMrlQSLXxiefBOJ t5QB9OtuoEXrpvAk4MqGhmXdgOMU6lg6OoaCaSqmfCMISCNHxNpoL/9UMhG9rWbtaKFv A5A647dSbkK4ILWyF4uPwqmXOvGoreiQgbvGUeEv+kcKAbmpGLCIcQt91lNWEZtRwb8G fHdwukJOtWX/Zc4IsegcX4TehfrOS03LBrlYX4Pn/XMkv0LuCGW4WtB4DpHA4+i7RWwg sIPg== X-Gm-Message-State: AMCzsaVkUvc/OhKO/j50DrUitkmWL79DJrPXVnW4LneFY0k/dbPRwFyi Uf6LMPKYbsc/Z7c8vM5rdCDXUw== X-Google-Smtp-Source: AOwi7QDsUej80h9Rwdg3zVULGFVptyGARHxTvfojrlsS+v6wbn+sqIdGKsaprNhYxvnwPw3Hv/Mumg== X-Received: by 10.223.151.51 with SMTP id r48mr6930524wrb.164.1507473537755; Sun, 08 Oct 2017 07:38:57 -0700 (PDT) Received: from localhost.localdomain ([85.119.94.113]) by smtp.gmail.com with ESMTPSA id o11sm3434494wrg.5.2017.10.08.07.38.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Oct 2017 07:38:57 -0700 (PDT) From: Petr Vorel To: netdev@vger.kernel.org Cc: Petr Vorel , Julien Fortin , Stephen Hemminger Subject: [PATCH iproute2 1/1] color: Fix ip segfault in color_fprintf() when using --color switch Date: Sun, 8 Oct 2017 16:38:47 +0200 Message-Id: <20171008143847.21699-1-petr.vorel@gmail.com> X-Mailer: git-send-email 2.14.2 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This fixes two regressions: Commit 959f1428 ("color: add new COLOR_NONE and disable_color function") caused segfault, when running ip with --color switch, as 'attr + 8' in color_fprintf() access array item out of bounds. Changing latter value of ternar operator in attr_colors[] index is for restoring the same colors. Reproduce the bug with: $ ip -c a Commit d0e72011 ("ip: ipaddress.c: add support for json output") introduced passing -1 as enum color_attr. This is not only wrong as no color_attr has value -1, but also causes another segfault in color_fprintf() on this setup as there is no item with index -1 in array of enum attr_colors[]. Using 0 is valid option. Reproduce the bug with: $ COLORFGBG='0;15' ip -c a NOTE: COLORFGBG is environmental variable used for defining whether user has light or dark background. COLORFGBG="0;15" is used to ask for color set suitable for light background, COLORFGBG="15;0" is used to ask for color set suitable for dark background. Signed-off-by: Petr Vorel --- include/json_print.h | 2 +- lib/color.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index b6ce1f9f..2f3f07c8 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -53,7 +53,7 @@ void close_json_array(enum output_type type, const char *delim); const char *fmt, \ type value) \ { \ - print_color_##type_name(t, -1, key, fmt, value); \ + print_color_##type_name(t, 0, key, fmt, value); \ } _PRINT_FUNC(int, int); _PRINT_FUNC(bool, bool); diff --git a/lib/color.c b/lib/color.c index 79d5e289..e597798f 100644 --- a/lib/color.c +++ b/lib/color.c @@ -110,7 +110,7 @@ int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...) } ret += fprintf(fp, "%s", - color_codes[attr_colors[is_dark_bg ? attr + 8 : attr]]); + color_codes[attr_colors[is_dark_bg ? attr + 6 : attr - 1]]); ret += vfprintf(fp, fmt, args); ret += fprintf(fp, "%s", color_codes[C_CLEAR]);