diff --git a/src/main/client/put.c b/src/main/client/put.c index fa7910bb7..6e369128b 100644 --- a/src/main/client/put.c +++ b/src/main/client/put.c @@ -62,10 +62,6 @@ PyObject *AerospikeClient_Put_Invoke(AerospikeClient *self, PyObject *py_key, bool key_initialised = false; bool record_initialised = false; - // Initialize record - as_record_init(&rec, 0); - record_initialised = true; - as_static_pool static_pool; memset(&static_pool, 0, sizeof(static_pool)); @@ -94,9 +90,11 @@ PyObject *AerospikeClient_Put_Invoke(AerospikeClient *self, PyObject *py_key, // Convert python bins and metadata objects to as_record as_record_init_from_pyobject(self, &err, py_bins, py_meta, &rec, serializer_option, &static_pool); + // Initialize record if (err.code != AEROSPIKE_OK) { goto CLEANUP; } + record_initialised = true; // Convert python policy object to as_policy_write pyobject_to_policy_write(self, &err, py_policy, &write_policy, diff --git a/src/main/client/remove_bin.c b/src/main/client/remove_bin.c index 1444f5747..655cba60f 100644 --- a/src/main/client/remove_bin.c +++ b/src/main/client/remove_bin.c @@ -56,6 +56,7 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, char *binName = NULL; int count = 0; PyObject *py_ustr = NULL; + bool record_initialised = false; // For converting expressions. as_exp exp_list; @@ -63,8 +64,6 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, // Get the bin list size; Py_ssize_t size = PyList_Size(py_binList); - // Initialize record - as_record_inita(&rec, size); // Convert python key object to as_key pyobject_to_key(err, py_key, &key); @@ -83,6 +82,20 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, } // Invoke operation + // TODO: check for mem leak from policy + PyObject *py_bins = PyDict_New(); + if (py_bins) { + goto CLEANUP; + } + + // Serializer option doesn't matter since we're just deleting bins + // static_pool isn't used since the bin dictionary is guaranteed to be empty + as_record_init_from_pyobject(self, err, py_bins, py_meta, &rec, + SERIALIZER_NONE, NULL); + if (err->code != AEROSPIKE_OK) { + goto CLEANUP; + } + record_initialised = true; for (count = 0; count < size; count++) { PyObject *py_val = PyList_GetItem(py_binList, count); @@ -93,9 +106,11 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, else { as_error_update(err, AEROSPIKE_ERR_CLIENT, "Invalid bin name, bin name should be a string or " - "unicode string") goto CLEANUP; + "unicode string"); + goto CLEANUP; } if (!as_record_set_nil(&rec, binName)) { + // TODO: mem leak goto CLEANUP; } if (py_ustr) { @@ -104,52 +119,15 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, } } - if (py_meta && PyDict_Check(py_meta)) { - PyObject *py_gen = PyDict_GetItemString(py_meta, "gen"); - PyObject *py_ttl = PyDict_GetItemString(py_meta, "ttl"); - - if (py_ttl) { - if (PyLong_Check(py_ttl)) { - rec.ttl = (uint32_t)PyLong_AsLong(py_ttl); - if ((uint32_t)-1 == rec.ttl && PyErr_Occurred()) { - as_error_update( - err, AEROSPIKE_ERR_PARAM, - "integer value for ttl exceeds sys.maxsize"); - goto CLEANUP; - } - } - else { - as_error_update(err, AEROSPIKE_ERR_PARAM, - "Ttl should be an int or long"); - goto CLEANUP; - } - } - - if (py_gen) { - if (PyLong_Check(py_gen)) { - rec.gen = (uint16_t)PyLong_AsLongLong(py_gen); - if ((uint16_t)-1 == rec.gen && PyErr_Occurred()) { - as_error_update( - err, AEROSPIKE_ERR_PARAM, - "integer value for gen exceeds sys.maxsize"); - goto CLEANUP; - } - } - else { - as_error_update(err, AEROSPIKE_ERR_PARAM, - "Generation should be an int or long"); - goto CLEANUP; - } - } - } - Py_BEGIN_ALLOW_THREADS aerospike_key_put(self->as, err, write_policy_p, &key, &rec); Py_END_ALLOW_THREADS CLEANUP: - as_record_destroy(&rec); + if (record_initialised) { + as_record_destroy(&rec); + } if (exp_list_p) { as_exp_destroy(exp_list_p); @@ -163,6 +141,9 @@ AerospikeClient_RemoveBin_Invoke(AerospikeClient *self, PyObject *py_key, raise_exception_base(err, py_key, Py_None, Py_None, Py_None, Py_None); return NULL; } + else if (PyErr_Occurred()) { + return NULL; + } return PyLong_FromLong(0); }