Fix proc parsing in newuname01
v2 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.
v2:
- added returns to safe_file_read_str()
- fixed typo in doc comment
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 | 26 +++++++++++++++++++ .../kernel/syscalls/newuname/newuname01.c | 8 +++--- 3 files changed, 51 insertions(+), 4 deletions(-)
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 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 | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
include/tst_safe_file_ops.h +21 -0
@@ -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 recommended 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 +26 -0
@@ -160,6 +160,32 @@ 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); +return ; + } + +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); +return ; + } +}/* * Try to parse each line from file specified by 'path' according
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] lib: safe_file_ops: Add SAFE_FILE_READ_STR()
Hi Cyril,
--- [PATCH 1/2] ---
lib/safe_file_ops.c void safe_file_scanf(const char *file, const int lineno, · line 178
Quoted 6 lines · Cyril Hrubis
lib:
safe_file_ops: AddSAFE_FILE_READ_STR()lib/safe_file_ops.c +4
-0+if (!fgets (buf, buf_size, f)) + buf[0 ] =0 ; + +size_t len =strlen (buf);
Move size_t len to the top of the function alongside FILE *f; mixed declarations after executable statements violate kernel coding style.
--- [PATCH 2/2] ---
Quoted 3 lines · Cyril Hrubis
syscalls: newuname01: Fix fail on empty domainname
Fixes: must reference a git commit hash in the form Fixes: <hash> ("<subject>"). Use Closes: to reference GitHub issues.
---
Note:
Our agent completed the review of the patch.
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] lib: safe_file_ops: Add SAFE_FILE_READ_STR()
Hi,
Quoted 7 lines
Hi Cyril,
--- [PATCH 1/2] ---
Quoted 1 line · Cyril Hrubis
lib:
safe_file_ops: AddSAFE_FILE_READ_STR()
Quoted 7 lines
lib/safe_file_ops.c void safe_file_scanf(const char *file, const int lineno, · line 178
Quoted 4 lines
lib/safe_file_ops.c +4
-0+if (!fgets (buf, buf_size, f)) + buf[0 ] =0 ; + +size_t len =strlen (buf);Move
size_t lento the top of the function alongsideFILE *f; mixed declarations after executable statements violate kernel coding style.
I see this is happening again. I'm gonna add a rule for it.
Quoted 11 lines
--- [PATCH 2/2] ---
Quoted 3 lines · Cyril Hrubis
syscalls: newuname01: Fix fail on empty domainname
Fixes:must reference a git commit hash in the formFixes: <hash> ("<subject>"). UseCloses:to reference GitHub issues.
This is actually correct. I will change the Fixes to Closes before merge.
Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 1/2] lib: safe_file_ops: Add SAFE_FILE_READ_STR()
In reply to Cyril Hrubis--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 2/2 syscalls: newuname01: Fix fail on empty domainname
In reply to Cyril HrubisThe 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
@@ -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.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 2/2] syscalls: newuname01: Fix fail on empty domainname
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Andrea Cervesato via ltp
AppliedMerged, Thanks!
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp