@@ -180,22 +180,23 @@ class CDB
180
180
Dbt datValue;
181
181
datValue.set_flags (DB_DBT_MALLOC);
182
182
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 ());
193
198
}
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;
199
200
}
200
201
201
202
template <typename K, typename T>
@@ -222,8 +223,8 @@ class CDB
222
223
int ret = pdb->put (activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
223
224
224
225
// 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 ());
227
228
return (ret == 0 );
228
229
}
229
230
@@ -245,7 +246,7 @@ class CDB
245
246
int ret = pdb->del (activeTxn, &datKey, 0 );
246
247
247
248
// Clear memory
248
- memset (datKey.get_data (), 0 , datKey.get_size ());
249
+ memory_cleanse (datKey.get_data (), datKey.get_size ());
249
250
return (ret == 0 || ret == DB_NOTFOUND);
250
251
}
251
252
@@ -265,7 +266,7 @@ class CDB
265
266
int ret = pdb->exists (activeTxn, &datKey, 0 );
266
267
267
268
// Clear memory
268
- memset (datKey.get_data (), 0 , datKey.get_size ());
269
+ memory_cleanse (datKey.get_data (), datKey.get_size ());
269
270
return (ret == 0 );
270
271
}
271
272
@@ -308,8 +309,8 @@ class CDB
308
309
ssValue.write ((char *)datValue.get_data (), datValue.get_size ());
309
310
310
311
// 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 ());
313
314
free (datKey.get_data ());
314
315
free (datValue.get_data ());
315
316
return 0 ;
0 commit comments