From 3c5db8f419f32948dbc142b2ec1f4c1160b2fcdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 6 Feb 2023 16:19:22 +0100 Subject: [PATCH] src: avoid leaking snapshot fp on error Call fclose() on the snapshot file regardless of whether reading the snapshot data succeeded. PR-URL: https://github.com/nodejs/node/pull/46497 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Darshan Sen --- src/node.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/node.cc b/src/node.cc index c9fb6afd05b178..8604db69ed3dbb 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1168,14 +1168,15 @@ ExitCode LoadSnapshotDataAndRun(const SnapshotData** snapshot_data_ptr, return exit_code; } std::unique_ptr read_data = std::make_unique(); - if (!SnapshotData::FromBlob(read_data.get(), fp)) { + bool ok = SnapshotData::FromBlob(read_data.get(), fp); + fclose(fp); + if (!ok) { // If we fail to read the customized snapshot, simply exit with 1. // TODO(joyeecheung): should be kStartupSnapshotFailure. exit_code = ExitCode::kGenericUserError; return exit_code; } *snapshot_data_ptr = read_data.release(); - fclose(fp); } else if (per_process::cli_options->node_snapshot) { // If --snapshot-blob is not specified, we are reading the embedded // snapshot, but we will skip it if --no-node-snapshot is specified.