MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / getBitOffsetFromArgument

Function getBitOffsetFromArgument

src/bitops.cpp:419–446  ·  view source on GitHub ↗

This helper function used by GETBIT / SETBIT parses the bit offset argument * making sure an error is returned if it is negative or if it overflows * Redis 512 MB limit for the string value or more (server.proto_max_bulk_len). * * If the 'hash' argument is true, and 'bits is positive, then the command * will also parse bit offsets prefixed by "#". In such a case the offset * is multiplied by

Source from the content-addressed store, hash-verified

417 * will also parse bit offsets prefixed by "#". In such a case the offset
418 * is multiplied by 'bits'. This is useful for the BITFIELD command. */
419int getBitOffsetFromArgument(client *c, robj *o, uint64_t *offset, int hash, int bits) {
420 long long loffset;
421 const char *err = "bit offset is not an integer or out of range";
422 char *p = szFromObj(o);
423 size_t plen = sdslen(p);
424 int usehash = 0;
425
426 /* Handle #<offset> form. */
427 if (p[0] == '#' && hash && bits > 0) usehash = 1;
428
429 if (string2ll(p+usehash,plen-usehash,&loffset) == 0) {
430 addReplyError(c,err);
431 return C_ERR;
432 }
433
434 /* Adjust the offset by 'bits' for #<offset> form. */
435 if (usehash) loffset *= bits;
436
437 /* Limit offset to server.proto_max_bulk_len (512MB in bytes by default) */
438 if ((loffset < 0) || (loffset >> 3) >= g_pserver->proto_max_bulk_len)
439 {
440 addReplyError(c,err);
441 return C_ERR;
442 }
443
444 *offset = loffset;
445 return C_OK;
446}
447
448/* This helper function for BITFIELD parses a bitfield type in the form
449 * <sign><bits> where sign is 'u' or 'i' for unsigned and signed, and

Callers 3

setbitCommandFunction · 0.85
getbitCommandFunction · 0.85
bitfieldGenericFunction · 0.85

Calls 4

szFromObjFunction · 0.85
sdslenFunction · 0.85
addReplyErrorFunction · 0.85
string2llFunction · 0.70

Tested by

no test coverage detected