This function implements CLIENT SETNAME, including replying to the * user with an error if the charset is wrong (in that case C_ERR is * returned). If the function succeeeded C_OK is returned, and it's up * to the caller to send a reply if needed. * * Setting an empty string as name has the effect of unsetting the * currently set name: the client will remain unnamed. * * This function is a
| 2975 | * |
| 2976 | * This function is also used to implement the HELLO SETNAME option. */ |
| 2977 | int clientSetNameOrReply(client *c, robj *name) { |
| 2978 | int len = sdslen((sds)ptrFromObj(name)); |
| 2979 | char *p = (char*)ptrFromObj(name); |
| 2980 | |
| 2981 | /* Setting the client name to an empty string actually removes |
| 2982 | * the current name. */ |
| 2983 | if (len == 0) { |
| 2984 | if (c->name) decrRefCount(c->name); |
| 2985 | c->name = NULL; |
| 2986 | return C_OK; |
| 2987 | } |
| 2988 | |
| 2989 | /* Otherwise check if the charset is ok. We need to do this otherwise |
| 2990 | * CLIENT LIST format will break. You should always be able to |
| 2991 | * split by space to get the different fields. */ |
| 2992 | for (int j = 0; j < len; j++) { |
| 2993 | if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */ |
| 2994 | addReplyError(c, |
| 2995 | "Client names cannot contain spaces, " |
| 2996 | "newlines or special characters."); |
| 2997 | return C_ERR; |
| 2998 | } |
| 2999 | } |
| 3000 | if (c->name) decrRefCount(c->name); |
| 3001 | c->name = name; |
| 3002 | incrRefCount(name); |
| 3003 | return C_OK; |
| 3004 | } |
| 3005 | |
| 3006 | /* Reset the client state to resemble a newly connected client. |
| 3007 | */ |
no test coverage detected