Unverified Commit 08ff606b authored by Meir Shpilraien (Spielrein)'s avatar Meir Shpilraien (Spielrein) Committed by GitHub
Browse files

Changed fuction name to be case insensitive. (#9984)

Use case insensitive string comparison for function names (like we do for commands and configs)
In addition, add verification that the functions only use the following characters: [a-zA-Z0-9_]
parent 63f606d3
...@@ -54,10 +54,10 @@ dictType engineDictType = { ...@@ -54,10 +54,10 @@ dictType engineDictType = {
}; };
dictType functionDictType = { dictType functionDictType = {
dictSdsHash, /* hash function */ dictSdsCaseHash, /* hash function */
dictSdsDup, /* key dup */ dictSdsDup, /* key dup */
NULL, /* val dup */ NULL, /* val dup */
dictSdsKeyCompare, /* key compare */ dictSdsKeyCaseCompare,/* key compare */
dictSdsDestructor, /* key destructor */ dictSdsDestructor, /* key destructor */
engineFunctionDispose,/* val destructor */ engineFunctionDispose,/* val destructor */
NULL /* allow to expand */ NULL /* allow to expand */
...@@ -438,10 +438,34 @@ NULL }; ...@@ -438,10 +438,34 @@ NULL };
addReplyHelp(c, help); addReplyHelp(c, help);
} }
/* Verify that the function name is of the format: [a-zA-Z0-9_][a-zA-Z0-9_]? */
static int functionsVerifyName(sds name) {
if (sdslen(name) == 0) {
return C_ERR;
}
for (size_t i = 0 ; i < sdslen(name) ; ++i) {
char curr_char = name[i];
if ((curr_char >= 'a' && curr_char <= 'z') ||
(curr_char >= 'A' && curr_char <= 'Z') ||
(curr_char >= '0' && curr_char <= '9') ||
(curr_char == '_'))
{
continue;
}
return C_ERR;
}
return C_OK;
}
/* Compile and save the given function, return C_OK on success and C_ERR on failure. /* Compile and save the given function, return C_OK on success and C_ERR on failure.
* In case on failure the err out param is set with relevant error message */ * In case on failure the err out param is set with relevant error message */
int functionsCreateWithFunctionCtx(sds function_name,sds engine_name, sds desc, sds code, int functionsCreateWithFunctionCtx(sds function_name,sds engine_name, sds desc, sds code,
int replace, sds* err, functionsCtx *functions) { int replace, sds* err, functionsCtx *functions) {
if (functionsVerifyName(function_name)) {
*err = sdsnew("Function names can only contain letters and numbers and must be at least one character long");
return C_ERR;
}
engineInfo *ei = dictFetchValue(engines, engine_name); engineInfo *ei = dictFetchValue(engines, engine_name);
if (!ei) { if (!ei) {
*err = sdsnew("Engine not found"); *err = sdsnew("Engine not found");
......
...@@ -11,6 +11,20 @@ start_server {tags {"scripting"}} { ...@@ -11,6 +11,20 @@ start_server {tags {"scripting"}} {
set _ $e set _ $e
} {*Function already exists*} } {*Function already exists*}
test {FUNCTION - Create an already exiting function raise error (case insensitive)} {
catch {
r function create LUA TEST {return 'hello1'}
} e
set _ $e
} {*Function already exists*}
test {FUNCTION - Create a function with wrong name format} {
catch {
r function create LUA {bad\0foramat} {return 'hello1'}
} e
set _ $e
} {*Function names can only contain letters and numbers*}
test {FUNCTION - Create function with unexisting engine} { test {FUNCTION - Create function with unexisting engine} {
catch { catch {
r function create bad_engine test {return 'hello1'} r function create bad_engine test {return 'hello1'}
...@@ -30,6 +44,10 @@ start_server {tags {"scripting"}} { ...@@ -30,6 +44,10 @@ start_server {tags {"scripting"}} {
r fcall test 0 r fcall test 0
} {hello1} } {hello1}
test {FUNCTION - test function case insensitive} {
r fcall TEST 0
} {hello1}
test {FUNCTION - test replace argument with function creation failure keeps old function} { test {FUNCTION - test replace argument with function creation failure keeps old function} {
catch {r function create LUA test REPLACE {error}} catch {r function create LUA test REPLACE {error}}
r fcall test 0 r fcall test 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment