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

Function getBitfieldTypeFromArgument

src/bitops.cpp:455–479  ·  view source on GitHub ↗

This helper function for BITFIELD parses a bitfield type in the form * where sign is 'u' or 'i' for unsigned and signed, and * the bits is a value between 1 and 64. However 64 bits unsigned integers * are reported as an error because of current limitations of Redis protocol * to return unsigned integer values greater than INT64_MAX. * * On error C_ERR is returned and an error is

Source from the content-addressed store, hash-verified

453 *
454 * On error C_ERR is returned and an error is sent to the client. */
455int getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {
456 char *p = szFromObj(o);
457 const char *err = "Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.";
458 long long llbits;
459
460 if (p[0] == 'i') {
461 *sign = 1;
462 } else if (p[0] == 'u') {
463 *sign = 0;
464 } else {
465 addReplyError(c,err);
466 return C_ERR;
467 }
468
469 if ((string2ll(p+1,strlen(p+1),&llbits)) == 0 ||
470 llbits < 1 ||
471 (*sign == 1 && llbits > 64) ||
472 (*sign == 0 && llbits > 63))
473 {
474 addReplyError(c,err);
475 return C_ERR;
476 }
477 *bits = llbits;
478 return C_OK;
479}
480
481/* This is an helper function for commands implementations that need to write
482 * bits to a string object. The command creates or pad with zeroes the string

Callers 1

bitfieldGenericFunction · 0.85

Calls 3

szFromObjFunction · 0.85
addReplyErrorFunction · 0.85
string2llFunction · 0.70

Tested by

no test coverage detected