summaryrefslogtreecommitdiff
path: root/src/object.c
diff options
context:
space:
mode:
authorsundb <sundbcn@gmail.com>2020-12-02 03:41:26 +0800
committerGitHub <noreply@github.com>2020-12-01 21:41:26 +0200
commit3ba2281f96098691d9d672ce8b484207379b09ee (patch)
treed8378708184710df5af943956d43bede62d36d76 /src/object.c
parentc1b1e8c329567e90ec0edcb8c176e9f58a7d7435 (diff)
downloadredis-3ba2281f96098691d9d672ce8b484207379b09ee.tar.gz
Improve dbid range check for SELECT, MOVE, COPY (#8085)
SELECT used to read the index into a `long` variable, and then pass it to a function that takes an `int`, possibly causing an overflow before the range check. Now all these commands use better and cleaner range check, and that also results in a slight change of the error response in case of an invalid database index. SELECT: in the past it would have returned either `-ERR invalid DB index` (if not a number), or `-ERR DB index is out of range` (if not between 1..16 or alike). now it'll return either `-ERR value is out of range` (if not a number), or `-ERR value is out of range, value must between -2147483648 and 2147483647` (if not in the range for an int), or `-ERR DB index is out of range` (if not between 0..16 or alike) MOVE: in the past it would only fail with `-ERR index out of range` no matter the reason. now return the same errors as the new ones for SELECT mentioned above. (i.e. unlike for SELECT even for a value like 17 we changed the error message) COPY: doesn't really matter how it behaved in the past (new command), new behavior is like the above two.
Diffstat (limited to 'src/object.c')
-rw-r--r--src/object.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/object.c b/src/object.c
index d35ba0eaf..71eceb6d6 100644
--- a/src/object.c
+++ b/src/object.c
@@ -747,6 +747,16 @@ int getPositiveLongFromObjectOrReply(client *c, robj *o, long *target, const cha
return getRangeLongFromObjectOrReply(c, o, 0, LONG_MAX, target, msg);
}
+int getIntFromObjectOrReply(client *c, robj *o, int *target, const char *msg) {
+ long value;
+
+ if (getRangeLongFromObjectOrReply(c, o, INT_MIN, INT_MAX, &value, msg) != C_OK)
+ return C_ERR;
+
+ *target = value;
+ return C_OK;
+}
+
char *strEncoding(int encoding) {
switch(encoding) {
case OBJ_ENCODING_RAW: return "raw";