Skip to contentSkip to conversation

Fix proc parsing in newuname01

PATCH Superseded Cyril Hrubis 2 participants 8 messages Apr 9 – May 7 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix proc parsing in newuname01

Turns out some of the strings in procfs can be empty (file with just a newline) and in such case scanf("%s", ...) fails to read the file while what we needed instead is a function that would produce an empty string. This patchset addds such function and makes use of it in the newuname01 test.

Cyril Hrubis (2):
lib: safe_file_ops: Add SAFE_FILE_READ_STR()
syscalls: newuname01: Fix fail on empty domainname

 include/tst_safe_file_ops.h                   | 21 ++++++++++++++++
 lib/safe_file_ops.c                           | 24 +++++++++++++++++++
 .../kernel/syscalls/newuname/newuname01.c     |  8 +++----
 3 files changed, 49 insertions(+), 4 deletions(-)
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/2 lib: safe_file_ops: Add SAFE_FILE_READ_STR()

Unlike the scanf("%s") this function can properly handle empty files and in case of an empty file produces an empty string.

---
 include/tst_safe_file_ops.h | 21 +++++++++++++++++++++
 lib/safe_file_ops.c         | 24 ++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
include/tst_safe_file_ops.h +21 -0
index 0d8819594..d9110ba9c 100644
@@ -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__)
lib/safe_file_ops.c +24 -0
index 8314c4b1b..2d163c0af 100644
@@ -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
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH 1/2] lib: safe_file_ops: Add SAFE_FILE_READ_STR()

Hi!

Quoted 7 lines
lib/safe_file_ops.c +0 -0
index 8314c4b1b..2d163c0af 100644
@@ -160,6 +160,30 @@ void safe_file_scanf(const char *file, const int lineno,
 	}
 }

lib/safe_file_ops.c void safe_file_scanf(const char *file, const int lineno, · line 171

Quoted 10 lines
lib/safe_file_ops.c +9 -0
 
+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);

We need a return; here in an unlikely case that someone would call this from a test cleanup() callback.

Quoted 24 lines
lib/safe_file_ops.c +15 -0
+	}
+
+	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
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 2/2 syscalls: newuname01: Fix fail on empty domainname

In reply to Cyril Hrubis

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.

---
 testcases/kernel/syscalls/newuname/newuname01.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/newuname/newuname01.c +4 -4
index 37058023a..995ea978e 100644
@@ -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);
 }
 
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
In reply to Cyril Hrubis

Agent didn't send the following email:

--- Reply to [PATCH 1/2] ---

Quoted 1 line · Cyril Hrubis

lib: safe_file_ops: Add SAFE_FILE_READ_STR()

Hi Cyril,

Review

Quoted 7 lines
(quoted diff) +7 -0
+	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))

Missing return; after tst_brkm_() — f is NULL if fopen failed, so fgets() dereferences NULL. Every other error path in this file has return; after tst_brkm_().

Same issue in the fclose() error path below.

Review

Quoted 1 line
(quoted diff) +1 -0
+	size_t len = strlen(buf);

Variable declaration after a statement. Move len to the top of the function with FILE *f.

Quoted 1 line

+ * It's recomended to use this for various sysfs or procfs files that may be

s/recomended/recommended/

Regards,
LTP AI Reviewer

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi!

Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	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))

Missing return; after tst_brkm_() — f is NULL if fopen failed, so fgets() dereferences NULL. Every other error path in this file has return; after tst_brkm_().

Yes I'm aware, thanks for reminding me though.

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	size_t len = strlen(buf);

Variable declaration after a statement. Move len to the top of the function with FILE *f.

Do we stil follow the C89 in this regard? We already force gnu99 in the config.mk.

Quoted 11 lines
Quoted 1 line

+ * It's recomended to use this for various sysfs or procfs files that may be

s/recomended/recommended/

Regards,
LTP AI Reviewer

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi Cyril,

Quoted 15 lines

Hi!

Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+	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))

Missing return; after tst_brkm_() — f is NULL if fopen failed, so fgets() dereferences NULL. Every other error path in this file has return; after tst_brkm_().

Yes I'm aware, thanks for reminding me though.

Quoted 7 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	size_t len = strlen(buf);

Variable declaration after a statement. Move len to the top of the function with FILE *f.

Do we stil follow the C89 in this regard? We already force gnu99 in the config.mk.

It doesn't make any sense indeed. I added the c99 agent rule, but we told it to follow kernel linux conventions, which are specific, including this one.

We can safely ignore and see if it happens again with other reviews, and eventually update the configuration.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Hi!

Quoted 6 lines
Quoted 2 lines

Do we stil follow the C89 in this regard? We already force gnu99 in the config.mk.

It doesn't make any sense indeed. I added the c99 agent rule, but we told it to follow kernel linux conventions, which are specific, including this one.

I do not think that they enforce that rule these days. AFAIK kernel forces even newer c11 standard for some time.

Signature · 5 lines
Cyril Hrubis
[email protected]

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