selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
Subsystems (3)
PATCH v2 1/1 selftests/mm: mlock2-tests: fix fclose(NULL) in is_vmflag_set()
is_vmflag_set() jumps to the shared 'out' label when seek_to_smaps_entry() fails. smaps is NULL on that path, and the code at the 'out' label calls fclose(smaps) unconditionally. fclose(NULL) is undefined behaviour, so the test crashes before it can report a test result.
Return early on that error path instead, so smaps is always valid at the 'out' label.
get_value_for_name() follows the same pattern. Return early on the seek_to_smaps_entry() error path there as well, and drop the now redundant 'if (smaps)' guard on the fclose().
---
Changes in v2:
- Convert get_value_for_name() in the same way, as suggested by Liam R.Howlett.
- Update the commit message.
--- tools/testing/selftests/mm/mlock2-tests.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/mlock2-tests.c +3 -4
@@ -66,7 +66,7 @@ static bool is_vmflag_set(unsigned long addr, const char *vmflag) smaps =seek_to_smaps_entry (addr);if (!smaps) {ksft_print_msg ("Unable to parse /proc/self/smaps\n" ); - goto out; +return false; }while (getline (&line, &size, smaps) >0 ) {
@@ -103,7 +103,7 @@ static unsigned long get_value_for_name(unsigned long addr, const char *name) smaps =seek_to_smaps_entry (addr);if (!smaps) {ksft_print_msg ("Unable to parse /proc/self/smaps\n" ); - goto out; +return value; }while (getline (&line, &size, smaps) >0 ) {
@@ -123,9 +123,8 @@ static unsigned long get_value_for_name(unsigned long addr, const char *name) } out: -if (smaps) -fclose (smaps);free (line); +fclose (smaps);return value; }
---
base-commit: 74d73ffcffe03804f97e9fb82301dadf75671282
change-id: 20261104-for-mlock2-a67f86dad498
Best regards,
Signature · 1 line
Hongfu Li <[email protected]>
Quoted 5 lines · Hongfu Li
is_vmflag_set()jumps to the shared 'out' label whenseek_to_smaps_entry()fails. smaps is NULL on that path, and the code at the 'out' label calls fclose(smaps) unconditionally. fclose(NULL) is undefined behaviour, so the test crashes before it can report a test result.
I was briefly confuses staring at get_value_for_name() diff below. WHich is clarified below.
Quoted 9 lines
Return early on that error path instead, so smaps is always valid at the 'out' label.
get_value_for_name()follows the same pattern. Return early on theseek_to_smaps_entry()error path there as well, and drop the now redundant 'if (smaps)' guard on thefclose().Signed-off-by Hongfu Li <[email protected]>
Apart form that LGTM
Signature · 3 lines
Cheers, David
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 5 lines · Hongfu Li
is_vmflag_set()jumps to the shared 'out' label whenseek_to_smaps_entry()fails. smaps is NULL on that path, and the code at the 'out' label calls fclose(smaps) unconditionally. fclose(NULL) is undefined behaviour, so the test crashes before it can report a test result.I was briefly confuses staring at
get_value_for_name()diff below. WHich is clarified below.Quoted 8 lines
Return early on that error path instead, so smaps is always valid at the 'out' label.
get_value_for_name()follows the same pattern. Return early on theseek_to_smaps_entry()error path there as well, and drop the now redundant 'if (smaps)' guard on thefclose().
Quoted 7 lines
Quoted 1 line
Signed-off-by Hongfu Li <[email protected]>Fixes tag? :)Apart form that LGTM
Reviewed-by David Hildenbrand (Arm) <[email protected]>
Similarly,
Quoted 5 lines
--
Cheers,David
--
Cheers, Lorenzo
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/mm: missing Fixes trailer for fclose(NULL) fix in is_vmflag_set() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/mm: missing Fixes trailer for fclose(NULL) fix in is_vmflag_set()
--
commit 38a7fefcf9bd0b1ed77197eeaab78f5845d74dfd
Author: Hongfu Li <[email protected]>
Quoted 8 lines
selftests/mm: mlock2-tests: fix fclose(NULL) in
is_vmflag_set()
is_vmflag_set()jumps to the shared 'out' label whenseek_to_smaps_entry()fails. smaps is NULL on that path, and the code at the 'out' label calls fclose(smaps) unconditionally. fclose(NULL) is undefined behaviour, so the test crashes before it can report a test result.
Quoted 8 lines
Return early on that error path instead, so smaps is always valid at the 'out' label.
get_value_for_name()follows the same pattern. Return early on theseek_to_smaps_entry()error path there as well, and drop the now redundant 'if (smaps)' guard on thefclose().Signed-off-by Hongfu Li <[email protected]>
[Severity: Low]
Since this fixes the fclose(NULL) crash in is_vmflag_set(), should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Andrew Morton
maintainerQuoted 4 lines · On Sat, 14 Nov 2026 02:53:30 +0000 [email protected] wrote:
Since this fixes the fclose(NULL) crash in
is_vmflag_set(), should this include:Fixes b3b0d09c7a23 ("selftests: vm: add tests for lock on fault")
Yes. Added.