antirez/redis — pull request #1640 by mattsta (2014-03-29 03:34:23+00:00)
Title: redis-cli: Re-attach selected DB after auth

Previously, if you started redis-cli against a server
requiring auth and requested a numbered DB,
redis-cli would SELECT the requested DB,
the SELECT would fail due to no auth (if not provided on
the command line), then after authentication,
all commands would run against DB 0 because the original
SELECT failed and it never gets retried (even though
redis-cli was showing the user their previously 'selected'
DB number).

There are a few places this *could* be fixed:
- Allow Redis to accept SELECT before AUTH, but that requires a server change.
- Add better error checking to SELECT in redis-cli so it fails or returns
errors if it gets an AUTH rejection.
- There are a lot of places where the return values of
cliConnect() aren't checked, so it would require adding error
checking more places.
- Re-request the SELECT after a successful authentication.
- Plus, a little bit of the second one above:
add better error checking to cliSelect(), even though the
result isn't acted on anywhere.

I picked the third option because it required the least amount
of code change to get the proper behavior. The comment
in redis-cli says the REPL doesn't abort on connection or setup
errors, so killing redis-cli on non-auth'd-select at startup
would change too much existing behavior.

Note: running into this situation is a (hopefully) very limited
used case of people using multiple DBs and AUTH all at the same
time.

Fixes antirez#1639