From 7b4164d3bad6d9301b20d670d9a4e121d04b5dd1 Mon Sep 17 00:00:00 2001 From: TapTap Date: Thu, 3 Sep 2026 22:14:47 +0200 Subject: [PATCH] fix: accept numeric chmod special bits --- src/shared/chmod.c | 4 ++-- tests/test_client_cli.c | 12 ++++++++++++ tests/test_metadata.c | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/shared/chmod.c b/src/shared/chmod.c index 058d80b..bfea77b 100644 --- a/src/shared/chmod.c +++ b/src/shared/chmod.c @@ -62,12 +62,12 @@ bool chmod_apply(mode_t mode, const char* spec, mode_t* result) { for (size_t i = 0; i < length && numeric; i++) numeric = spec[i] >= '0' && spec[i] <= '7'; if (numeric) { - if (length < 3 || (length == 4 && spec[0] != '0')) + if (length == 0 || length > 4) return false; mode_t parsed = 0; for (size_t i = 0; i < length; i++) parsed = (mode_t)((parsed << 3) | (spec[i] - '0')); - *result = (*result & ~07777) | parsed; + *result = parsed; return true; } diff --git a/tests/test_client_cli.c b/tests/test_client_cli.c index 311d28f..b6e4339 100644 --- a/tests/test_client_cli.c +++ b/tests/test_client_cli.c @@ -196,6 +196,17 @@ static void test_parse_args_chmod() { config_delete(cfg); } +static void test_parse_args_numeric_chmod() { + Config* cfg = config_create(); + char* argv[] = {"fastsync", "--chmod", "7777", "/src", "/dst"}; + int positional_args[2]; + int positional_count = 0; + EXPECT_EQ_INT(parse_args(cfg, 5, argv, positional_args, &positional_count), 0); + EXPECT_EQ_STR(cfg->chmod_spec, "7777"); + EXPECT_TRUE(cfg->use_metadata); + config_delete(cfg); +} + static void test_parse_args_rejects_invalid_chmod() { Config* cfg = config_create(); char* argv[] = {"fastsync", "--chmod=a+X", "/src", "/dst"}; @@ -377,6 +388,7 @@ void test_client_cli() { test_parse_args_version(); test_parse_args_valid_port(); test_parse_args_chmod(); + test_parse_args_numeric_chmod(); test_parse_args_rejects_invalid_chmod(); test_parse_args_invalid_port(); test_parse_args_non_numeric_port(); diff --git a/tests/test_metadata.c b/tests/test_metadata.c index 4933e58..36f81cd 100644 --- a/tests/test_metadata.c +++ b/tests/test_metadata.c @@ -151,8 +151,17 @@ static void test_chmod_changes() { EXPECT_EQ_INT(result, 0644); EXPECT_TRUE(chmod_apply(0644, "a+x", &result)); EXPECT_EQ_INT(result, 0755); + result = 0777; + EXPECT_TRUE(chmod_apply(0777, "0000", &result)); + EXPECT_EQ_INT(result, 0000); + result = 0777; + EXPECT_TRUE(chmod_apply(0777, "7777", &result)); + EXPECT_EQ_INT(result, 07777); + result = 0777; EXPECT_TRUE(chmod_apply(0777, "755", &result)); EXPECT_EQ_INT(result, 0755); + EXPECT_FALSE(chmod_apply(0777, "888", &result)); + EXPECT_FALSE(chmod_apply(0777, "10000", &result)); EXPECT_FALSE(chmod_apply(0777, "a+X", &result)); EXPECT_FALSE(chmod_apply(0777, "a+r,", &result)); }