Mercurial > vim
changeset 3792:73557eda7027 v7.3.654
updated for version 7.3.654
Problem: When creating a Vim dictionary from Python objects an empty key
might be used.
Solution: Do not use empty keys, throw an IndexError. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 05 Sep 2012 18:45:28 +0200 |
parents | e70503274f3a |
children | 8a78255c68d7 |
files | src/if_py_both.h src/version.c |
diffstat | 2 files changed, 24 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/src/if_py_both.h +++ b/src/if_py_both.h @@ -607,6 +607,14 @@ pyll_add(PyObject *self, pylinkedlist_T static PyTypeObject DictionaryType; +#define DICTKEY_GET_NOTEMPTY(err) \ + DICTKEY_GET(err) \ + if (*key == NUL) \ + { \ + PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \ + return err; \ + } + typedef struct { PyObject_HEAD @@ -659,7 +667,7 @@ pydict_to_tv(PyObject *obj, typval_T *tv if (valObject == NULL) return -1; - DICTKEY_GET(-1) + DICTKEY_GET_NOTEMPTY(-1) di = dictitem_alloc(key); @@ -730,7 +738,7 @@ pymap_to_tv(PyObject *obj, typval_T *tv, return -1; } - DICTKEY_GET(-1) + DICTKEY_GET_NOTEMPTY(-1) valObject = PyTuple_GetItem(litem, 1); if (valObject == NULL) @@ -784,16 +792,22 @@ DictionaryLength(PyObject *self) DictionaryItem(PyObject *self, PyObject *keyObject) { char_u *key; - dictitem_T *val; + dictitem_T *di; DICTKEY_DECL - DICTKEY_GET(NULL) - - val = dict_find(((DictionaryObject *) (self))->dict, key, -1); + DICTKEY_GET_NOTEMPTY(NULL) + + di = dict_find(((DictionaryObject *) (self))->dict, key, -1); + + if (di == NULL) + { + PyErr_SetString(PyExc_IndexError, _("no such key in dictionary")); + return NULL; + } DICTKEY_UNREF - return ConvertToPyObject(&val->di_tv); + return ConvertToPyObject(&di->di_tv); } static PyInt @@ -811,7 +825,7 @@ DictionaryAssItem(PyObject *self, PyObje return -1; } - DICTKEY_GET(-1) + DICTKEY_GET_NOTEMPTY(-1) di = dict_find(d, key, -1);