simplefs icon indicating copy to clipboard operation
simplefs copied to clipboard

Incorrect boundary check in simplefs_ext_search()

Open ebendun opened this issue 4 months ago • 1 comments

Bug Report: Incorrect boundary check in simplefs_ext_search()

Location

File: extent.c, Line: 67

Issue Description

There's a logical error in the final boundary check of the simplefs_ext_search() function.

Current code (incorrect):

if (iblock >= end_block && iblock < end_len)
    return end;

Should be:

if (iblock >= end_block && iblock < end_block + end_len)
    return end;

Problem Analysis

The current code incorrectly compares iblock with end_len (which is a count of blocks) instead of the actual end boundary of the extent.

  • end_block represents the starting logical block number of an extent (e.g., 10)
  • end_len represents the number of consecutive blocks in the extent (e.g., 5)
  • An extent covering blocks [10, 11, 12, 13, 14] should have the range check: iblock >= 10 && iblock < 15
  • The current condition iblock < end_len becomes iblock < 5, which is incorrect

Code Inconsistency

The binary search loop correctly implements the boundary check:

if (iblock >= block && iblock < block + len) {
    return mid;
}

However, the final verification check uses an inconsistent pattern:

if (iblock >= end_block && iblock < end_len)  // Inconsistent!

This creates a logical contradiction where a block might never be found even if it exists within a valid extent.

ebendun avatar Aug 23 '25 02:08 ebendun