public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] open: fix cleanup condition and use snprintf
@ 2026-02-12 13:22 Jinseok Kim
  2026-02-12 13:22 ` [LTP] [PATCH 2/2] open: replace getdtablesize with getrlimit Jinseok Kim
  0 siblings, 1 reply; 7+ messages in thread
From: Jinseok Kim @ 2026-02-12 13:22 UTC (permalink / raw)
  To: ltp

The test uses sprintf() to build temporary file names, which may
overflow the fixed-size buffer. Replace it with snprintf() to avoid
potential buffer overflows.

The cleanup logic also checked '!first' to decide whether to close
file descriptors. Since file descriptor 0 is valid, this condition
can incorrectly skip cleanup and leak file descriptors.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open04.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index 3dc3486d3..ed5dd27bb 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -30,7 +30,7 @@ static void setup(void)
 	fds[0] = first;

 	for (i = first + 1; i < fds_limit; i++) {
-		sprintf(fname, FNAME ".%d", i);
+		snprintf(fname, sizeof(fname), FNAME ".%d", i);
 		fd = open(fname, O_RDWR | O_CREAT, 0777);
 		if (fd == -1) {
 			if (errno != EMFILE)
@@ -50,7 +50,7 @@ static void run(void)

 static void cleanup(void)
 {
-	if (!first || !fds)
+	if (first < 0 || !fds)
 		return;

 	for (i = first; i < fds_limit; i++)
--
2.43.0

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [LTP] [PATCH 2/2] open: replace getdtablesize with getrlimit
  2026-02-12 13:22 [LTP] [PATCH 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
@ 2026-02-12 13:22 ` Jinseok Kim
  2026-02-17 11:40   ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 7+ messages in thread
From: Jinseok Kim @ 2026-02-12 13:22 UTC (permalink / raw)
  To: ltp

The test currently uses getdtablesize() to determine the maximum
number of file descriptors for the process. This interface is
considered legacy and is not specified by POSIX.

Use getrlimit() instead, which provides a well-defined
and portable way to obtain the per-process file descriptor limit.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open04.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index ed5dd27bb..3d8ea2ae8 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -11,6 +11,7 @@

 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/resource.h>
 #include "tst_test.h"

 #define FNAME "open04"
@@ -23,7 +24,10 @@ static void setup(void)
 {
 	int fd;

-	fds_limit = getdtablesize();
+	struct rlimit rlim;
+
+	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim);
+	fds_limit = rlim.rlim_cur;
 	first = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0777);

 	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
--
2.43.0

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH 2/2] open: replace getdtablesize with getrlimit
  2026-02-12 13:22 ` [LTP] [PATCH 2/2] open: replace getdtablesize with getrlimit Jinseok Kim
@ 2026-02-17 11:40   ` Andrea Cervesato via ltp
  2026-02-17 12:22     ` [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Cervesato via ltp @ 2026-02-17 11:40 UTC (permalink / raw)
  To: Jinseok Kim, ltp

Hi!

Patch is ok, but it might be good to change `sprintf()` with `snprintf()`
in the setup() as well to ensure safety.

King regards,
-- 
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf
  2026-02-17 11:40   ` Andrea Cervesato via ltp
