This is an automated email from the ASF dual-hosted git repository. pengzheng pushed a commit to branch hotfix/query-cmd-fix in repository https://gitbox.apache.org/repos/asf/celix.git
commit 02ddb5cb72833477d3553a7db9e2d89eb00218ee Author: PengZheng <[email protected]> AuthorDate: Tue Apr 22 21:59:57 2025 +0800 Fix the "query" command to properly validate bundle ID input. Previously, query filtering with a service name or LDAP filter is broken. --- bundles/shell/shell/gtest/src/ShellTestSuite.cc | 23 ++++++++++++++++++++++- bundles/shell/shell/src/query_command.c | 3 ++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bundles/shell/shell/gtest/src/ShellTestSuite.cc b/bundles/shell/shell/gtest/src/ShellTestSuite.cc index b7b5f791e..b361db02e 100644 --- a/bundles/shell/shell/gtest/src/ShellTestSuite.cc +++ b/bundles/shell/shell/gtest/src/ShellTestSuite.cc @@ -41,6 +41,7 @@ public: auto properties = celix_properties_create(); celix_properties_set(properties, "LOGHELPER_ENABLE_STDOUT_FALLBACK", "true"); celix_properties_set(properties, CELIX_FRAMEWORK_CACHE_DIR, ".cacheShellTestSuite"); + celix_properties_setBool(properties, CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE, true); celix_properties_set(properties, "CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL", "trace"); //to ensure "query 0" is still a test case for am empty result. @@ -202,6 +203,16 @@ TEST_F(ShellTestSuite, queryTest) { EXPECT_TRUE(found != nullptr); free(buf); } + { + char *buf = nullptr; + size_t len; + FILE *sout = open_memstream(&buf, &len); + command->executeCommand(command->handle, "query 1", sout, sout); + fclose(sout); + char* found = strstr(buf, "Provided services found 1"); //note could be 11, 12, etc + EXPECT_TRUE(found != nullptr); + free(buf); + } { char *buf = nullptr; size_t len; @@ -209,7 +220,17 @@ TEST_F(ShellTestSuite, queryTest) { auto cmd = std::string{"query "} + std::to_string(d->resourceBundleId); command->executeCommand(command->handle, cmd.c_str(), sout, sout); //note query test resource bundle -> no results fclose(sout); - char* found = strstr(buf, "No results"); //note could be 11, 12, etc + char* found = strstr(buf, "No results"); + EXPECT_TRUE(found != nullptr); + free(buf); + } + { + char *buf = nullptr; + size_t len; + FILE *sout = open_memstream(&buf, &len); + command->executeCommand(command->handle, "query celix_shell_command", sout, sout); //note query test resource bundle -> no results + fclose(sout); + char* found = strstr(buf, "Provided services found 1"); //note could be 11, 12, etc EXPECT_TRUE(found != nullptr); free(buf); } diff --git a/bundles/shell/shell/src/query_command.c b/bundles/shell/shell/src/query_command.c index 49eb73444..4bc65033a 100644 --- a/bundles/shell/shell/src/query_command.c +++ b/bundles/shell/shell/src/query_command.c @@ -17,6 +17,7 @@ *under the License. */ +#include <ctype.h> #include <string.h> #include <stdlib.h> @@ -201,7 +202,7 @@ bool queryCommand_execute(void *_ptr, const char *command_line_str, FILE *sout, //check if its a number (bundle id) errno = 0; long bndId = strtol(sub_str, NULL, 10); - if (bndId >= CELIX_FRAMEWORK_BUNDLE_ID && errno == 0 /*not EINVAL*/) { + if (bndId >= CELIX_FRAMEWORK_BUNDLE_ID && isdigit(sub_str[0]) && errno == 0 /*not EINVAL*/) { opts.bndId = bndId; } else { //not option and not a bundle id -> query
