Merge pull request #29117 from tchaikov/wip-mgr-set_health_checks-unicode

mgr: check for unicode passed to "set_health_checks()"

Reviewed-by: Neha Ojha <nojha@redhat.com>
This commit is contained in:
Kefu Chai 2019-07-25 23:03:00 +08:00 committed by GitHub
commit b3abfe4812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 8 deletions

View File

@ -284,24 +284,23 @@ ceph_set_health_checks(BaseMgrModule *self, PyObject *args)
}
string ks(k);
if (ks == "severity") {
if (!PyString_Check(v)) {
if (auto [vs, valid] = PyString_ToString(v); !valid) {
derr << __func__ << " check " << check_name
<< " severity value not string" << dendl;
continue;
}
string vs(PyString_AsString(v));
if (vs == "warning") {
} else if (vs == "warning") {
severity = HEALTH_WARN;
} else if (vs == "error") {
severity = HEALTH_ERR;
}
} else if (ks == "summary") {
if (!PyString_Check(v) && !PyUnicode_Check(v)) {
if (auto [vs, valid] = PyString_ToString(v); !valid) {
derr << __func__ << " check " << check_name
<< " summary value not [unicode] string" << dendl;
continue;
} else {
summary = std::move(vs);
}
summary = PyString_AsString(v);
} else if (ks == "detail") {
if (!PyList_Check(v)) {
derr << __func__ << " check " << check_name
@ -310,12 +309,13 @@ ceph_set_health_checks(BaseMgrModule *self, PyObject *args)
}
for (int k = 0; k < PyList_Size(v); ++k) {
PyObject *di = PyList_GET_ITEM(v, k);
if (!PyString_Check(di) && !PyUnicode_Check(di)) {
if (auto [vs, valid] = PyString_ToString(di); !valid) {
derr << __func__ << " check " << check_name
<< " detail item " << k << " not a [unicode] string" << dendl;
continue;
} else {
detail.push_back(std::move(vs));
}
detail.push_back(PyString_AsString(di));
}
} else {
derr << __func__ << " check " << check_name

View File

@ -21,6 +21,9 @@
#include "PythonCompat.h"
#include <stack>
#include <string>
#include <string_view>
#include <sstream>
#include <memory>
#include <list>

View File

@ -4,6 +4,7 @@
#pragma once
#include <Python.h>
#include <string>
// Python's pyconfig-64.h conflicts with ceph's acconfig.h
#undef HAVE_SYS_WAIT_H
@ -36,3 +37,19 @@ inline PyObject* PyInt_FromString(const char *str, char **pend, int base) {
}
#define PyString_Type PyUnicode_Type
#endif
inline std::pair<std::string, bool> PyString_ToString(PyObject *o) {
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(o)) {
return {PyUnicode_AsUTF8(o), true};
} else {
return {{}, false};
}
#else
if (PyString_Check(o) || PyUnicode_Check(o)) {
return {PyString_AsString(o), true};
} else {
return {{}, false};
}
#endif
}