Commit 62300c69 authored by Marius Wachtler's avatar Marius Wachtler

interpreter+bjit: fix leak when encountering a dict or set node with duplicate keys

parent 30d2e36c
...@@ -1574,8 +1574,13 @@ Value ASTInterpreter::visit_dict(AST_Dict* node) { ...@@ -1574,8 +1574,13 @@ Value ASTInterpreter::visit_dict(AST_Dict* node) {
BoxedDict* dict = new BoxedDict(); BoxedDict* dict = new BoxedDict();
for (size_t i = 0; i < node->keys.size(); ++i) { for (size_t i = 0; i < node->keys.size(); ++i) {
Value v = visit_expr(node->values[i]); Value v = visit_expr(node->values[i]);
AUTO_DECREF(v.o);
Value k = visit_expr(node->keys[i]); Value k = visit_expr(node->keys[i]);
dict->d[k.o] = v.o; AUTO_DECREF(k.o);
int ret = PyDict_SetItem(dict, k.o, v.o);
if (ret == -1)
throwCAPIException();
values.push_back(v); values.push_back(v);
keys.push_back(k); keys.push_back(k);
...@@ -1590,7 +1595,11 @@ Value ASTInterpreter::visit_set(AST_Set* node) { ...@@ -1590,7 +1595,11 @@ Value ASTInterpreter::visit_set(AST_Set* node) {
BoxedSet::Set set; BoxedSet::Set set;
for (AST_expr* e : node->elts) { for (AST_expr* e : node->elts) {
Value v = visit_expr(e); Value v = visit_expr(e);
set.insert(v.o); auto&& p = set.insert(v.o);
if (!p.second /* already exists */) {
Py_DECREF(p.first->value);
*p.first = v.o;
}
items.push_back(v); items.push_back(v);
} }
......
...@@ -854,7 +854,9 @@ Box* JitFragmentWriter::callattrHelper(Box* obj, BoxedString* attr, CallattrFlag ...@@ -854,7 +854,9 @@ Box* JitFragmentWriter::callattrHelper(Box* obj, BoxedString* attr, CallattrFlag
Box* JitFragmentWriter::createDictHelper(uint64_t num, Box** keys, Box** values) { Box* JitFragmentWriter::createDictHelper(uint64_t num, Box** keys, Box** values) {
BoxedDict* dict = (BoxedDict*)createDict(); BoxedDict* dict = (BoxedDict*)createDict();
for (uint64_t i = 0; i < num; ++i) { for (uint64_t i = 0; i < num; ++i) {
dict->d[keys[i]] = values[i]; int ret = PyDict_SetItem(dict, autoDecref(keys[i]), autoDecref(values[i]));
if (ret == -1)
throwCAPIException();
} }
return dict; return dict;
} }
...@@ -870,8 +872,13 @@ Box* JitFragmentWriter::createListHelper(uint64_t num, Box** data) { ...@@ -870,8 +872,13 @@ Box* JitFragmentWriter::createListHelper(uint64_t num, Box** data) {
Box* JitFragmentWriter::createSetHelper(uint64_t num, Box** data) { Box* JitFragmentWriter::createSetHelper(uint64_t num, Box** data) {
BoxedSet* set = (BoxedSet*)createSet(); BoxedSet* set = (BoxedSet*)createSet();
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i) {
set->s.insert(data[i]); auto&& p = set->s.insert(data[i]);
if (!p.second /* already exists */) {
Py_DECREF(p.first->value);
*p.first = data[i];
}
}
return set; return set;
} }
......
d = {2:2} d = {2:"should get overwritten", 2:2}
d[1] = 1 d[1] = 1
print d print d
print d[1], d[1L], d[1.0], d[True] print d[1], d[1L], d[1.0], d[True]
......
s1 = {1} s1 = {1, 1}
def sorted(s): def sorted(s):
l = list(s) l = list(s)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment