Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1045431lqd; Thu, 25 Apr 2024 04:43:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUi+qlXo4EiAV+XO8tiTgXyiVW+tZWkLCSl8xze9Jm3XUmfs8ZoVVYJmpWKumpHazbMo3MK9D9SMe8V5qcPwF1aCtqjPBn1ZVDruUk2vw== X-Google-Smtp-Source: AGHT+IEMkQbVUYdvffiyuwzVDkt9lVXXrwjRSrxstEgSOFPuEq3/A8MkuDHkD/1lrrHnDFuVkR/p X-Received: by 2002:a17:906:52d8:b0:a51:95f1:4308 with SMTP id w24-20020a17090652d800b00a5195f14308mr3922093ejn.51.1714045407990; Thu, 25 Apr 2024 04:43:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714045407; cv=pass; d=google.com; s=arc-20160816; b=dnAiGZCVAhNvglOVum1Is6buNvXBL9nF3HH9XTpAhBqPOXLcFl9SeEUyiGGH0Vkv07 WVRcCTat989Qmr9gUW83yxCXwcyPJ8vWHgSNUKq/sXyy6/yvNi3N+1+qrTbYGgS0BAGZ 7RS9P5Hb0kKTnDpiJ4IaFNSVujYla3NRGX6ISgBO5iUWvZGE55Z3uSqiBJGrUnAbXG6p /aZU6BfssNVuuc9MPbUR86JDJG1yFOeGNeqlo9OsdyR+5cHd01k1hZVtnbPXpMU8mK4K sSYNU+WADKb0RbHP1SG8qqsS+K9Y6BgVv3Nw7egKrAg+XEycXlNijMGkRD1gfYc1Gbir cvHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=lc9xuCWwYIx7hSoqrOy+IbDJpFX48pGg8iO4Ae5Eluc=; fh=UUBnW7XVqDYJ9nUciQx/e7NB7NBKcODLmMFauNG28Mc=; b=YDoccUT+rL4IDS10IWbI2NEl7yVJSGpVcILvVjUcBugpSI/Q5xQi9hoQiYYpuN+dsb oXuiekdywlO5Ioycr2CvYs6znJN8tEvT4mifWV5AVPnjKcpcCRN2t87rWXdg92e7ENF5 xj8+5iCXdXjLGg+iOIrysvdv15CBGrfr1BjbQzA7PnmlZSO8DHrm3yFvCHGfwjSSBtHB QD+Ge3e0B8IdMZAaA3nkPYjzQB4LyY/qtuhatXdxBlBp9xcuGD7IURvRK0cZWzxcqv48 g6n5OfnzvoWXzHwYiDXca1Tg1DqiHZEgGWRGochKqxTfkIO0yv/uL9CSafY6oN7lB6U9 F/fg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=UaC2S4av; arc=pass (i=1 spf=pass spfdomain=microchip.com dkim=pass dkdomain=microchip.com dmarc=pass fromdomain=microchip.com); spf=pass (google.com: domain of linux-kernel+bounces-158468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microchip.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z18-20020a170906669200b00a47503563c2si9544483ejo.294.2024.04.25.04.43.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 04:43:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=UaC2S4av; arc=pass (i=1 spf=pass spfdomain=microchip.com dkim=pass dkdomain=microchip.com dmarc=pass fromdomain=microchip.com); spf=pass (google.com: domain of linux-kernel+bounces-158468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microchip.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B4E9A1F22CE4 for ; Thu, 25 Apr 2024 11:43:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BFA912AAE0; Thu, 25 Apr 2024 11:43:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b="UaC2S4av" Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFE401E497; Thu, 25 Apr 2024 11:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=68.232.154.123 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714045400; cv=none; b=fg4RZoaZqU2Y+/vN9TxM9ljvvmLnBK0ouHNUTDswGTWz0ud19Ca768CYrAS14xoYIfWh93YEw9wHjYW/4cVRon5pGS8a4ijYFiacICfDhf5plFmSQDNk1yc8Jem31qZInPcsh3Xth76rq+P5nLahrWh+HM7sh+d2SaWJgFEtGOg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714045400; c=relaxed/simple; bh=r9iQCgsNre3zD/T+CXIOgTbGIYkjP59ItYR/6wtRGDE=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Vk16jhmV7m0kXOs9OtjooA+cSwORSe+hTAtC5mwuXKRMzHGyBr96rGYERNdRB+WS7gDJryomTOFb+/9ou6qQfCSYRKOp9oDLxbq5UsWiVcd/Pm4KdTi6oCDEI6pT71VQSikYw9X1dqk2541ZFNHTunEYe2IWOx8cTysj+KMJo8I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com; spf=pass smtp.mailfrom=microchip.com; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b=UaC2S4av; arc=none smtp.client-ip=68.232.154.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=microchip.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1714045398; x=1745581398; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=r9iQCgsNre3zD/T+CXIOgTbGIYkjP59ItYR/6wtRGDE=; b=UaC2S4avZrmuOk+wRRkCKNAIqWXgBBiGTvNb/lGjAGz6Y1jXyHQXi8qA 8YGUIxZ04+py4TCmg0SmGSGm9a0yd6yOkZ+JnSpGJmDtR/YjnMdmPmRBJ cGArFt98NBMFs4lnOyHWw6jGl38cjCLFwMiXBP/1iLEuRWk22g13vOvLG VFxZ8ythAVJsj3HxhNlZCO11YFRZG9spvPa5KDmpgRPyTwxvXDKrJRKMi SUbPIx8AmJzL47dYzkBGDVJrkZuA7mO63nY1zEs+tVqKWYJk6inHKele5 8aHaE+rxQeeodC2A+1U64EQNx0o1aCSA6tjsHbsvje/BBt3RSfNwZoZwr g==; X-CSE-ConnectionGUID: qteOOrYnSzSrTY39UU9dBg== X-CSE-MsgGUID: n5dJTltjRx+4BgqX4bl2Zg== X-IronPort-AV: E=Sophos;i="6.07,229,1708412400"; d="scan'208";a="22585804" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa4.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 25 Apr 2024 04:43:16 -0700 Received: from chn-vm-ex04.mchp-main.com (10.10.85.152) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 25 Apr 2024 04:42:36 -0700 Received: from marius-VM.mshome.net (10.10.85.11) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Thu, 25 Apr 2024 04:42:34 -0700 From: To: , , , CC: , , , Subject: [PATCH v1] iio: adc: PAC1934: fix accessing out of bounds array index Date: Thu, 25 Apr 2024 14:42:32 +0300 Message-ID: <20240425114232.81390-1-marius.cristea@microchip.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain From: Marius Cristea Fix accessing out of bounds array index for average current and voltage measurements. The device itself has only 4 channels, but in sysfs there are "fake" channels for the average voltages and currents too. Fixes: 0fb528c8255b: "iio: adc: adding support for PAC193x" Reported-by: Conor Dooley Signed-off-by: Marius Cristea --- drivers/iio/adc/pac1934.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/iio/adc/pac1934.c b/drivers/iio/adc/pac1934.c index f751260605e4..456f12faa348 100644 --- a/drivers/iio/adc/pac1934.c +++ b/drivers/iio/adc/pac1934.c @@ -787,6 +787,15 @@ static int pac1934_read_raw(struct iio_dev *indio_dev, s64 curr_energy; int ret, channel = chan->channel - 1; + /* + * For AVG the index should be between 5 to 8. + * To calculate PAC1934_CH_VOLTAGE_AVERAGE, + * respectively PAC1934_CH_CURRENT real index, we need + * to remove the added offset (PAC1934_MAX_NUM_CHANNELS). + */ + if (channel >= PAC1934_MAX_NUM_CHANNELS) + channel = channel - PAC1934_MAX_NUM_CHANNELS; + ret = pac1934_retrieve_data(info, PAC1934_MIN_UPDATE_WAIT_TIME_US); if (ret < 0) return ret; base-commit: b80ad8e3cd2712b78b98804d1f59199680d8ed91 -- 2.34.1