From 3ba2281f96098691d9d672ce8b484207379b09ee Mon Sep 17 00:00:00 2001 From: sundb Date: Wed, 2 Dec 2020 03:41:26 +0800 Subject: 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. --- src/object.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/object.c') 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"; -- cgit v1.2.1