Apache Solr open-source search software
SOLR-6662: better validation when parsing command-line options that expect a value
Timothy Potterd76c1768b621
diff --git a/solr/bin/solr b/solr/bin/solrindex b9531950364..0d5e54a4e03 100755--- a/solr/bin/solr+++ b/solr/bin/solr@@ -342,7 +342,7 @@ function stop_solr() {DIR="$1"SOLR_PORT="$2"- STOP_PORT="79${SOLR_PORT: -2}"+ STOP_PORT=`expr $SOLR_PORT - 1000`STOP_KEY="$3"SOLR_PID="$4"@@ -408,7 +408,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; thenwhile true; docase $1 in-c|-collection)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected collection name but found $2 instead!"exit 1fi@@ -416,7 +416,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; thenshift 2;;-z|-zkhost)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected a ZooKeeper connection string but found $2 instead!"exit 1fi@@ -472,7 +472,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];while true; docase $1 in-n|-name)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected $CREATE_TYPE name but found $2 instead!"exit 1fi@@ -480,7 +480,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];shift 2;;-c|-configset)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected configset name but found $2 instead!"exit 1fi@@ -488,7 +488,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];shift 2;;-shards)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected shard count but found $2 instead!"exit 1fi@@ -496,7 +496,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];shift 2;;-replicationFactor)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected replication factor but found $2 instead!"exit 1fi@@ -549,7 +549,6 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];STATUS_INFO=`run_tool status -solr http://localhost:$CREATE_PORT/solr | tr -d ' '`IS_CLOUD=`echo $STATUS_INFO | grep $'"cloud":' | tr -d ' '`- echo $IS_CLOUDif [ "$IS_CLOUD" != "" ]; thenif [ "$SCRIPT_CMD" == "create_core" ]; thenecho -e "\nERROR: Solr running on port $CREATE_PORT is running in SolrCloud mode, please use create_collection command instead.\n"@@ -593,7 +592,7 @@ if [ $# -gt 0 ]; thenshift;;-d|-dir)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!"exit 1fi@@ -607,7 +606,7 @@ if [ $# -gt 0 ]; thenshift 2;;-s|-solr.home)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!"exit 1fi@@ -616,7 +615,7 @@ if [ $# -gt 0 ]; thenshift 2;;-e|-example)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected example name but found $2 instead!"exit 1fi@@ -628,7 +627,7 @@ if [ $# -gt 0 ]; thenshift;;-h|-host)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected hostname but found $2 instead!"exit 1fi@@ -636,7 +635,7 @@ if [ $# -gt 0 ]; thenshift 2;;-m|-memory)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected memory setting but found $2 instead!"exit 1fi@@ -644,7 +643,7 @@ if [ $# -gt 0 ]; thenshift 2;;-p|-port)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected port number but found $2 instead!"exit 1fi@@ -652,7 +651,7 @@ if [ $# -gt 0 ]; thenshift 2;;-z|-zkhost)- if [ "${2:0:1}" == "-" ]; then+ if [[ "$2" == "" || "${2:0:1}" == "-" ]]; thenprint_usage "$SCRIPT_CMD" "Expected ZooKeeper connection string but found $2 instead!"exit 1fi@@ -853,7 +852,7 @@ if [ "$SOLR_PORT" == "" ]; thenfiif [ "$STOP_PORT" == "" ]; then- STOP_PORT="79${SOLR_PORT: -2}"+ STOP_PORT=`expr $SOLR_PORT - 1000`fiif [[ "$SCRIPT_CMD" == "start" ]]; then@@ -985,7 +984,7 @@ fifunction launch_solr() {run_in_foreground="$1"- stop_port="79${SOLR_PORT: -2}"+ stop_port="$STOP_PORT"SOLR_ADDL_ARGS="$2"@@ -1088,7 +1087,7 @@ if [ "$EXAMPLE" != "cloud" ]; then"$JAVA" -Durl=http://localhost:$SOLR_PORT/solr/$EXAMPLE/update -jar $SOLR_TIP/example/exampledocs/post.jar $SOLR_TIP/example/exampledocs/*.xmlfi- echo -e "\nSolr $EXAMPLE launched successfully. Direct your Web browser to http://localhost:$SOLR_PORT/solr to visit the Solr Admin UI\n"+ echo -e "\nSolr $EXAMPLE example launched successfully. Direct your Web browser to http://localhost:$SOLR_PORT/solr to visit the Solr Admin UI\n"fielse#diff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmdindex d6f30d4d08e..ea47cd9e9c3 100644--- a/solr/bin/solr.cmd+++ b/solr/bin/solr.cmd@@ -278,6 +278,10 @@ goto parse_args:set_server_dirset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Directory name is required!+ goto invalid_cmd_line+)set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected directory but found %2 instead!@@ -297,6 +301,11 @@ goto parse_args:set_solr_home_dirset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Directory name is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected directory but found %2 instead!@@ -310,6 +319,11 @@ goto parse_args:set_exampleset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Example name is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected example name but found %2 instead!@@ -324,6 +338,11 @@ goto parse_args:set_memoryset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Memory setting is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected memory setting but found %2 instead!@@ -337,6 +356,11 @@ goto parse_args:set_hostset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Hostname is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected hostname but found %2 instead!@@ -350,6 +374,11 @@ goto parse_args:set_portset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Port is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected port but found %2 instead!@@ -363,9 +392,14 @@ goto parse_args:set_stop_keyset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=Stop key is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (- set SCRIPT_ERROR=Expected port but found %2 instead!+ set SCRIPT_ERROR=Expected stop key but found %2 instead!goto invalid_cmd_line)set STOP_KEY=%~2@@ -381,6 +415,11 @@ goto parse_args:set_zookeeperset "arg=%~2"+IF "%arg%"=="" (+ set SCRIPT_ERROR=ZooKeeper connection string is required!+ goto invalid_cmd_line+)+set firstChar=%arg:~0,1%IF "%firstChar%"=="-" (set SCRIPT_ERROR=Expected ZooKeeper connection string but found %2 instead!@@ -638,7 +677,9 @@ IF "%EXAMPLE%"=="techproducts" ()@echo.-@echo Solr %EXAMPLE% launched successfully.+IF NOT "%EXAMPLE%"=="" (+ @echo Solr %EXAMPLE% example launched successfully.+)@echo Direct your Web browser to http://localhost:%SOLR_PORT%/solr to visit the Solr Admin UI@echo.