Skip to content

Commit

Permalink
eval: Use better error messages when failing to dump values
Browse files Browse the repository at this point in the history
Examples:

    let g:SR = [[]]
    call add(g:SR[0], g:SR)
    wshada
    " E952: Unable to dump variable g:SR: container references itself in index 0, index 0

    let g:F = {'_TYPE': v:msgpack_types.map, '_VAL': [[{'abc': 1}, function("tr")]]}
    wshada
    " E951: Error while dumping variable g:F, key {'abc': 1} at index 0 from special map, key '': attempt to dump function reference
    " (no msgpack#string available)
    " E951: Error while dumping variable g:F, key {="abc": 1} at index 0 from special map, key '': attempt to dump function reference
    " (msgpack#string available)

    let g:F = {'_TYPE': v:msgpack_types.map, '_VAL': [[g:SR, function("tr")]]}
    wshada
    " E951: Error while dumping variable g:F, key [[[[{E724@0}]]]] at index 0 from special map, index 1: attempt to dump function reference

    call msgpackdump([g:SR])
    " E952: Unable to dump msgpackdump() argument, index 0: container references itself in index 0, index 0

Not tested yet.
  • Loading branch information
ZyX-I committed Jan 4, 2016
1 parent 59eaba2 commit d26b01d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 32 deletions.
137 changes: 119 additions & 18 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -6479,7 +6479,8 @@ static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
static int name##_convert_one_value(firstargtype firstargname, \
MPConvStack *const mpstack, \
typval_T *const tv, \
const int copyID) \
const int copyID, \
const char *const objname) \
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT \
{ \
switch (tv->v_type) { \
Expand Down Expand Up @@ -6713,14 +6714,16 @@ name##_convert_one_value_regular_dict: \
return OK; \
} \
\
scope int vim_to_##name(firstargtype firstargname, typval_T *const tv) \
scope int vim_to_##name(firstargtype firstargname, typval_T *const tv, \
const char *const objname) \
FUNC_ATTR_WARN_UNUSED_RESULT \
{ \
current_copyID += COPYID_INC; \
const int copyID = current_copyID; \
MPConvStack mpstack; \
kv_init(mpstack); \
if (name##_convert_one_value(firstargname, &mpstack, tv, copyID) == FAIL) { \
if (name##_convert_one_value(firstargname, &mpstack, tv, copyID, objname) \
== FAIL) { \
goto vim_to_msgpack_error_ret; \
} \
while (kv_size(mpstack)) { \
Expand Down Expand Up @@ -6769,17 +6772,17 @@ scope int vim_to_##name(firstargtype firstargname, typval_T *const tv) \
} \
const list_T *const kv_pair = cur_mpsv->data.l.li->li_tv.vval.v_list; \
if (name##_convert_one_value(firstargname, &mpstack, \
&kv_pair->lv_first->li_tv, copyID) \
== FAIL) { \
&kv_pair->lv_first->li_tv, copyID, \
objname) == FAIL) { \
goto vim_to_msgpack_error_ret; \
} \
cur_tv = &kv_pair->lv_last->li_tv; \
cur_mpsv->data.l.li = cur_mpsv->data.l.li->li_next; \
break; \
} \
} \
if (name##_convert_one_value(firstargname, &mpstack, cur_tv, copyID) \
== FAIL) { \
if (name##_convert_one_value(firstargname, &mpstack, cur_tv, copyID, \
objname) == FAIL) { \
goto vim_to_msgpack_error_ret; \
} \
} \
Expand Down Expand Up @@ -6975,7 +6978,7 @@ static char *tv2string(typval_T *tv, size_t *len)
{
garray_T ga;
ga_init(&ga, (int)sizeof(char), 80);
vim_to_string(&ga, tv);
vim_to_string(&ga, tv, "tv2string() argument");
did_echo_string_emsg = false;
if (len != NULL) {
*len = (size_t) ga.ga_len;
Expand All @@ -7001,7 +7004,7 @@ static char *echo_string(typval_T *tv, size_t *len)
ga_concat(&ga, tv->vval.v_string);
}
} else {
vim_to_echo(&ga, tv);
vim_to_echo(&ga, tv, ":echo argument");
did_echo_string_emsg = false;
}
if (len != NULL) {
Expand Down Expand Up @@ -12686,6 +12689,100 @@ static inline bool vim_list_to_buf(const list_T *const list,
return true;
}

/// Show a error message when converting to msgpack value
///
/// @param[in] msg Error message to dump. Must contain exactly two %s that
/// will be replaced with what was being dumped: first with
/// something like “F” or “function argument”, second with path
/// to the failed value.
/// @param[in] mpstack Path to the failed value.
/// @param[in] objname Dumped object name.
///
/// @return FAIL.
static int conv_error(const char *const msg, const MPConvStack *const mpstack,
const char *const objname)
FUNC_ATTR_NONNULL_ALL
{
garray_T msg_ga;
ga_init(&msg_ga, (int)sizeof(char), 80);
char *const key_msg = _("key %s");
char *const key_pair_msg = _("key %s at index %i from special map");
char *const idx_msg = _("index %i");
for (size_t i = 0; i < kv_size(*mpstack); i++) {
if (i != 0) {
ga_concat(&msg_ga, (char_u *) ", ");
}
MPConvStackVal v = kv_A(*mpstack, i);
switch (v.type) {
case kMPConvDict: {
typval_T key_tv = {
.v_type = VAR_STRING,
.vval = { .v_string = (v.data.d.hi == NULL
? v.data.d.dict->dv_hashtab.ht_array
: (v.data.d.hi - 1))->hi_key },
};
char *const key = tv2string(&key_tv, NULL);
vim_snprintf((char *) IObuff, IOSIZE, key_msg, key);
xfree(key);
ga_concat(&msg_ga, IObuff);
break;
}
case kMPConvPairs:
case kMPConvList: {
int idx = 0;
const listitem_T *li;
for (li = v.data.l.list->lv_first;
li != NULL && li->li_next != v.data.l.li;
li = li->li_next) {
idx++;
}
if (v.type == kMPConvList
|| li == NULL
|| (li->li_tv.v_type != VAR_LIST
&& li->li_tv.vval.v_list->lv_len <= 0)) {
vim_snprintf((char *) IObuff, IOSIZE, idx_msg, idx);
ga_concat(&msg_ga, IObuff);
} else {
typval_T key_tv = li->li_tv.vval.v_list->lv_first->li_tv;
trylevel++;
typval_T rettv;
int doesrange;
char *key;
bool free_key = false;
if (call_func((char_u *) "msgpack#string",
sizeof("msgpack#string") - 1,
&rettv, 1, &key_tv, 0L, 0L, &doesrange, true,
NULL) == FAIL
|| ((key = (char *) get_tv_string(&rettv)) == NULL)
|| did_throw
|| (msg_list != NULL && *msg_list != NULL)) {
key = tv2string(&key_tv, NULL);
free_key = true;
}
did_emsg = false;
discard_current_exception();
if (msg_list != NULL && *msg_list != NULL) {
free_global_msglist();
}
trylevel--;
vim_snprintf((char *) IObuff, IOSIZE, key_pair_msg, key, idx);
clear_tv(&rettv);
if (free_key) {
xfree(key);
}
ga_concat(&msg_ga, IObuff);
}
break;
}
}
}
EMSG3(msg, objname, (kv_size(*mpstack) == 0
? _("itself")
: (char *) msg_ga.ga_data));
ga_clear(&msg_ga);
return FAIL;
}

#define CONV_STRING(buf, len) \
do { \
if (buf == NULL) { \
Expand Down Expand Up @@ -12726,10 +12823,9 @@ static inline bool vim_list_to_buf(const list_T *const list,
msgpack_pack_double(packer, (double) (flt))

#define CONV_FUNC(fun) \
do { \
EMSG2(_(e_invarg2), "attempt to dump function reference"); \
return FAIL; \
} while (0)
return conv_error(_("E951: Error while dumping %s, %s: " \
"attempt to dump function reference"), \
mpstack, objname)

#define CONV_EMPTY_LIST() \
msgpack_pack_array(packer, 0)
Expand Down Expand Up @@ -12772,10 +12868,9 @@ static inline bool vim_list_to_buf(const list_T *const list,
#define CONV_LIST_BETWEEN_ITEMS(lst)

#define CONV_RECURSE(val, conv_type) \
do { \
EMSG2(_(e_invarg2), "container references itself"); \
return FAIL; \
} while (0)
return conv_error(_("E952: Unable to dump %s: " \
"container references itself in %s"), \
mpstack, objname)

#define CONV_ALLOW_SPECIAL true

Expand Down Expand Up @@ -12817,8 +12912,14 @@ static void f_msgpackdump(typval_T *argvars, typval_T *rettv)
return;
}
msgpack_packer *lpacker = msgpack_packer_new(ret_list, &msgpack_list_write);
const char *const msg = _("msgpackdump() argument, index %i");
// Assume that translation will not take more then 4 times more space
char msgbuf[sizeof("msgpackdump() argument, index ") * 4 + NUMBUFLEN];
int idx = 0;
for (listitem_T *li = list->lv_first; li != NULL; li = li->li_next) {
if (vim_to_msgpack(lpacker, &li->li_tv) == FAIL) {
vim_snprintf(msgbuf, sizeof(msgbuf), (char *) msg, idx);
idx++;
if (vim_to_msgpack(lpacker, &li->li_tv, msgbuf) == FAIL) {
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/nvim/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ enum {
/// Maximum number of function arguments
#define MAX_FUNC_ARGS 20

int vim_to_msgpack(msgpack_packer *const, typval_T *const);
int vim_to_msgpack(msgpack_packer *const, typval_T *const,
const char *const objname);

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "eval.h.generated.h"
Expand Down
39 changes: 26 additions & 13 deletions src/nvim/shada.c
Original file line number Diff line number Diff line change
Expand Up @@ -1684,17 +1684,18 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf);
msgpack_packer *spacker = msgpack_packer_new(&sbuf, &msgpack_sbuffer_write);
#define DUMP_ADDITIONAL_ELEMENTS(src) \
#define DUMP_ADDITIONAL_ELEMENTS(src, what) \
do { \
if ((src) != NULL) { \
for (listitem_T *li = (src)->lv_first; li != NULL; li = li->li_next) { \
if (vim_to_msgpack(spacker, &li->li_tv) == FAIL) { \
if (vim_to_msgpack(spacker, &li->li_tv, \
_("additional elements of ShaDa " what)) == FAIL) { \
goto shada_pack_entry_error; \
} \
} \
} \
} while (0)
#define DUMP_ADDITIONAL_DATA(src) \
#define DUMP_ADDITIONAL_DATA(src, what) \
do { \
dict_T *const d = (src); \
if (d != NULL) { \
Expand All @@ -1706,7 +1707,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
const size_t key_len = strlen((const char *) hi->hi_key); \
msgpack_pack_str(spacker, key_len); \
msgpack_pack_str_body(spacker, (const char *) hi->hi_key, key_len); \
if (vim_to_msgpack(spacker, &di->di_tv) == FAIL) { \
if (vim_to_msgpack(spacker, &di->di_tv, \
_("additional data of ShaDa " what)) == FAIL) { \
goto shada_pack_entry_error; \
} \
} \
Expand Down Expand Up @@ -1741,7 +1743,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
if (is_hist_search) {
msgpack_pack_uint8(spacker, (uint8_t) entry.data.history_item.sep);
}
DUMP_ADDITIONAL_ELEMENTS(entry.data.history_item.additional_elements);
DUMP_ADDITIONAL_ELEMENTS(entry.data.history_item.additional_elements,
"history entry item");
break;
}
case kSDItemVariable: {
Expand All @@ -1750,14 +1753,20 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
? 0
: entry.data.global_var.additional_elements->lv_len);
msgpack_pack_array(spacker, arr_size);
PACK_BIN(cstr_as_string(entry.data.global_var.name));
if (vim_to_msgpack(spacker, &entry.data.global_var.value) == FAIL) {
const String varname = cstr_as_string(entry.data.global_var.name);
PACK_BIN(varname);
char vardesc[256] = "variable g:";
memcpy(&vardesc[sizeof("variable g:") - 1], varname.data,
varname.size + 1);
if (vim_to_msgpack(spacker, &entry.data.global_var.value, vardesc)
== FAIL) {
ret = kSDWriteIgnError;
EMSG2(_(WERR "Failed to write variable %s"),
entry.data.global_var.name);
goto shada_pack_entry_error;
}
DUMP_ADDITIONAL_ELEMENTS(entry.data.global_var.additional_elements);
DUMP_ADDITIONAL_ELEMENTS(entry.data.global_var.additional_elements,
"variable item");
break;
}
case kSDItemSubString: {
Expand All @@ -1767,7 +1776,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
: entry.data.sub_string.additional_elements->lv_len);
msgpack_pack_array(spacker, arr_size);
PACK_BIN(cstr_as_string(entry.data.sub_string.sub));
DUMP_ADDITIONAL_ELEMENTS(entry.data.sub_string.additional_elements);
DUMP_ADDITIONAL_ELEMENTS(entry.data.sub_string.additional_elements,
"sub string item");
break;
}
case kSDItemSearchPattern: {
Expand Down Expand Up @@ -1814,7 +1824,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
msgpack_pack_int64(spacker, entry.data.search_pattern.offset);
}
#undef PACK_BOOL
DUMP_ADDITIONAL_DATA(entry.data.search_pattern.additional_data);
DUMP_ADDITIONAL_DATA(entry.data.search_pattern.additional_data,
"search pattern item");
break;
}
case kSDItemChange:
Expand Down Expand Up @@ -1849,7 +1860,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
PACK_STATIC_STR(KEY_NAME_CHAR);
msgpack_pack_uint8(spacker, (uint8_t) entry.data.filemark.name);
}
DUMP_ADDITIONAL_DATA(entry.data.filemark.additional_data);
DUMP_ADDITIONAL_DATA(entry.data.filemark.additional_data,
"mark (change, jump, global or local) item");
break;
}
case kSDItemRegister: {
Expand Down Expand Up @@ -1877,7 +1889,7 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
PACK_STATIC_STR(REG_KEY_WIDTH);
msgpack_pack_uint64(spacker, (uint64_t) entry.data.reg.width);
}
DUMP_ADDITIONAL_DATA(entry.data.reg.additional_data);
DUMP_ADDITIONAL_DATA(entry.data.reg.additional_data, "register item");
break;
}
case kSDItemBufferList: {
Expand Down Expand Up @@ -1908,7 +1920,8 @@ static ShaDaWriteResult shada_pack_entry(msgpack_packer *const packer,
msgpack_pack_uint64(
spacker, (uint64_t) entry.data.buffer_list.buffers[i].pos.col);
}
DUMP_ADDITIONAL_DATA(entry.data.buffer_list.buffers[i].additional_data);
DUMP_ADDITIONAL_DATA(entry.data.buffer_list.buffers[i].additional_data,
"buffer list subitem");
}
break;
}
Expand Down

0 comments on commit d26b01d

Please sign in to comment.