@ 2026-02-17 12:22     ` Jinseok Kim
  2026-02-18 12:34       ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 7+ messages in thread
From: Jinseok Kim @ 2026-02-17 12:22 UTC (permalink / raw)
  To: ltp, andrea.cervesato

Hi Cervesato.

Thanks for the review!

Changes since v1:
- Replace remaining sprintf() with snprintf() in setup()

Thanks,
Jinseok.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open04.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index 3dc3486d3..5d39c1569 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -30,7 +30,7 @@ static void setup(void)
 	fds[0] = first;

 	for (i = first + 1; i < fds_limit; i++) {
-		sprintf(fname, FNAME ".%d", i);
+		snprintf(fname, sizeof(fname), FNAME ".%d", i);
 		fd = open(fname, O_RDWR | O_CREAT, 0777);
 		if (fd == -1) {
 			if (errno != EMFILE)
@@ -44,13 +44,13 @@ static void setup(void)

 static void run(void)
 {
-	sprintf(fname, FNAME ".%d", fds_limit);
+	snprintf(fname, sizeof(fname), FNAME ".%d", fds_limit);
 	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
 }

 static void cleanup(void)
 {
-	if (!first || !fds)
+	if (first < 0 || !fds)
 		return;

 	for (i = first; i < fds_limit; i++)
--
2.43.0

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf
  2026-02-17 12:22     ` [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
@ 2026-02-18 12:34       ` Andrea Cervesato via ltp
  0 siblings, 0 replies; 7+ messages in thread
From: Andrea Cervesato via ltp @ 2026-02-18 12:34 UTC (permalink / raw)
  To: Jinseok Kim, ltp

On Tue Feb 17, 2026 at 1:22 PM CET, Jinseok Kim wrote:
> Hi Cervesato.
>
> Thanks for the review!
>
> Changes since v1:
> - Replace remaining sprintf() with snprintf() in setup()
>
> Thanks,
> Jinseok.
>
> Signed-off-by: Jinseok Kim <always.starving0@gmail.com>

Are you using an automatic tool? This seems like a wrong commit message.
Please resend the whole patch-set with a proper version and right commit
message. I will reject this one.

Kind regards,
-- 
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf
@ 2026-02-18 14:47 Jinseok Kim
  2026-02-19  9:37 ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 7+ messages in thread
From: Jinseok Kim @ 2026-02-18 14:47 UTC (permalink / raw)
  To: ltp

Replace remaining sprintf() with snprintf() in setup()

Thanks for the review.

Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
 testcases/kernel/syscalls/open/open04.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index 3dc3486d3..5d39c1569 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -30,7 +30,7 @@ static void setup(void)
 	fds[0] = first;

 	for (i = first + 1; i < fds_limit; i++) {
-		sprintf(fname, FNAME ".%d", i);
+		snprintf(fname, sizeof(fname), FNAME ".%d", i);
 		fd = open(fname, O_RDWR | O_CREAT, 0777);
 		if (fd == -1) {
 			if (errno != EMFILE)
@@ -44,13 +44,13 @@ static void setup(void)

 static void run(void)
 {
-	sprintf(fname, FNAME ".%d", fds_limit);
+	snprintf(fname, sizeof(fname), FNAME ".%d", fds_limit);
 	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
 }

 static void cleanup(void)
 {
-	if (!first || !fds)
+	if (first < 0 || !fds)
 		return;

 	for (i = first; i < fds_limit; i++)
--
2.43.0

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf
  2026-02-18 14:47 Jinseok Kim
@ 2026-02-19  9:37 ` Andrea Cervesato via ltp
  0 siblings, 0 replies; 7+ messages in thread
From: Andrea Cervesato via ltp @ 2026-02-19  9:37 UTC (permalink / raw)
  To: Jinseok Kim, ltp

Hi!

On Wed Feb 18, 2026 at 3:47 PM CET, Jinseok Kim wrote:
> Replace remaining sprintf() with snprintf() in setup()
>
> Thanks for the review.

Again, there's something wrong with your automated system. Is it LLM? :-)
The message is misleading, also because not only snprintf() touched, but
also cleanup() logic.

This has to be fixed, otherwise patch can't be accepted.

>
> Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
> ---
>  testcases/kernel/syscalls/open/open04.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
> index 3dc3486d3..5d39c1569 100644
> --- a/testcases/kernel/syscalls/open/open04.c
> +++ b/testcases/kernel/syscalls/open/open04.c
> @@ -30,7 +30,7 @@ static void setup(void)
>  	fds[0] = first;
>
>  	for (i = first + 1; i < fds_limit; i++) {
> -		sprintf(fname, FNAME ".%d", i);
> +		snprintf(fname, sizeof(fname), FNAME ".%d", i);
>  		fd = open(fname, O_RDWR | O_CREAT, 0777);
>  		if (fd == -1) {
>  			if (errno != EMFILE)
> @@ -44,13 +44,13 @@ static void setup(void)
>
>  static void run(void)
>  {
> -	sprintf(fname, FNAME ".%d", fds_limit);
> +	snprintf(fname, sizeof(fname), FNAME ".%d", fds_limit);
>  	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
>  }
>
>  static void cleanup(void)
>  {
> -	if (!first || !fds)
> +	if (first < 0 || !fds)

And this cleanup has an issue. See below..

>  		return;
>
>  	for (i = first; i < fds_limit; i++)
> --
> 2.43.0

This test has to work also for `-i 0` option. The way we can make it
works is the following:

- "first" should be initialized to -1, otherwise "first < 0" won't be
  catched
- the fds should be initialized with -1 after SAFE_MALLOC(), otherwise
  you will fail the cleanup if malloc will break the test


Kind Regards,
-- 
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-02-19  9:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-12 13:22 [LTP] [PATCH 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
2026-02-12 13:22 ` [LTP] [PATCH 2/2] open: replace getdtablesize with getrlimit Jinseok Kim
2026-02-17 11:40   ` Andrea Cervesato via ltp
2026-02-17 12:22     ` [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
2026-02-18 12:34       ` Andrea Cervesato via ltp
  -- strict thread matches above, loose matches on Subject: below --
2026-02-18 14:47 Jinseok Kim
2026-02-19  9:37 ` Andrea Cervesato via ltp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox