@@ -180,22 +180,23 @@ class CDB
180180 Dbt datValue;
181181 datValue.set_flags (DB_DBT_MALLOC);
182182 int ret = pdb->get (activeTxn, &datKey, &datValue, 0 );
183- memset (datKey.get_data (), 0 , datKey.get_size ());
184- if (datValue.get_data () == NULL )
185- return false ;
186-
187- // Unserialize value
188- try {
189- CDataStream ssValue ((char *)datValue.get_data (), (char *)datValue.get_data () + datValue.get_size (), SER_DISK, CLIENT_VERSION);
190- ssValue >> value;
191- } catch (const std::exception&) {
192- return false ;
183+ memory_cleanse (datKey.get_data (), datKey.get_size ());
184+ bool success = false ;
185+ if (datValue.get_data () != NULL ) {
186+ // Unserialize value
187+ try {
188+ CDataStream ssValue ((char *)datValue.get_data (), (char *)datValue.get_data () + datValue.get_size (), SER_DISK, CLIENT_VERSION);
189+ ssValue >> value;
190+ success = true ;
191+ } catch (const std::exception&) {
192+ // In this case success remains 'false'
193+ }
194+
195+ // Clear and free memory
196+ memory_cleanse (datValue.get_data (), datValue.get_size ());
197+ free (datValue.get_data ());
193198 }
194-
195- // Clear and free memory
196- memset (datValue.get_data (), 0 , datValue.get_size ());
197- free (datValue.get_data ());
198- return (ret == 0 );
199+ return ret == 0 && success;
199200 }
200201
201202 template <typename K, typename T>
@@ -222,8 +223,8 @@ class CDB
222223 int ret = pdb->put (activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
223224
224225 // Clear memory in case it was a private key
225- memset (datKey.get_data (), 0 , datKey.get_size ());
226- memset (datValue.get_data (), 0 , datValue.get_size ());
226+ memory_cleanse (datKey.get_data (), datKey.get_size ());
227+ memory_cleanse (datValue.get_data (), datValue.get_size ());
227228 return (ret == 0 );
228229 }
229230
@@ -245,7 +246,7 @@ class CDB
245246 int ret = pdb->del (activeTxn, &datKey, 0 );
246247
247248 // Clear memory
248- memset (datKey.get_data (), 0 , datKey.get_size ());
249+ memory_cleanse (datKey.get_data (), datKey.get_size ());
249250 return (ret == 0 || ret == DB_NOTFOUND);
250251 }
251252
@@ -265,7 +266,7 @@ class CDB
265266 int ret = pdb->exists (activeTxn, &datKey, 0 );
266267
267268 // Clear memory
268- memset (datKey.get_data (), 0 , datKey.get_size ());
269+ memory_cleanse (datKey.get_data (), datKey.get_size ());
269270 return (ret == 0 );
270271 }
271272
@@ -308,8 +309,8 @@ class CDB
308309 ssValue.write ((char *)datValue.get_data (), datValue.get_size ());
309310
310311 // Clear and free memory
311- memset (datKey.get_data (), 0 , datKey.get_size ());
312- memset (datValue.get_data (), 0 , datValue.get_size ());
312+ memory_cleanse (datKey.get_data (), datKey.get_size ());
313+ memory_cleanse (datValue.get_data (), datValue.get_size ());
313314 free (datKey.get_data ());
314315 free (datValue.get_data ());
315316 return 0 ;
0 commit comments