summaryrefslogtreecommitdiff
path: root/cli/standalone.rs
diff options
context:
space:
mode:
authorYosi Pramajaya <yosi.pramajaya@gmail.com>2020-12-13 02:41:43 +0700
committerGitHub <noreply@github.com>2020-12-12 20:41:43 +0100
commit84ef9bd21fb48fb6b5fbc8dafc3de9f361bade3b (patch)
tree45c5f2c46425f72f07e92ee1f212ac522732279e /cli/standalone.rs
parent623bc22ad0aa7446bc383dc93d7c8da3c4c2fe22 (diff)
fix(cli/compile): error when the output path already exists (#8681)
Diffstat (limited to 'cli/standalone.rs')
-rw-r--r--cli/standalone.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/cli/standalone.rs b/cli/standalone.rs
index 067fec36a..6559242bd 100644
--- a/cli/standalone.rs
+++ b/cli/standalone.rs
@@ -5,6 +5,7 @@ use crate::tokio_util;
use crate::version;
use crate::worker::MainWorker;
use crate::worker::WorkerOptions;
+use deno_core::error::bail;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::FutureExt;
@@ -169,6 +170,24 @@ pub async fn create_standalone_binary(
} else {
output
};
+
+ if output.exists() {
+ // If the output is a directory, throw error
+ if output.is_dir() {
+ bail!("Could not compile: {:?} is a directory.", &output);
+ }
+
+ // Make sure we don't overwrite any file not created by Deno compiler.
+ // Check for magic trailer in last 16 bytes
+ let mut output_file = File::open(&output)?;
+ output_file.seek(SeekFrom::End(-16))?;
+ let mut trailer = [0; 16];
+ output_file.read_exact(&mut trailer)?;
+ let (magic_trailer, _) = trailer.split_at(8);
+ if magic_trailer != MAGIC_TRAILER {
+ bail!("Could not compile: cannot overwrite {:?}.", &output);
+ }
+ }
tokio::fs::write(&output, final_bin).await?;
#[cfg(unix)]
{