summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTim Burke <tim.burke@gmail.com>2016-10-11 11:46:49 -0700
committerClay Gerrard <clay.gerrard@gmail.com>2016-11-29 11:57:59 -0800
commitcc6f4bba26ee2a1e2dc18e17131ff5b5fbe7d6d9 (patch)
tree4805926575ae7a3da0654317d32fd5f62c825a94 /src
parent9a2071131004968cc7e2ec1d17eda32ad12ce9f6 (diff)
downloadpyeclib-cc6f4bba26ee2a1e2dc18e17131ff5b5fbe7d6d9.tar.gz
Fix formatted get_metadata memory leak
Co-Authored-By: Kota Tsuyuzaki <tsuyuzaki.kota@lab.ntt.co.jp> Change-Id: I678e10008c3c5bc04640f7f19498334d94cb0cd6
Diffstat (limited to 'src')
-rw-r--r--src/c/pyeclib_c/pyeclib_c.c68
1 files changed, 13 insertions, 55 deletions
diff --git a/src/c/pyeclib_c/pyeclib_c.c b/src/c/pyeclib_c/pyeclib_c.c
index d6b546f..8e1a23c 100644
--- a/src/c/pyeclib_c/pyeclib_c.c
+++ b/src/c/pyeclib_c/pyeclib_c.c
@@ -1030,66 +1030,24 @@ hex_encode_string(char *buf, uint32_t buf_len)
static PyObject*
fragment_metadata_to_dict(fragment_metadata_t *fragment_metadata)
{
- PyObject* metadata_dict = NULL;
- metadata_dict = PyDict_New();
-
- if (metadata_dict == NULL) {
- pyeclib_c_seterr(-ENOMEM, "fragment_metadata_to_dict ERROR: ");
- return NULL;
- }
-
- if (PyDict_SetItemString(metadata_dict, "index",
- PyLong_FromLong((unsigned long)fragment_metadata->idx)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict index ERROR: ");
- return NULL;
- }
-
- if (PyDict_SetItemString(metadata_dict, "size",
- PyLong_FromLong(fragment_metadata->size)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict size ERROR: ");
- return NULL;
- }
-
- if (PyDict_SetItemString(metadata_dict, "orig_data_size",
- PyLong_FromLong(fragment_metadata->orig_data_size)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict orig_data_size ERROR: ");
- return NULL;
- }
-
- const char *chksum_type_str = chksum_type_to_str(fragment_metadata->chksum_type);
- if (PyDict_SetItemString(metadata_dict, "chksum_type",
- PyString_FromString(chksum_type_str)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict chksum_type ERROR: ");
- return NULL;
- }
-
+ const char *chksum_type_str = chksum_type_to_str(fragment_metadata->chksum_type);
char *encoded_chksum = hex_encode_string((char*)fragment_metadata->chksum,
chksum_length(fragment_metadata->chksum_type));
- if (PyDict_SetItemString(metadata_dict, "chksum",
- PyString_FromString(encoded_chksum)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict chksum ERROR: ");
- return NULL;
- }
-
- if (PyDict_SetItemString(metadata_dict, "chksum_mismatch",
- PyLong_FromLong((unsigned long)fragment_metadata->chksum_mismatch)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict chksum_mismatch ERROR: ");
- return NULL;
- }
-
const char *backend_id_str = backend_id_to_str(fragment_metadata->backend_id);
- if (PyDict_SetItemString(metadata_dict, "backend_id",
- PyString_FromString(backend_id_str)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict backend_id ERROR: ");
- return NULL;
- }
-
- if (PyDict_SetItemString(metadata_dict, "backend_version",
- PyLong_FromLong((unsigned long)fragment_metadata->backend_version)) < 0) {
- pyeclib_c_seterr(-EINVALIDPARAMS, "fragment_metadata_to_dict backend_version ERROR: ");
+ PyObject* metadata_dict = Py_BuildValue(
+ "{s:k, s:k, s:K, s:s, s:s, s:B, s:s, s:k}",
+ "index", fragment_metadata->idx,
+ "size", fragment_metadata->size,
+ "orig_data_size", fragment_metadata->orig_data_size,
+ "chksum_type", chksum_type_str,
+ "chksum", encoded_chksum,
+ "chksum_mismatch", fragment_metadata->chksum_mismatch,
+ "backend_id", backend_id_str,
+ "backend_version", fragment_metadata->backend_version);
+ if (metadata_dict == NULL) {
+ pyeclib_c_seterr(-ENOMEM, "fragment_metadata_to_dict ERROR: ");
return NULL;
}
-
return metadata_dict;
}