Browse Source

SDFile - fix errorneous offset computation

... my fault, I was originally too optimistic about the overflow

Fixes #3077

PFW-1233
D.R.racer 4 years ago
parent
commit
cdcc06f376
1 changed files with 8 additions and 5 deletions
  1. 8 5
      Firmware/SdFile.cpp

+ 8 - 5
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() {