From patchwork Fri May 16 15:25:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Moryakov X-Patchwork-Id: 2086795 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=D+n5D5Ir; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4ZzW9y6gKhz1yYR for ; Sat, 17 May 2025 01:25:42 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8BA728006D; Fri, 16 May 2025 17:25:55 +0200 (CEST) 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="D+n5D5Ir"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2C9A982998; Fri, 16 May 2025 17:25:54 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) (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 3A3C78129F for ; Fri, 16 May 2025 17:25:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ant.v.moryakov@gmail.com Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-43edecbfb94so23913775e9.1 for ; Fri, 16 May 2025 08:25:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747409151; x=1748013951; 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=M7OnFqQC9vmPtOx/grx76LrORgu7/IPi6GTTAkEqoYc=; b=D+n5D5Ir3VcgtVT34cbzJlY/klAaYalbc8uzr8N4QZDKo/87Fr5lLRjb9OlzpeKyRO yRemeP4kYhOZ6wlXEfBiKu4oFn+W9gfury47IvmiR3jE2gWwxSstxI274xF6uJZbeZh/ Bne31qjXAp4G6r2kF3TbVlKoqGw110l+ruVV3U/dCN3CCePbRUyJzZ541O3XqtA44yxI Z5NcWs3+i4vk1xKp0UJSeOzLm/xc1Ck0u5UH9gCXHiE5RROokLZLonGjjDZqmy484C1s nBcLMe/zQf9cfdxXSSQ7bOdskCq4TiEs4am7v7h5J6nKHPuW6FH4/hog2PaGPM6tGS2f 6MQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747409151; x=1748013951; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state: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: by 2002:a05:6512:31d1:b0:54a:cc04:ea24 with SMTP id 2adb3069b0e04-550e72418e1mr1571222e87.46.1747409140742; Fri, 16 May 2025 08:25:40 -0700 (PDT) Received: from anton-desktop.. (109-252-120-31.nat.spd-mgts.ru. [109.252.120.31]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-328085b8f15sm4579291fa.83.2025.05.16.08.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 May 2025 08:25:40 -0700 (PDT) From: ant.v.moryakov@gmail.com To: u-boot@lists.denx.de Cc: trini@konsulko.com, Anton Moryakov Subject: [PATCH] tools: fix handle leak in ifdtool.c Date: Fri, 16 May 2025 18:25:38 +0300 Message-Id: <20250516152538.75325-1-ant.v.moryakov@gmail.com> X-Mailer: git-send-email 2.34.1 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: Anton Moryakov Prevent file descriptor leaks by properly closing 'fd' and 'new_fd' when fstat() or write() operations fail. - Added close(fd) before return in open_for_read() if fstat() fails. - Added close(new_fd) before return in write_image() if write() fails. - No close needed if open() fails (fd == -1 is invalid). Signed-off-by: Anton Moryakov Reviewed-by: Quentin Schulz --- tools/ifdtool.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/ifdtool.c b/tools/ifdtool.c index b70570361f4..5db6fa7d195 100644 --- a/tools/ifdtool.c +++ b/tools/ifdtool.c @@ -499,8 +499,10 @@ static int write_image(char *filename, char *image, int size) S_IWUSR | S_IRGRP | S_IROTH); if (new_fd < 0) return perror_fname("Could not open file '%s'", filename); - if (write(new_fd, image, size) != size) + if (write(new_fd, image, size) != size) { + close(new_fd); return perror_fname("Could not write file '%s'", filename); + } close(new_fd); return 0; @@ -604,8 +606,10 @@ int open_for_read(const char *fname, int *sizep) if (fd == -1) return perror_fname("Could not open file '%s'", fname); - if (fstat(fd, &buf) == -1) + if (fstat(fd, &buf) == -1) { + close(fd); return perror_fname("Could not stat file '%s'", fname); + } *sizep = buf.st_size; debug("File %s is %d bytes\n", fname, *sizep);