selftests/resctrl: Fix null pointer dereference on open failed
authorColin Ian King <colin.i.king@gmail.com>
Tue, 26 Apr 2022 12:25:31 +0000 (13:25 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Jun 2022 08:22:51 +0000 (10:22 +0200)
commit2324985aa700457738fe737216140fb4f3d3f14a
treedc8ded37179615f2ed7871884e325443df6028e8
parent4be045434923e549a50846a066a04b7b6c1d6d33
selftests/resctrl: Fix null pointer dereference on open failed

[ Upstream commit c7b607fa9325ccc94982774c505176677117689c ]

Currently if opening /dev/null fails to open then file pointer fp
is null and further access to fp via fprintf will cause a null
pointer dereference. Fix this by returning a negative error value
when a null fp is detected.

Detected using cppcheck static analysis:
tools/testing/selftests/resctrl/fill_buf.c:124:6: note: Assuming
that condition '!fp' is not redundant
 if (!fp)
     ^
tools/testing/selftests/resctrl/fill_buf.c:126:10: note: Null
pointer dereference
 fprintf(fp, "Sum: %d ", ret);

Fixes: a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/testing/selftests/resctrl/fill_buf.c