Python module Doc_setattr: miscplaced DECREF caused crashes

This commit is contained in:
Jean-Francois Dockes 2016-06-01 09:39:24 +02:00
parent e711f1e1a9
commit 1796ba1a2e

View File

@ -609,7 +609,6 @@ Doc_getattro(recoll_DocObject *self, PyObject *nameobj)
static int static int
Doc_setattr(recoll_DocObject *self, char *name, PyObject *value) Doc_setattr(recoll_DocObject *self, char *name, PyObject *value)
{ {
LOGDEB0(("Doc_setattr: doc %p\n", self->doc));
if (self->doc == 0 || the_docs.find(self->doc) == the_docs.end()) { if (self->doc == 0 || the_docs.find(self->doc) == the_docs.end()) {
PyErr_SetString(PyExc_AttributeError, "doc??"); PyErr_SetString(PyExc_AttributeError, "doc??");
return -1; return -1;
@ -619,21 +618,19 @@ Doc_setattr(recoll_DocObject *self, char *name, PyObject *value)
"Configuration not initialized"); "Configuration not initialized");
return -1; return -1;
} }
if (name == 0) {
PyErr_SetString(PyExc_AttributeError, "name??");
return -1;
}
#if PY_MAJOR_VERSION < 3 if (PyBytes_Check(value)) {
if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, "UTF-8", "strict");
value = PyUnicode_FromObject(value);
if (value == 0) if (value == 0)
return -1; return -1;
} }
#endif
if (!PyUnicode_Check(value)) { if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_AttributeError, "value not str/unicode??"); PyErr_SetString(PyExc_AttributeError, "value not unicode??");
return -1;
}
if (name == 0) {
PyErr_SetString(PyExc_AttributeError, "name??");
return -1; return -1;
} }
@ -644,10 +641,11 @@ Doc_setattr(recoll_DocObject *self, char *name, PyObject *value)
return -1; return -1;
} }
char* uvalue = PyBytes_AsString(putf8); char* uvalue = PyBytes_AsString(putf8);
Py_DECREF(putf8);
string key = rclconfig->fieldQCanon(string(name)); string key = rclconfig->fieldQCanon(string(name));
LOGDEB0(("Doc_setattr: [%s] (%s) -> [%s]\n", key.c_str(), name, uvalue)); LOGDEB0(("Doc_setattr: doc %p [%s] (%s) -> [%s]\n",
self->doc, key.c_str(), name, uvalue));
// We set the value in the meta array in all cases. Good idea ? or do it // We set the value in the meta array in all cases. Good idea ? or do it
// only for fields without a dedicated Doc:: entry? // only for fields without a dedicated Doc:: entry?
self->doc->meta[key] = uvalue; self->doc->meta[key] = uvalue;
@ -701,6 +699,7 @@ Doc_setattr(recoll_DocObject *self, char *name, PyObject *value)
} }
break; break;
} }
Py_DECREF(putf8);
return 0; return 0;
} }