From patchwork Tue Dec 12 17:33:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1875336 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=SD2r7r6L; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SqQhT5Grkz1ySd for ; Wed, 13 Dec 2023 04:34:01 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0BED6807E2; Tue, 12 Dec 2023 18:33:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SD2r7r6L"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9D83D80844; Tue, 12 Dec 2023 18:33:55 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 53611804CE for ; Tue, 12 Dec 2023 18:33:51 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=zajec5@gmail.com Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-50dfac6c0beso3426447e87.2 for ; Tue, 12 Dec 2023 09:33:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702402431; x=1703007231; darn=lists.denx.de; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=sSEzRB6F/9q3x0IvJ35JV0W7klsQRoW3I1+qiHJDAOU=; b=SD2r7r6Lm/N3b53u+K9AA6VRAamPGDnd7s3NSmAMT8E4FFj+PbhDGhnAE+tvBEdbHp OWDeL4umVKVIlxctw48RAoJvDYBZaW4Qg8oUd7W0oSejRHY9C9uF9dIqysK82ZWpfq0S u2CeSeG9jwJxVKvJoxW4WtBjUY3IsAvx/dNcN3dkXceGS4kGFrtyuGEPE+jUHu8RBO4B Vh+AMNJ7BqtNlWord4th4nsvmR9cL922yGQP9vGz3MKzHYdQ41rpUOBd/a2CFKbdJGsl bJI4rV0keANdFjvG6gStTDiPMCjB2QnuS7weU98pBydXCYImDtVi9nresZ39A8GVq5HY +Qeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702402431; x=1703007231; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sSEzRB6F/9q3x0IvJ35JV0W7klsQRoW3I1+qiHJDAOU=; b=C8qPYq3uEHa800RK84ZBOiONHDIgJZLmt/Gj7+cwA9Hv7VLLyJCSyrHPvdFhnVzuD1 k9HeUKqimxwsX06MocxVlyv1UfVHcBwOUYv3GivgRYvnqQ9gcff3/B3weSNLsUFvWTQz 2/cJlBgjMbnyjavh4NDXfkiIt2OVjnXUJPEzCbPTmpfP7q3dakxK6SxmH7UjWYTbEKjB 1C6Qa+0s/thadhzkadHh35Gg6lAT1RymtBwsOXtkhKnkLHTiz5Pcf2XKOY4iRPaxUXef 5KYWlxmxhXxRpX1qJFvC+S5zYVdy3MZLzYXox/xBI7wmImnbtSW1J2N97DU3SgsmpOrm rK2g== X-Gm-Message-State: AOJu0YyQo8DW8iY56gbGZ4+afz0XdeZA/TjxXGY57rVjHGGK5vLrLk+k wDFSJ9kxYSxhRjInJ97D+Hmc5yfc02I= X-Google-Smtp-Source: AGHT+IHYqK4wASv56l1bxQz+d1Uy4WQQ6ZbuR4D4uRv9KnfhOojSPT0v5JBZrqijsMCG08k2WGRHxw== X-Received: by 2002:a05:6512:1154:b0:50d:1b79:b34a with SMTP id m20-20020a056512115400b0050d1b79b34amr3561135lfg.82.1702402430668; Tue, 12 Dec 2023 09:33:50 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id vi7-20020a170907d40700b00a1dc7e789fbsm6493714ejc.21.2023.12.12.09.33.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 09:33:50 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Tom Rini , u-boot@lists.denx.de Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Jaehoon Chung Subject: [PATCH] fw_env: fix reading NVMEM device's "compatible" value Date: Tue, 12 Dec 2023 18:33:27 +0100 Message-Id: <20231212173327.22819-1-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: Rafał Miłecki Call to fread() was changed to check for return value. The problem is it can't be checked for returning 1 (as it is) to determine success. We call fread() with buffer size as "size" argument. Reading any "compatible" value shorter than buffer size will result in returning 0 even on success. Modify code to use fstat() to determine expected read length. This fixes regression that broke using fw_env with NVMEM devices. Fixes: c059a22b7776 ("tools: env: fw_env: Fix unused-result warning") Cc: Jaehoon Chung Signed-off-by: Rafał Miłecki --- tools/env/fw_env.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index c9a8774ace..bf8b7ba304 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -1732,6 +1732,7 @@ static int find_nvmem_device(void) } while (!nvmem && (dent = readdir(dir))) { + struct stat s; FILE *fp; size_t size; @@ -1749,14 +1750,22 @@ static int find_nvmem_device(void) continue; } - size = fread(buf, sizeof(buf), 1, fp); + if (fstat(fileno(fp), &s)) { + fprintf(stderr, "Failed to fstat %s\n", comp); + goto next; + } + + if (s.st_size >= sizeof(buf)) { + goto next; + } + + size = fread(buf, s.st_size, 1, fp); if (size != 1) { fprintf(stderr, "read failed about %s\n", comp); - fclose(fp); - return -EIO; + goto next; } - + buf[s.st_size] = '\0'; if (!strcmp(buf, "u-boot,env")) { bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name); @@ -1765,6 +1774,7 @@ static int find_nvmem_device(void) } } +next: fclose(fp); }