Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Song Hu" <husong@kylinos.cn>,
	hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev,
	shakeelb@google.com, muchun.song@linux.dev,
	akpm@linux-foundation.org
Cc: tj@kernel.org, tjmercier@google.com, ddrokosov@salutedevices.com,
	linux-mm@kvack.org, mkoutny@suse.com
Subject: Re: [PATCH 4/4] samples/cgroup: don't let dirname() clobber the monitored path
Date: Tue, 03 Nov 2026 12:26:00 +0000	[thread overview]
Message-ID: <sashiko-outbox-195237@kernel.org> (raw)
In-Reply-To: <20261102122528.1498193-5-husong@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] samples/cgroup: main() prints dirname()-clobbered path instead of argv[1]
--

commit e427c2a6d742e00fa3e83aac4c0262cf23a479ec
Author: Song Hu <husong@kylinos.cn>

samples/cgroup: don't let dirname() clobber the monitored path

This commit duplicates argv[1] with strdup() before passing it to dirname()
in cgroup_event_listener so that dirname() does not modify argv[1] in place.

> diff --git a/samples/cgroup/cgroup_event_listener.c b/samples/cgroup/cgroup_event_listener.c
> index 3d70dc831a766..308cacec80a8b 100644
> --- a/samples/cgroup/cgroup_event_listener.c
> +++ b/samples/cgroup/cgroup_event_listener.c
> @@ -26,17 +26,27 @@ int main(int argc, char **argv)
>  	int event_control = -1;
>  	char event_control_path[PATH_MAX];
>  	char line[LINE_MAX];
> +	char *path;
>  	int ret;
>  
>  	if (argc != 3)
>  		errx(1, "%s", USAGE_STR);
>  
> -	cfd = open(argv[1], O_RDONLY);
> +	/*
> +	 * dirname(3) may modify its argument in place; work on a copy so
> +	 * argv[1] still refers to the monitored file when events are
> +	 * reported.
> +	 */
> +	path = strdup(argv[1]);
> +	if (!path)
> +		err(1, "Cannot duplicate %s", argv[1]);
> +
> +	cfd = open(path, O_RDONLY);
>  	if (cfd == -1)
> -		err(1, "Cannot open %s", argv[1]);
> +		err(1, "Cannot open %s", path);
>  
>  	ret = snprintf(event_control_path, PATH_MAX, "%s/cgroup.event_control",
> -			dirname(argv[1]));
> +			dirname(path));
>  	if (ret >= PATH_MAX)
>  		errx(1, "Path to cgroup.event_control is too long");
>  
> @@ -76,7 +86,7 @@ int main(int argc, char **argv)
>  		if (ret == -1)
>  			err(1, "cgroup.event_control is not accessible any more");
>  
> -		printf("%s %s: crossed\n", argv[1], argv[2]);
> +		printf("%s %s: crossed\n", path, argv[2]);

[Severity: Medium]
Should printf() in main() keep printing argv[1] here instead of path?

dirname(path) modifies the buffer pointed to by path in place while leaving
argv[1] intact, so switching the printf() call from argv[1] to path causes
main() to print the truncated parent directory rather than the full monitored
file path.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261102122528.1498193-1-husong@kylinos.cn?part=4


      reply	other threads:[~2026-11-03 12:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 12:25 [PATCH 0/4] samples/cgroup: fix issues in the event listener samples Song Hu
2026-11-02 12:25 ` [PATCH 1/4] samples/cgroup: fix poll error handling in memcg_event_listener Song Hu
2026-11-02 12:25 ` [PATCH 2/4] samples/cgroup: exit cleanly when the monitored memcg is removed Song Hu
2026-11-02 12:25 ` [PATCH 3/4] samples/cgroup: arm the inotify watch before reading the baseline Song Hu
2026-11-03 12:25   ` sashiko-bot
2026-11-02 12:25 ` [PATCH 4/4] samples/cgroup: don't let dirname() clobber the monitored path Song Hu
2026-11-03 12:26   ` sashiko-bot [this message]

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=sashiko-outbox-195237@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=ddrokosov@salutedevices.com \
    --cc=hannes@cmpxchg.org \
    --cc=husong@kylinos.cn \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeelb@google.com \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox