From cdcc06f3769967857eeba263e748d021aa1a921b Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 29 Mar 2021 12:21:42 +0200 Subject: [PATCH] SDFile - fix errorneous offset computation ... my fault, I was originally too optimistic about the overflow Fixes #3077 PFW-1233 --- Firmware/SdFile.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Firmware/SdFile.cpp b/Firmware/SdFile.cpp index 1bad4319f..e21144ee1 100644 --- a/Firmware/SdFile.cpp +++ b/Firmware/SdFile.cpp @@ -178,14 +178,17 @@ eof_or_fail: } bool SdFile::gfEnsureBlock(){ - if ( vol_->cacheRawBlock(gfBlock, SdVolume::CACHE_FOR_READ)){ + // this comparison is heavy-weight, especially when there is another one inside cacheRawBlock + // but it is necessary to avoid computing of terminateOfs if not needed + if( gfBlock != vol_->cacheBlockNumber_ ){ + if ( ! vol_->cacheRawBlock(gfBlock, SdVolume::CACHE_FOR_READ)){ + return false; + } // terminate with a '\n' - const uint16_t terminateOfs = fileSize_ - gfOffset; + const uint32_t terminateOfs = fileSize_ - gfOffset; vol_->cache()->data[ terminateOfs < 512 ? terminateOfs : 512 ] = '\n'; - return true; - } else { - return false; } + return true; } bool SdFile::gfComputeNextFileBlock() {