Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2850145imu; Sun, 6 Jan 2019 11:31:22 -0800 (PST) X-Google-Smtp-Source: ALg8bN46SnWIKs6t2iFDzb7zMDQW/m6yrnUAwpCUPQHRZaSSuuCZu5acNIwMdGQ2TniTlZoLg/Dn X-Received: by 2002:a63:e84c:: with SMTP id a12mr8475539pgk.241.1546803082008; Sun, 06 Jan 2019 11:31:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546803081; cv=none; d=google.com; s=arc-20160816; b=Ot34uo/ghPmunKh0zA7vZzKYdPYhIdzhYo9dZ4q9AHiTateIIzWlhHlKHqaX67XkP3 sLWSQtYQ/gYicOyYkC3halZ9bHdkVVGCW6KQtOa18QaJyAGm8htMr44AniQVboxa0yW/ grU89gKgcuh0V4vKfTG5at5P38KcKWVfQfZviEQTKdG6+gUGWIe66oqXuuTxoHNktaoC N0kWJpVDPl1kz74O2JUqm/tnRJlwbLZyC8i8x4IA5DXmRn70F7zsat6s87MFtsEY0kqH jJofiG3N9R/aLbDlnPNA7U4J541OLapSGcPeOBV2SEEoCqaLCnm/LnSGOnIBy3xbbaVA 3XFw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=hIgGuo6ha+LxC9MmANerqWmoZNZ3lnyXe6alRqNQ6po=; b=XUrx5oy2X29LYas++shDXH+TGXyIxgnsB9Vydx6wM+1L5hkIGiPbaBRjcieOsqPCJX ZsqdaFZHOrH494N2YMc/psui6U/YtTVzzFDGS5dU59icdsR86CoHhDRGZTXQoC9oEYbL m4CVj5ZiaGEnOva4pjRx391uN4GhcpdE+gUMRtGedGrRPP6dpwkMBkKyzNNi3Kbz8k4L LIU9bmik4uqQFVx0epp+L4Y+tWR3rgvREi3kMcpVo96GbPU5mkvzlsl+S2InY+5dRDmS mvOyBu17z69dBCigfJWyvt6a2SgjdIhfCKliVaVO6QNWwRbDLMTK3ZjHvAg/C4lrA7Xx 655w== ARC-Authentication-Results: i=1; mx.google.com; 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 a19si9403281pgn.102.2019.01.06.11.31.06; Sun, 06 Jan 2019 11:31:21 -0800 (PST) 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; 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 S1726424AbfAFT25 (ORCPT + 99 others); Sun, 6 Jan 2019 14:28:57 -0500 Received: from smtp3-g21.free.fr ([212.27.42.3]:32302 "EHLO smtp3-g21.free.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726050AbfAFT25 (ORCPT ); Sun, 6 Jan 2019 14:28:57 -0500 Received: from localhost.localdomain (unknown [IPv6:2a02:8108:4840:8f74:143e:fcd5:beb7:2140]) (Authenticated sender: albeu) by smtp3-g21.free.fr (Postfix) with ESMTPA id 9E00F13F8C1; Sun, 6 Jan 2019 20:28:49 +0100 (CET) From: Alban Bedel To: Srinivas Kandagatla Cc: linux-kernel@vger.kernel.org, Alban Bedel Subject: [PATCH 4/8] nvmem: core: Fix cell lookup when no cell is found Date: Sun, 6 Jan 2019 20:28:16 +0100 Message-Id: <20190106192820.12558-5-albeu@free.fr> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190106192820.12558-1-albeu@free.fr> References: <20190106192820.12558-1-albeu@free.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the cell list is not empty and nvmem_find_cell_by_node/name() is called for a cell that is not present in the list they will return an invalid pointer instead of NULL. This happen because list_for_each_entry() stop once it reach the list head again, but as the list head is not contained in a struct nvmem_cell the iteration variable then contains an invalid value. This is easily solved by using a variable to iterate over the list and one to return the cell found. Signed-off-by: Alban Bedel --- drivers/nvmem/core.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 8e1b52559467..a7556b20cff4 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -525,12 +525,14 @@ static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) static struct nvmem_cell * nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) { - struct nvmem_cell *cell = NULL; + struct nvmem_cell *iter, *cell = NULL; mutex_lock(&nvmem_mutex); - list_for_each_entry(cell, &nvmem->cells, node) { - if (strcmp(cell_id, cell->name) == 0) + list_for_each_entry(iter, &nvmem->cells, node) { + if (strcmp(cell_id, iter->name) == 0) { + cell = iter; break; + } } mutex_unlock(&nvmem_mutex); @@ -994,12 +996,14 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) static struct nvmem_cell * nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) { - struct nvmem_cell *cell = NULL; + struct nvmem_cell *iter, *cell = NULL; mutex_lock(&nvmem_mutex); - list_for_each_entry(cell, &nvmem->cells, node) { - if (np == cell->np) + list_for_each_entry(iter, &nvmem->cells, node) { + if (np == iter->np) { + cell = iter; break; + } } mutex_unlock(&nvmem_mutex); -- 2.19.1