summaryrefslogtreecommitdiffstats
path: root/zellij-utils/src/cli.rs
diff options
context:
space:
mode:
authordeepsghimire <70006817+deepsghimire@users.noreply.github.com>2023-09-16 12:00:35 +0545
committerGitHub <noreply@github.com>2023-09-16 15:15:35 +0900
commitf1bef8f0d150df38352379a2bce5666838cb0438 (patch)
treedb01b52bfb7f6061b85056ae29b9455faa5fe3a6 /zellij-utils/src/cli.rs
parente70743e3f1911be1ee7961058e350e6416885c3e (diff)
fix(utils): validate session name (#2607)
* fix(utils): validate session name * cargo fmt * refactor: assign constant values to variables * refactor: move operations unrealted to the condition --------- Co-authored-by: Jae-Heon Ji <atx6419@gmail.com>
Diffstat (limited to 'zellij-utils/src/cli.rs')
-rw-r--r--zellij-utils/src/cli.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs
index f8c59f543..323834b37 100644
--- a/zellij-utils/src/cli.rs
+++ b/zellij-utils/src/cli.rs
@@ -9,6 +9,30 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use url::Url;
+fn validate_session(name: &str) -> Result<String, String> {
+ #[cfg(unix)]
+ {
+ use crate::consts::ZELLIJ_SOCK_MAX_LENGTH;
+
+ let mut socket_path = crate::consts::ZELLIJ_SOCK_DIR.clone();
+ socket_path.push(name);
+
+ if socket_path.as_os_str().len() >= ZELLIJ_SOCK_MAX_LENGTH {
+ // socket path must be less than 108 bytes
+ let available_length = ZELLIJ_SOCK_MAX_LENGTH
+ .saturating_sub(socket_path.as_os_str().len())
+ .saturating_sub(1);
+
+ return Err(format!(
+ "session name must be less than {} characters",
+ available_length
+ ));
+ };
+ };
+
+ Ok(name.to_owned())
+}
+
#[derive(Parser, Default, Debug, Clone, Serialize, Deserialize)]
#[clap(version, name = "zellij")]
pub struct CliArgs {
@@ -25,7 +49,7 @@ pub struct CliArgs {
pub server: Option<PathBuf>,
/// Specify name of a new session
- #[clap(long, short, overrides_with = "session", value_parser)]
+ #[clap(long, short, overrides_with = "session", value_parser = validate_session)]
pub session: Option<String>,
/// Name of a predefined layout inside the layout directory or the path to a layout file