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
| 2394 | * |
| 2395 | * This function is also used to implement the HELLO SETNAME option. */ |
| 2396 | int clientSetNameOrReply(client *c, robj *name) { |
| 2397 | int len = sdslen(name->ptr); |
| 2398 | char *p = name->ptr; |
| 2399 | |
| 2400 | /* Setting the client name to an empty string actually removes |
| 2401 | * the current name. */ |
| 2402 | if (len == 0) { |
| 2403 | if (c->name) decrRefCount(c->name); |
| 2404 | c->name = NULL; |
| 2405 | return C_OK; |
| 2406 | } |
| 2407 | |
| 2408 | /* Otherwise check if the charset is ok. We need to do this otherwise |
| 2409 | * CLIENT LIST format will break. You should always be able to |
| 2410 | * split by space to get the different fields. */ |
| 2411 | for (int j = 0; j < len; j++) { |
| 2412 | if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */ |
| 2413 | addReplyError(c, |
| 2414 | "Client names cannot contain spaces, " |
| 2415 | "newlines or special characters."); |
| 2416 | return C_ERR; |
| 2417 | } |
| 2418 | } |
| 2419 | if (c->name) decrRefCount(c->name); |
| 2420 | c->name = name; |
| 2421 | incrRefCount(name); |
| 2422 | return C_OK; |
| 2423 | } |
| 2424 | |
| 2425 | /* Reset the client state to resemble a newly connected client. |
| 2426 | */ |
no test coverage detected