* [LTP] [PATCH 1/2] lib: safe_file_ops: Add SAFE_FILE_READ_STR()
2026-04-09 10:30 [LTP] [PATCH 0/2] Fix proc parsing in newuname01 Cyril Hrubis
@ 2026-04-09 10:30 ` Cyril Hrubis
2026-04-09 13:35 ` Cyril Hrubis
2026-04-09 10:30 ` [LTP] [PATCH 2/2] syscalls: newuname01: Fix fail on empty domainname Cyril Hrubis
1 sibling, 1 reply; 4+ messages in thread
From: Cyril Hrubis @ 2026-04-09 10:30 UTC (permalink / raw)
To: ltp
Unlike the scanf("%s") this function can properly handle empty files and
in case of an empty file produces an empty string.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
include/tst_safe_file_ops.h | 21 +++++++++++++++++++++
lib/safe_file_ops.c | 24 ++++++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/include/tst_safe_file_ops.h b/include/tst_safe_file_ops.h
index 0d8819594..d9110ba9c 100644
--- a/include/tst_safe_file_ops.h
+++ b/include/tst_safe_file_ops.h
@@ -14,6 +14,27 @@
safe_file_scanf(__FILE__, __LINE__, NULL, \
(path), (fmt), ## __VA_ARGS__)
+/**
+ * SAFE_FILE_READ_STR() - Reads a string from a file.
+ *
+ * Unlike scanf("%s") this function works fine with empty files or files that
+ * consist only of white spaces. In such case an empty string is stored into
+ * the supplied buffer.
+ *
+ * It's recomended to use this for various sysfs or procfs files that may be
+ * empty.
+ *
+ * @path: A path to a file.
+ * @buf: A buffer to store the string into.
+ * @buf_size: A buffer size.
+ */
+#define SAFE_FILE_READ_STR(path, buf, buf_size) \
+ safe_file_read_str(__FILE__, __LINE__, \
+ (path), (buf), (buf_size))
+
+void safe_file_read_str(const char *file, const int lineno,
+ const char *path, char *buf, size_t buf_size);
+
#define FILE_LINES_SCANF(path, fmt, ...) \
file_lines_scanf(__FILE__, __LINE__, NULL, 0,\
(path), (fmt), ## __VA_ARGS__)
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 8314c4b1b..2d163c0af 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -160,6 +160,30 @@ void safe_file_scanf(const char *file, const int lineno,
}
}
+void safe_file_read_str(const char *file, const int lineno,
+ const char *path, char *buf, size_t buf_size)
+{
+ FILE *f;
+
+ f = fopen(path, "r");
+ if (!f) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+ "Failed to open FILE '%s' for reading", path);
+ }
+
+ if (!fgets(buf, buf_size, f))
+ buf[0] = 0;
+
+ size_t len = strlen(buf);
+
+ if (len && buf[len-1] == '\n')
+ buf[len-1] = 0;
+
+ if (fclose(f)) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+ "Failed to close FILE '%s'", path);
+ }
+}
/*
* Try to parse each line from file specified by 'path' according
--
2.52.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread* [LTP] [PATCH 2/2] syscalls: newuname01: Fix fail on empty domainname
2026-04-09 10:30 [LTP] [PATCH 0/2] Fix proc parsing in newuname01 Cyril Hrubis
2026-04-09 10:30 ` [LTP] [PATCH 1/2] lib: safe_file_ops: Add SAFE_FILE_READ_STR() Cyril Hrubis
@ 2026-04-09 10:30 ` Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2026-04-09 10:30 UTC (permalink / raw)
To: ltp
The domainname can be empty hence we cannot use scanf() string for
format for reading the file. Use the newly added SAFE_FILE_READ_STR()
to read the strings from procfs instead.
Fixes: https://github.com/linux-test-project/ltp/issues/1304
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
testcases/kernel/syscalls/newuname/newuname01.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/testcases/kernel/syscalls/newuname/newuname01.c b/testcases/kernel/syscalls/newuname/newuname01.c
index 37058023a..995ea978e 100644
--- a/testcases/kernel/syscalls/newuname/newuname01.c
+++ b/testcases/kernel/syscalls/newuname/newuname01.c
@@ -31,16 +31,16 @@ static void run(void)
TST_EXP_EQ_STR(name->sysname, "Linux");
- SAFE_FILE_SCANF("/proc/sys/kernel/hostname", "%1023[^\n]", proc_val);
+ SAFE_FILE_READ_STR("/proc/sys/kernel/hostname", proc_val, sizeof(proc_val));
TST_EXP_EQ_STR(name->nodename, proc_val);
- SAFE_FILE_SCANF("/proc/sys/kernel/osrelease", "%1023[^\n]", proc_val);
+ SAFE_FILE_READ_STR("/proc/sys/kernel/osrelease", proc_val, sizeof(proc_val));
TST_EXP_EQ_STR(name->release, proc_val);
- SAFE_FILE_SCANF("/proc/sys/kernel/version", "%1023[^\n]", proc_val);
+ SAFE_FILE_READ_STR("/proc/sys/kernel/version", proc_val, sizeof(proc_val));
TST_EXP_EQ_STR(name->version, proc_val);
- SAFE_FILE_SCANF("/proc/sys/kernel/domainname", "%1023[^\n]", proc_val);
+ SAFE_FILE_READ_STR("/proc/sys/kernel/domainname", proc_val, sizeof(proc_val));
TST_EXP_EQ_STR(name->domainname, proc_val);
}
--
2.52.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread