From 87cc940604e19bdcdb4a57161d70cb6430b8fb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20P=C3=A9tr=C3=A9?= Date: Mon, 7 Jan 2019 00:05:05 +0100 Subject: [PATCH] Fix an improper use of `assign` in the DAC scans This fixes a bug due to an access to a deleted memory range when checking the "CACHED" key existence in the LMDB database. An overload of lmdb::dbi::get is implemented for std::string in order to check the exsitence of a key in the LMDB database. --- include/lmdb_cpp_wrapper.h | 14 ++++++++++++++ src/calibration_routines.cpp | 13 +++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/lmdb_cpp_wrapper.h b/include/lmdb_cpp_wrapper.h index ab75f8c..4cdcdcb 100644 --- a/include/lmdb_cpp_wrapper.h +++ b/include/lmdb_cpp_wrapper.h @@ -1527,6 +1527,20 @@ class lmdb::dbi { return lmdb::dbi_get(txn, handle(), key, data); } + /** + * Retrieves a key from this database. + * + * @param txn a transaction handle + * @param key + * @throws lmdb::error on failure + */ + bool get(MDB_txn* const txn, + const std::string& key) const { + const lmdb::val k{key}; + lmdb::val v{}; + return lmdb::dbi_get(txn, handle(), k, v); + } + /** * Retrieves a key from this database. * diff --git a/src/calibration_routines.cpp b/src/calibration_routines.cpp index 94166a2..2d93307 100644 --- a/src/calibration_routines.cpp +++ b/src/calibration_routines.cpp @@ -1149,7 +1149,7 @@ std::vector dacScanLocal(localArgs *la, uint32_t ohN, uint32_t dacSele LOGGER->log_message(LogManager::INFO, stdsprintf("Scanning DAC: %s",regName.c_str())); uint32_t adcAddr[24]; uint32_t adcCacheUpdateAddr[24]; - bool foundAdcCached=false; + bool foundAdcCached = false; for(int vfatN=0; vfatN<24; ++vfatN){ //Skip Masked VFATs if ( !( (notmask >> vfatN) & 0x1)) continue; @@ -1160,11 +1160,7 @@ std::vector dacScanLocal(localArgs *la, uint32_t ohN, uint32_t dacSele //Get ADC address if(useExtRefADC){ //Case: Use ADC with external reference //for backward compatibility, use ADC1 instead of ADC1_CACHED if it exists - lmdb::val key, db_res; - - key.assign(strRegBase+"ADC1_CACHED"); - foundAdcCached = la->dbi.get(la->rtxn,key,db_res); - if(foundAdcCached){ + if((foundAdcCached = la->dbi.get(la->rtxn, strRegBase + "ADC1_CACHED"))){ adcAddr[vfatN] = getAddress(la, strRegBase + "ADC1_CACHED"); adcCacheUpdateAddr[vfatN] = getAddress(la, strRegBase + "ADC1_UPDATE"); } @@ -1173,10 +1169,7 @@ std::vector dacScanLocal(localArgs *la, uint32_t ohN, uint32_t dacSele } //End Case: Use ADC with external reference else{ //Case: Use ADC with internal reference //for backward compatibility, use ADC0 instead of ADC0_CACHED if it exists - lmdb::val key, db_res; - key.assign(strRegBase+"ADC0_CACHED"); - foundAdcCached = la->dbi.get(la->rtxn,key,db_res); - if(foundAdcCached) { + if((foundAdcCached = la->dbi.get(la->rtxn, strRegBase + "ADC0_CACHED"))){ adcAddr[vfatN] = getAddress(la, strRegBase + "ADC0_CACHED"); adcCacheUpdateAddr[vfatN] = getAddress(la, strRegBase + "ADC0_UPDATE"); }