Skip to content

Commit

Permalink
upstream mcmc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dormando committed Aug 6, 2024
1 parent 3214ab5 commit dc40ae7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 44 deletions.
2 changes: 2 additions & 0 deletions t/proxyins.t
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ sub test_mgreq {
$t->c_recv("SERVER_ERROR str: nomap\r\n");
$t->clear();
};

# test sepkey pos 1 and 3
}

sub test_mgres {
Expand Down
55 changes: 13 additions & 42 deletions vendor/mcmc/mcmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>

// TODO: move these structs into mcmc.h, but only expose them if
// MCMC_EXPOSE_INTERNALS is defined... for tests and this thing.
Expand Down Expand Up @@ -44,7 +45,7 @@ typedef struct mcmc_ctx {
// INTERNAL FUNCTIONS

// FIXME: USHRT_MAX?
#define TOKENIZER_MAXLEN (1<<16)-1
#define TOKENIZER_MAXLEN USHRT_MAX

// Find the starting offsets of each token; ignoring length.
// This creates a fast small (<= cacheline) index into the request,
Expand All @@ -56,7 +57,6 @@ typedef struct mcmc_ctx {
// Function _assumes_ const char *line ends with \n or \r\n, but will not
// break so long as the passed in 'len' is reasonable.
MCMC_STATIC int _mcmc_tokenize_meta(mcmc_tokenizer_t *t, const char *line, size_t len, const int mstart, const int max) {
// FIXME: detect \r\n and reduce len
const char *s = line;

// since multigets can be huge, we can't purely judge reqlen against this
Expand All @@ -80,9 +80,12 @@ MCMC_STATIC int _mcmc_tokenize_meta(mcmc_tokenizer_t *t, const char *line, size_
case 0:
// scanning for first non-space to find a token.
if (*s != ' ') {
// FIXME: blow out if flag is out of range
if (curtoken >= mstart && *s > 64 && *s < 123) {
t->metaflags |= (uint64_t)1 << (*s - 65);
if (curtoken >= mstart) {
if (*s > 64 && *s < 123) {
t->metaflags |= (uint64_t)1 << (*s - 65);
} else {
return MCMC_NOK;
}
}
t->tokens[curtoken] = s - line;
if (++curtoken == max) {
Expand Down Expand Up @@ -120,7 +123,7 @@ MCMC_STATIC int _mcmc_tokenize_meta(mcmc_tokenizer_t *t, const char *line, size_
t->ntokens = curtoken;
t->mstart = mstart;

return 0;
return MCMC_OK;
}

__attribute__((unused)) MCMC_STATIC int _mcmc_token_len(const char *line, mcmc_tokenizer_t *t, size_t token) {
Expand Down Expand Up @@ -613,17 +616,6 @@ const char *mcmc_token_get(const char *l, mcmc_tokenizer_t *t, int idx, int *len
}
}

// FIXME: delete once tested.
/*int mcmc_token_get_u32(const char *l, mcmc_tokenizer_t *t, int idx, uint32_t *val) {
if (idx > 0 && idx < t->ntokens) {
int tlen = 0;
const char *tok = _mcmc_token(l, t, idx, &tlen);
return mcmc_toktou32(tok, tlen, val);
} else {
return MCMC_ERR;
}
}*/

#define X(n, p, c) \
int n(const char *l, mcmc_tokenizer_t *t, int idx, p *val) { \
if (idx > 0 && idx < t->ntokens) { \
Expand All @@ -642,15 +634,6 @@ X(mcmc_token_get_64, int64_t, mcmc_tokto64)

#undef X

// FIXME: macro and/or inline definition?
int mcmc_token_has_flag_bit(mcmc_tokenizer_t *t, uint64_t flag) {
if (t->metaflags & flag) {
return MCMC_OK;
} else {
return MCMC_NOK;
}
}

int mcmc_token_has_flag(const char *l, mcmc_tokenizer_t *t, char flag) {
flag -= 65;
if (flag < 0 || flag > 57) {
Expand All @@ -664,13 +647,11 @@ int mcmc_token_has_flag(const char *l, mcmc_tokenizer_t *t, char flag) {
}
}

// User can optionally call has_flag or has_flag_bit before calling these
// functions. Is optional because it can be either wasteful to check if the
// flag will always be there, or it's useful to the caller to know the flag
// exists but there wasn't a token attached or we failed to parse it.
const char *mcmc_token_get_flag(const char *l, mcmc_tokenizer_t *t, char flag, int *len) {
// ensure the flag exists before we search the string.
// FIXME: remove this check. let the user pick has flag or has flag bit
// then call this.
if (mcmc_token_has_flag(l, t, flag) != MCMC_OK) {
return NULL;
}
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
Expand All @@ -690,12 +671,8 @@ const char *mcmc_token_get_flag(const char *l, mcmc_tokenizer_t *t, char flag, i
return NULL;
}

// FIXME: need to map out return codes and drive more consistency.
// TODO: once tested, turn to macro for u64/32/64
int mcmc_token_get_flag_u32(const char *l, mcmc_tokenizer_t *t, char flag, uint32_t *val) {
if (mcmc_token_has_flag(l, t, flag) != MCMC_OK) {
return MCMC_NOK;
}
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
Expand All @@ -708,9 +685,6 @@ int mcmc_token_get_flag_u32(const char *l, mcmc_tokenizer_t *t, char flag, uint3
}

int mcmc_token_get_flag_64(const char *l, mcmc_tokenizer_t *t, char flag, int64_t *val) {
if (mcmc_token_has_flag(l, t, flag) != MCMC_OK) {
return MCMC_NOK;
}
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
Expand All @@ -723,9 +697,6 @@ int mcmc_token_get_flag_64(const char *l, mcmc_tokenizer_t *t, char flag, int64_
}

int mcmc_token_get_flag_idx(const char *l, mcmc_tokenizer_t *t, char flag) {
if (mcmc_token_has_flag(l, t, flag) != MCMC_OK) {
return -1;
}
for (int x = t->mstart; x < t->ntokens; x++) {
const char *tflag = l + t->tokens[x];
if (tflag[0] == flag) {
Expand Down
4 changes: 2 additions & 2 deletions vendor/mcmc/mcmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#endif

#define MCMC_OK 0
#define MCMC_NOK 1
#define MCMC_NOK -2
#define MCMC_ERR -1
#define MCMC_NOT_CONNECTED 1
#define MCMC_CONNECTED 2
Expand Down Expand Up @@ -145,7 +145,7 @@ int mcmc_token_get_u64(const char *l, mcmc_tokenizer_t *t, int idx, uint64_t *va
int mcmc_token_get_32(const char *l, mcmc_tokenizer_t *t, int idx, int32_t *val);
int mcmc_token_get_64(const char *l, mcmc_tokenizer_t *t, int idx, int64_t *val);
int mcmc_token_has_flag(const char *l, mcmc_tokenizer_t *t, char flag);
int mcmc_token_has_flag_bit(mcmc_tokenizer_t *t, uint64_t flag);
#define mcmc_token_has_flag_bit(t, f) (((t)->metaflags & f) ? MCMC_OK : MCMC_NOK)
const char *mcmc_token_get_flag(const char *l, mcmc_tokenizer_t *t, char flag, int *len);
int mcmc_token_get_flag_u32(const char *l, mcmc_tokenizer_t *t, char flag, uint32_t *val);
int mcmc_token_get_flag_u64(const char *l, mcmc_tokenizer_t *t, char flag, uint64_t *val);
Expand Down

0 comments on commit dc40ae7

Please sign in to comment.