From patchwork Sat Mar 14 23:15:56 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1978 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773530256; bh=6oU/3eF1r4dCXecOmamHG3y0ucii5cIc9Xu9Ad+imqE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=QTJRSpDFiYFW4J6qeboPe54ViE4KsCwtg/MCwpJGrfOrjBRKRIH1ETcrlWbtgkkDh lymzaz371pMIOjaP4bPF2oKIxJwh4ptiNF1+UWH8MeOrNN7vEA06nxYhW8lv29P96M Ak9PNMVnSJ5gZFaRxr7shYI7cmnYaR/sqhChysKhW5B78qMKi6/rSyOym4c184jj33 5EJ9Cm6mYjf6tVFp9aXlwLLhMd9YUATsZ8KxfGnPkKEX0+ltm1I6eolHpxYncMoKXf y0aExM6fc5Dpu+K1/C1evisC+QTxpqHEqF34mBWMPBoHNW+GuGVwv9d/szCW7D8Vr0 XhLmzgHtCJuYA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 647F56A031 for ; Sat, 14 Mar 2026 17:17:36 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id QdxRQBjJ1uxX for ; Sat, 14 Mar 2026 17:17:36 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773530254; bh=6oU/3eF1r4dCXecOmamHG3y0ucii5cIc9Xu9Ad+imqE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=JiFc3Fg4dxgbSE1YjeqHAT/E4N8GT6Th2eWp+QsgLfXVQUl8GCZURu4ppfCmZTUhT B4K/eG0ascDxq4f0NELmlinOlXY18j2MP2W0xW6QGik5rwBVGP5h9UjUvFDD0cvHJV MUJibEEOQY7lLyOcmjq3I/Rz+58t3NzMzD7dUozrPiGwx7hzQkRdwXwLU6HvGsgQB1 7QpJZF8TK3EbmHwU+WUeyTcRsDfjTAcdzErLg1o/rm9t3vZSwGK2wSQGvSW/CfSBjb 2AQWS4cKIT3n2Ec1kUCSJMfUoGEs49Vwijqnj+CB41y+8X/m8/zLew6dBDKF87KcdZ 5L6IVG0Mz0wJg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3BF0B6A03F for ; Sat, 14 Mar 2026 17:17:34 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773530251; bh=8p+FcmxmqZKewUu0alPHC+ENvQcSiVZ+jDs0CiD1A74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IoN3AU48gbQI7xI2sLbZNcTCa2m/vBANAhOvpEI2hOJCnG2gjfCKb/6Yr05CdjDn6 v1mRVGpndnMnR/N4231T46Uwwe0EwuqDnhsgUvf0jkXD2CkoC0ebNRW+2gFFYKAkB0 pWS6DqUUryc3zdimsP3HValRGAHhNzhu4o8gqmSIC+lt+xpGm7JkYFxHPHyZe+Bljf VdmKJuJmxV8EdtvVbApvce8fAV4/kXJuMMKd7RyM/zEJJvSS5WySzoH9L0VunqZhDB T+QlhDJ7eguEJ/5WubyCowG4ErQlbdxeSzDLG4Kov2uvdLukZ9VuwcX1hs3MfCkoSV HZtRVc4n6QUEw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 98D2C6A031; Sat, 14 Mar 2026 17:17:31 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id apIFwciFBKoV; Sat, 14 Mar 2026 17:17:31 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1773530251; bh=9p7XhBRfK4UwfORhjXK0GHPAfsw02HHpkbx7QpKidnU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PZK4F/bDfeAg6uX6WBM0kzkxtJz64P2SpvOoX800Y8xHrbLk0Ux8DagaQSSQztRnH 1coGwD6hPvEVRxWt1QHKmH+/JPuczOdJU+AGzC8NA9TmElNsqe3pL15x7Oup9urFi9 iGgRP9RVdoOXMfRW6QwnG1C9SvEvYZBV4a+aioSUvQ2LbmTTlnjo6+PogKMMhswC1+ VYzPs3dkeooGGFrrHWtMEaT4jqLG6Omj5gDkkRtgmway3D/Ke8XgkcAkyUCplr59ht dLIJYrl8mryf+w4NuZC2JBRUFzce1M9pXjr+SoqY0kc/M0R7qp100cWg9Fz7MoF5pe jTx0/EPZeO3SQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 08D3A69FF7; Sat, 14 Mar 2026 17:17:30 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Sat, 14 Mar 2026 17:15:56 -0600 Message-ID: <20260314231618.338113-6-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260314231618.338113-1-sjg@u-boot.org> References: <20260314231618.338113-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 6KVHUE42V2JPTVFB3WRC5C4CEMVIXER7 X-Message-ID-Hash: 6KVHUE42V2JPTVFB3WRC5C4CEMVIXER7 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 05/19] bootstd: Fix memory leak in bootflow scanning List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass When bootflow_check() fails during scanning, bflow->name and other fields allocated by bootdev_find_in_blk() are not freed before the next iteration overwrites them. This causes a memory leak of about 1.2 MB across ~7,280 iterations in a typical sandbox test run. Add bootflow_free() calls in bootflow_scan_first() and bootflow_scan_next() to release the failed bootflow's resources before retrying. Place the free after the BOOTFLOWIF_ALL early return so that callers requesting all bootflows still receive the populated bflow. The subsequent bootflow_check() reinitialises the bflow via bootflow_init(), so there is no use-after-free risk. Signed-off-by: Simon Glass --- boot/bootflow.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/boot/bootflow.c b/boot/bootflow.c index 0511d7f6cb8..befe507af96 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -660,6 +660,7 @@ int bootflow_scan_first(struct udevice *dev, const char *label, return log_msg_ret("all", ret); } iter->err = ret; + bootflow_free(bflow); ret = bootflow_scan_next(iter, bflow); if (ret) return log_msg_ret("get", ret); @@ -693,6 +694,7 @@ int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow) if (iter->flags & BOOTFLOWIF_ALL) return log_msg_ret("all", ret); } + bootflow_free(bflow); } else { log_debug("incr failed, err=%d\n", ret); iter->err = ret;