Skip to content

Commit

Permalink
Don't do inplace replacement of values for incr/decr
Browse files Browse the repository at this point in the history
All other places in the code we assume "copy on write", so this breaks the
contract for the object. Other threads may use the internal buffer for
sending at the same time.
  • Loading branch information
trondn committed Mar 29, 2010
1 parent 6a01c85 commit 8e59e16
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions items.c
Expand Up @@ -699,32 +699,21 @@ static ENGINE_ERROR_CODE do_add_delta(struct default_engine *engine,

*result = value;
char buf[80];
snprintf(buf, sizeof(buf), "%"PRIu64, value);
res = strlen(buf);
if (res + 2 > it->item.nbytes) { /* need to realloc */
hash_item *new_it;
new_it = do_item_alloc(engine, item_get_key(&it->item),
it->item.nkey, it->item.flags,
it->item.exptime, res + 2,
cookie );
if (new_it == 0) {
return ENGINE_ENOMEM;
}
memcpy(item_get_data(&new_it->item), buf, res);
memcpy(item_get_data(&new_it->item) + res, "\r\n", 2);
do_item_replace(engine, it, new_it);
*rcas = item_get_cas(&new_it->item);
do_item_release(engine, new_it); /* release our reference */
} else { /* replace in-place */
/* When changing the value without replacing the item, we
need to update the CAS on the existing item. */
item_set_cas(&it->item, get_cas_id());
*rcas = item_get_cas(&it->item);

memcpy(item_get_data(&it->item), buf, res);
memset(item_get_data(&it->item) + res, ' ',
it->item.nbytes - res - 2);
if ((res = snprintf(buf, sizeof(buf), "%" PRIu64 "\r\n", value)) == -1) {
return ENGINE_EINVAL;
}
hash_item *new_it = do_item_alloc(engine, item_get_key(&it->item),
it->item.nkey, it->item.flags,
it->item.exptime, res,
cookie );
if (new_it == 0) {
do_item_unlink(engine, it);
return ENGINE_ENOMEM;
}
memcpy(item_get_data(&new_it->item), buf, res);
do_item_replace(engine, it, new_it);
*rcas = item_get_cas(&new_it->item);
do_item_release(engine, new_it); /* release our reference */

return ENGINE_SUCCESS;
}
Expand Down

0 comments on commit 8e59e16

Please sign in to comment.