All of lore.kernel.org
 help / color / mirror / Atom feed
From: Li Wang <li.wang@linux.dev>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: Linux Test Project <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH 2/3] tst_memutils: Fix mapping overlap predicate
Date: Wed, 7 Oct 2026 14:18:44 +0800	[thread overview]
Message-ID: <asXkRD5wevW5v2zg@linux.dev> (raw)
In-Reply-To: <20261006-fix_simple_bugs_core_lib-v1-2-c94199a46ec5@suse.com>

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2026-10-07  7:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-06 13:02 [LTP] [PATCH 0/3] Fix various minor bugs in core library Andrea Cervesato
2026-10-06 13:02 ` [LTP] [PATCH 1/3] tst_timer_test: Fix out-of-bounds array access Andrea Cervesato
2026-10-06 18:53   ` [LTP] " linuxtestproject.agent
2026-10-07  5:50   ` [LTP] [PATCH 1/3] " Li Wang
2026-10-06 13:02 ` [LTP] [PATCH 2/3] tst_memutils: Fix mapping overlap predicate Andrea Cervesato
2026-10-07  6:18   ` Li Wang [this message]
2026-10-06 13:02 ` [LTP] [PATCH 3/3] tst_kconfig: Prevent stack buffer overread Andrea Cervesato
2026-10-07  5:51   ` Li Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=asXkRD5wevW5v2zg@linux.dev \
    --to=li.wang@linux.dev \
    --cc=andrea.cervesato@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.