Skip to content

Commit

Permalink
selftests/resctrl: Fix null pointer dereference on open failed
Browse files Browse the repository at this point in the history
[ Upstream commit c7b607f ]

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: a2561b1 ("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>
  • Loading branch information
ColinIanKing authored and gregkh committed Jun 9, 2022
1 parent c54d66c commit 97b56f1
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tools/testing/selftests/resctrl/fill_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,

/* Consume read result so that reading memory is not optimized out. */
fp = fopen("/dev/null", "w");
if (!fp)
if (!fp) {
perror("Unable to write to /dev/null");
return -1;
}
fprintf(fp, "Sum: %d ", ret);
fclose(fp);

Expand Down

0 comments on commit 97b56f1

Please sign in to comment.