From 4dc9d4f779ec4e4c10d9d3a962d1b82b8d10b8d7 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 30 Sep 2019 16:54:05 -0700 Subject: [PATCH 01/12] Update code to avoid comparisons between signed and unsigned integers. Add verbose comments to show that the values changed from signed to unsigned, for the current code, would always be positive values. Add verbose comments to clarify the meaning of local variables. Add verbose comments to show all three possible states at all key computation points, for the higher cyclomatic complexity code block. --- .../InternalFileSytem/src/flash/flash_cache.c | 105 +++++++++++++++--- 1 file changed, 88 insertions(+), 17 deletions(-) diff --git a/libraries/InternalFileSytem/src/flash/flash_cache.c b/libraries/InternalFileSytem/src/flash/flash_cache.c index 7004b6dd..d3e7f7c4 100644 --- a/libraries/InternalFileSytem/src/flash/flash_cache.c +++ b/libraries/InternalFileSytem/src/flash/flash_cache.c @@ -97,34 +97,105 @@ void flash_cache_flush (flash_cache_t* fc) void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count) { + // there is no check for overflow / wraparound for dst + count, addr + count. + // this might be a useful thing to add for at least debug builds. + // overwrite with cache value if available - if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) && - !(addr < fc->cache_addr && addr + count <= fc->cache_addr) && - !(addr >= fc->cache_addr + FLASH_CACHE_SIZE) ) + if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) && // cache is not valid + !(addr < fc->cache_addr && addr + count <= fc->cache_addr) && // starts before, ends before cache area + !(addr >= fc->cache_addr + FLASH_CACHE_SIZE) ) // starts after end of cache area { - int dst_off = fc->cache_addr - addr; - int src_off = 0; - - if ( dst_off < 0 ) + // This block is entered only when the read overlaps the cache area by at least one byte. + // If the read starts before the cache area, it's further guaranteed + // that count is large enough to cause the read to enter + // the cache area by at least 1 byte. + uint32_t dst_off = 0; + uint32_t src_off = 0; + if (addr < fc->cache_addr) { - src_off = -dst_off; - dst_off = 0; + dst_off = fc->cache_addr - addr; + // Read the bytes prior to the cache address + fc->read(dst, addr, dst_off); } + else + { + src_off = addr - fc->cache_addr; + } + + // Thus, after the above code block executes: + // *** AT MOST ***, only one of src_off and dst_off are non-zero; + // (Both may be zero when the read starts at the start of the cache area.) + // dst_off corresponds to the number of bytes already read from PRIOR to the cache area. + // src_off corresponds to the byte offset to start reading at, from WITHIN the cache area. - int cache_bytes = minof(FLASH_CACHE_SIZE-src_off, count - dst_off); + // How many bytes to memcpy from flash area? + // Remember that, AT MOST, one of src_off and dst_off are non-zero. + // If src_off is non-zero, then dst_off is zero, representing that the + // read starts inside the cache. In this case: + // PARAM1 := FLASH_CACHE_SIZE - src_off == maximum possible bytes to read from cache + // PARAM2 := count + // Thus, taking the minimum of the two gives the number of bytes to read from cache, + // in the range [ 1 .. FLASH_CACHE_SIZE-src_off ]. + // Else if dst_off is non-zero, then src_off is zero, representing that the + // read started prior to the cache area. In this case: + // PARAM1 := FLASH_CACHE_SIZE == full size of the cache + // PARAM2 := count - dst_off == total bytes requested, minus the count of those already read + // Because the original request is guaranteed to overlap the cache, the range for + // PARAM2 is ensured to be [ 1 .. count-1 ]. + // Thus, taking the minimum of the two gives the number of bytes to read from cache, + // in the range [ 1 .. FLASH_CACHE_SIZE ] + // Else both src_off and dst_off are zero, representing that the read is starting + // exactly aligned to the cache. + // PARAM1 := FLASH_CACHE_SIZE + // PARAM2 := count + // Thus, taking the minimum of the two gives the number of bytes to read from cache, + // in the range [ 1 .. FLASH_CACHE_SIZE ] + // + // Therefore, in all cases, there is assurance that cache_bytes + // will be in the final range [1..FLASH_CACHE_SIZE]. + uint32_t cache_bytes = minof(FLASH_CACHE_SIZE-src_off, count - dst_off); - // start to cached - if ( dst_off ) fc->read(dst, addr, dst_off); - - // cached + // Use memcpy to read cached data into the buffer + // If src_off is non-zero, then dst_off is zero, representing that the + // read starts inside the cache. In this case: + // PARAM1 := dst + // PARAM2 := fc->cache_buf + src_off + // PARAM3 := cache_bytes + // Thus, all works as expected when starting in the midst of the cache. + // Else if dst_off is non-zero, then src_off is zero, representing that the + // read started prior to the cache. In this case: + // PARAM1 := dst + dst_off == destination offset by number of bytes already read + // PARAM2 := fc->cache_buf + // PARAM3 := cache_bytes + // Thus, all works as expected when starting prior to the cache. + // Else both src_off and dst_off are zero, representing that the read is starting + // exactly aligned to the cache. + // PARAM1 := dst + // PARAM2 := fc->cache_buf + // PARAM3 := cache_bytes + // Thus, all works as expected when starting exactly at the cache boundary + // + // Therefore, in all cases, there is assurance that cache_bytes + // will be in the final range [1..FLASH_CACHE_SIZE]. memcpy(dst + dst_off, fc->cache_buf + src_off, cache_bytes); - // cached to end - int copied = dst_off + cache_bytes; - if ( copied < count ) fc->read(dst + copied, addr + copied, count - copied); + // Read any final bytes from flash + // As noted above, dst_off represents the count of bytes read prior to the cache + // while cache_bytes represents the count of bytes read from the cache; + // This code block is guaranteed to overlap the cache area by at least one byte. + // Thus, copied will correspond to the total bytes already copied, + // and is guaranteed to be in the range [ 1 .. count ]. + uint32_t copied = dst_off + cache_bytes; + + // + if ( copied < count ) + { + fc->read(dst + copied, addr + copied, count - copied); + } } else { + // not using the cache, so just forward to read from flash fc->read(dst, addr, count); } } From 9de75957588eda15aff40e91bee6e7c87b2b9fb1 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 30 Sep 2019 17:05:06 -0700 Subject: [PATCH 02/12] Ensure stack variable fields are initialized, prior to calling a function that uses the fields (two lines later). --- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 89e62bad..d97e6812 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -211,7 +211,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) if (rd_rsp->count) { - ble_gattc_handle_value_t hdl_value; + ble_gattc_handle_value_t hdl_value = { 0, null }; if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) ) { From 44599b6e7a589c650a5ebbb8d6f656eeebdae34d Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 30 Sep 2019 17:07:04 -0700 Subject: [PATCH 03/12] Fix another warning caused by comparison between signed/unsigned values. --- libraries/Bluefruit52Lib/src/bluefruit.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 6d02f4c9..1401649e 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -205,7 +205,7 @@ void AdafruitBluefruit::configUuid128Count(uint8_t uuid128_max) void AdafruitBluefruit::configAttrTableSize(uint32_t attr_table_size) { - _sd_cfg.attr_table_size = align4( maxof(attr_table_size, BLE_GATTS_ATTR_TAB_SIZE_MIN) ); + _sd_cfg.attr_table_size = align4( maxof(attr_table_size, (uint32_t)(BLE_GATTS_ATTR_TAB_SIZE_MIN)) ); } void AdafruitBluefruit::configPrphConn(uint16_t mtu_max, uint16_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize) From e92d18e1526027ff2d7fe78309dc49021923256f Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 30 Sep 2019 17:08:08 -0700 Subject: [PATCH 04/12] Fix warning caused by conditional expression that is always true. After all, a uint8_t is alwasy <= 0xFF. --- libraries/Bluefruit52Lib/src/services/BLEMidi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index 50b00324..e751e43d 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -314,7 +314,7 @@ bool BLEMidi::isStatusByte( uint8_t b ) bool BLEMidi::oneByteMessage( uint8_t status ) { // system messages - if (status >= 0xF4 && status <= 0xFF) return true; + if (status >= 0xF4) return true; // system common if (status == 0xF1) return true; From bcf88a87fd6e851cbec6f3c6d4db64685a7574a5 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 30 Sep 2019 17:50:07 -0700 Subject: [PATCH 05/12] Fix typo from `null` to `nullptr`. --- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index d97e6812..fa7223d9 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -211,7 +211,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) if (rd_rsp->count) { - ble_gattc_handle_value_t hdl_value = { 0, null }; + ble_gattc_handle_value_t hdl_value = { 0, nullptr }; if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) ) { From e62e803fc4a954f34623795a21d9113800f2f3db Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 17:04:43 -0700 Subject: [PATCH 06/12] Fix format string to match number of arguments passed. --- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp index 0c789b54..b5475994 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp @@ -84,7 +84,7 @@ bool BLEDiscovery::_discoverService(uint16_t conn_handle, BLEClientService& svc, // timeout or has no data (due to GATT Error) if ( bytecount <= 0 ) { - LOG_LV1("DISC", "[SVC] timeout or error", start_handle); + LOG_LV1("DISC", "[SVC] timeout or error %ud", start_handle); return false; } From a5ce35904f8da15af0b66b7c9e2c4bb8905eb889 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 17:25:48 -0700 Subject: [PATCH 07/12] Reduce compiler warnings due to format string mismatches. --- cores/nRF5/common_func.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/common_func.h b/cores/nRF5/common_func.h index fdec3f44..e2947495 100644 --- a/cores/nRF5/common_func.h +++ b/cores/nRF5/common_func.h @@ -171,7 +171,8 @@ const char* dbg_err_str(int32_t err_id); // TODO move to other place #define ADALOG_BUFFER(_tag, _buf, _n) \ do {\ - if ( _tag ) PRINTF("%-6s: len = %d\n", _tag, _n);\ + const char * _xtag = _tag;\ + if ( _xtag ) PRINTF("%-6s: len = %d\n", _xtag, _n);\ dbgDumpMemory(_buf, 1, _n, true);\ }while(0) From 659687bac4bcf72210727e43ccfa93963a240862 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 17:35:11 -0700 Subject: [PATCH 08/12] Fix printf format specifier to match length of data type. --- libraries/Bluefruit52Lib/src/bluefruit.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 1401649e..c3c6c603 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -437,11 +437,11 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count) if ( err ) { LOG_LV1("CFG", "SoftDevice config require more SRAM than provided by linker.\n" - "App Ram Start must be at least 0x%08X (provided 0x%08X)\n" + "App Ram Start must be at least 0x%08lX (provided 0x%08lX)\n" "Please update linker file or re-config SoftDevice", ram_start, (uint32_t) __data_start__); } - LOG_LV1("CFG", "SoftDevice's RAM requires: 0x%08X", ram_start); + LOG_LV1("CFG", "SoftDevice's RAM requires: 0x%08lX", ram_start); VERIFY_STATUS(err, false); /*------------- Configure BLE Option -------------*/ From f3cc656b4cd198adea48e16b0b8243de0f6a2165 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 18:01:53 -0700 Subject: [PATCH 09/12] Fix printf format specifier to match length of data type. --- libraries/Bluefruit52Lib/src/utility/bonding.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp index 28a1073f..9783ac5d 100644 --- a/libraries/Bluefruit52Lib/src/utility/bonding.cpp +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -123,7 +123,7 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_keys_t* bk bdata_write(&file, devname, strlen(devname)+1); // save also null char - BOND_LOG("Saved keys for \"%s\" to file %s ( %d bytes )", devname, filename, file.size()); + BOND_LOG("Saved keys for \"%s\" to file %s ( %ld bytes )", devname, filename, file.size()); file.close(); } @@ -178,7 +178,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv) bdata_write(&file, sys_attr, len); - BOND_LOG("Saved CCCD setting to file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1), len); + BOND_LOG("Saved CCCD setting to file %s ( offset = %ld, len = %d bytes )", filename, file.size() - (len + 1), len); file.close(); } @@ -217,7 +217,7 @@ bool bond_load_cccd(uint8_t role, uint16_t conn_hdl, uint16_t ediv) if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) { loaded = true; - BOND_LOG("Loaded CCCD from file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1), len); + BOND_LOG("Loaded CCCD from file %s ( offset = %ld, len = %d bytes )", filename, file.size() - (len + 1), len); } } } From f0f6b1041ad391f8fa53526db566b26ac7c7e0bc Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 18:02:09 -0700 Subject: [PATCH 10/12] Fix printf format specifier to match length of data type. --- libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index 9dcf47f3..0c24f4b2 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -179,7 +179,7 @@ const char* dbg_strerr_lfs (int32_t err) default: static char errcode[10]; - sprintf(errcode, "%d", err); + sprintf(errcode, "%ld", err); return errcode; } From 4904ee5a9d1871033dc206c7d860a2b50fda2ce1 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 18:02:26 -0700 Subject: [PATCH 11/12] Fix printf format specifier to match length of data type. Here, LFS uses an int for return value, which is 16-bit. The VERIFY_MESS expects a 32-bit value, so cast here at the boundary between the two sets of files. --- libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h index 2861ee75..4514646a 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h @@ -83,7 +83,7 @@ class Adafruit_LittleFS #define PRINT_LFS_ERR(_err) #else #define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs) - #define PRINT_LFS_ERR(_err) VERIFY_MESS(_err, dbg_strerr_lfs) + #define PRINT_LFS_ERR(_err) VERIFY_MESS((long int)_err, dbg_strerr_lfs) // LFS_ERR are of type int, VERIFY_MESS expects long_int const char* dbg_strerr_lfs (int32_t err); #endif From 31180ec4045cdde67201f2998c1c62350511e1e7 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sat, 5 Oct 2019 18:03:59 -0700 Subject: [PATCH 12/12] Fix printf format specifier to match length of data type. --- cores/nRF5/verify.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/nRF5/verify.h b/cores/nRF5/verify.h index 8314276b..1154f777 100644 --- a/cores/nRF5/verify.h +++ b/cores/nRF5/verify.h @@ -62,7 +62,7 @@ extern "C" do { \ const char* (*_fstr)(int32_t) = _funcstr;\ printf("%s: %d: verify failed, error = ", __PRETTY_FUNCTION__, __LINE__);\ - if (_fstr) printf(_fstr(_status)); else printf("%d", _status);\ + if (_fstr) printf(_fstr(_status)); else printf("%ld", _status);\ printf("\n");\ }while(0) #else