From: Jinseok Kim <always.starving0@gmail.com>
To: andrea.cervesato@suse.com
Cc: ltp@lists.linux.it
Subject: [LTP] [PATCH v6] open: fix cleanup condition and use snprintf
Date: Fri, 27 Mar 2026 23:03:08 +0900 [thread overview]
Message-ID: <20260327140310.2306-1-always.starving0@gmail.com> (raw)
In-Reply-To: <69c403b7.050a0220.1389b1.789b@mx.google.com>
Replace sprintf() with snprintf() for safer string handling.
The cleanup logic checked '!first' to decide whether to close file
descriptors. Since file descriptor 0 is valid, this may incorrectly
skip cleanup and leak descriptors.
Also initialize "first" to -1 and set the fds array to -1 after
allocation. Skip such entries in cleanup() to avoid calling SAFE_CLOSE()
on invalid file descriptors.
Signed-off-by: Jinseok Kim <always.starving0@gmail.com>
---
testcases/kernel/syscalls/open/open04.c | 26 ++++++++++++++-----------
1 file changed, 15 insertions(+), 11 deletions(-)
diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index 7573af1c8..6ec18cbfa 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -15,9 +15,10 @@
#define FNAME "open04"
-static int fds_limit, first, i;
+static int fds_limit;
+static int first = -1;
static int *fds;
-static char fname[20];
+static char fname[PATH_MAX];
static void setup(void)
{
@@ -29,10 +30,11 @@ static void setup(void)
first = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0777);
fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
+ memset(fds, -1, sizeof(int) * (fds_limit - first));
fds[0] = first;
- for (i = first + 1; i < fds_limit; i++) {
- sprintf(fname, FNAME ".%d", i);
+ for (int i = first + 1; i < fds_limit; i++) {
+ snprintf(fname, sizeof(fname), FNAME ".%d", i);
fd = open(fname, O_RDWR | O_CREAT, 0777);
if (fd == -1) {
if (errno != EMFILE)
@@ -46,20 +48,22 @@ 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)
- return;
+ if (first >= 0 && fds) {
+ int limit = fds_limit - first;
- for (i = first; i < fds_limit; i++)
- SAFE_CLOSE(fds[i - first]);
+ for (int i = 0; i < limit; i++) {
+ if (fds[i] != -1)
+ SAFE_CLOSE(fds[i]);
+ }
+ }
- if (fds)
- free(fds);
+ free(fds);
}
static struct tst_test test = {
--
2.43.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-03-27 14:03 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-18 14:47 [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf Jinseok Kim
2026-02-18 14:47 ` [LTP] [PATCH v2 2/2] open: replace getdtablesize with getrlimit Jinseok Kim
2026-02-19 9:37 ` [LTP] [PATCH v2 1/2] open: fix cleanup condition and use snprintf Andrea Cervesato via ltp
2026-02-19 14:15 ` [LTP] [PATCH v3 " Jinseok Kim
2026-02-19 14:15 ` [LTP] [PATCH v3 2/2] open: replace getdtablesize with getrlimit Jinseok Kim
2026-03-13 16:42 ` Petr Vorel
2026-03-20 13:56 ` [LTP] [PATCH v3 1/2] open: fix cleanup condition and use snprintf Andrea Cervesato via ltp
2026-03-21 14:08 ` [LTP] [PATCH v4] " Jinseok Kim
2026-03-23 6:45 ` Andrea Cervesato via ltp
2026-03-25 12:22 ` [LTP] [PATCH v5] " Jinseok Kim
2026-03-25 15:48 ` Andrea Cervesato via ltp
2026-03-27 14:03 ` Jinseok Kim [this message]
2026-03-27 14:52 ` [LTP] [PATCH v6] " Andrea Cervesato via ltp
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=20260327140310.2306-1-always.starving0@gmail.com \
--to=always.starving0@gmail.com \
--cc=andrea.cervesato@suse.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox