[LTP] [PATCH 2/3] tst_memutils: Fix mapping overlap predicate

Li Wang li.wang@linux.dev
Wed Oct 7 08:18:44 CEST 2026


On Tue, Oct 06, 2026 at 03:02:38PM +0200, Andrea Cervesato wrote:
> From: Andrea Cervesato <andrea.cervesato@suse.com>
> 
> tst_mapping_in_range() checks whether either mapping endpoint falls
> inside [low, high). This misses existing mappings that completely
> enclose the requested range, and falsely treats an adjacent mapping
> ending exactly at low as an overlap.
> 
> Use the standard half-open interval overlap check
> (start < high && end > low).
> 
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>

Reviewed-by: Li Wang <li.wang@linux.dev>

> ---
>  lib/tst_memutils.c | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
> index e49684ba1..81467fef7 100644
> --- a/lib/tst_memutils.c
> +++ b/lib/tst_memutils.c
> @@ -202,11 +202,7 @@ int tst_mapping_in_range(unsigned long low, unsigned long high)
>  			tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");
>  		}
>  
> -		if ((start >= low) && (start < high)) {
> -			fclose(fp);
> -			return 1;
> -		}
> -		if ((end >= low) && (end < high)) {
> +		if (start < high && end > low) {
>  			fclose(fp);
>  			return 1;
>  		}
> 
> -- 
> 2.51.0

This is a nice fix!

But after looking at tst_mapping_in_range(), I think there are still
places that need improvement:

1. feof() only returns true after a read attempt has failed.
   Using while (!feof(fp)) causes the last read to be processed twice.

2. If the end of file is reached, fscanf() returns EOF (-1),
   which is not a parsing error.

3. If the caller passes low > high, the function should report an error.
   Currently, it silently returns 0.

Something maybe better like:

int tst_mapping_in_range(unsigned long low, unsigned long high)
{
	FILE *fp;
	unsigned long start, end;
	int ret = 0;

	if (low >= high)
		tst_brk(TBROK, "invalid range [%lu, %lu)", low, high);

	fp = SAFE_FOPEN("/proc/self/maps", "r");

	while (fscanf(fp, "%lx-%lx %*[^\n]\n", &start, &end) == 2) {
		/*
		 * Standard half-open interval overlap check:
		 *   [start, end) intersects [low, high)
		 *     <=> start < high && end > low
		 */
		if (start < high && end > low) {
			ret = 1;
			break;
		}
	}

	fclose(fp);
	return ret;
}

-- 
Regards,
Li Wang


More information about the ltp mailing list