提交 d220a7d5 编写于 作者: C Colin Ian King 提交者: Zheng Zengkai

selftests/resctrl: Fix null pointer dereference on open failed

stable inclusion
from stable-v5.10.121
commit 97b56f17b355ddeb6558b2ba2ad8a40704b229e9
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5L6CQ

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=97b56f17b355ddeb6558b2ba2ad8a40704b229e9

--------------------------------

[ Upstream commit c7b607fa ]

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: a2561b12 ("selftests/resctrl: Add built in benchmark")
Signed-off-by: NColin Ian King <colin.i.king@gmail.com>
Signed-off-by: NShuah Khan <skhan@linuxfoundation.org>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
Acked-by: NXie XiuQi <xiexiuqi@huawei.com>
上级 f17f46e4
...@@ -121,8 +121,10 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr, ...@@ -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. */ /* Consume read result so that reading memory is not optimized out. */
fp = fopen("/dev/null", "w"); fp = fopen("/dev/null", "w");
if (!fp) if (!fp) {
perror("Unable to write to /dev/null"); perror("Unable to write to /dev/null");
return -1;
}
fprintf(fp, "Sum: %d ", ret); fprintf(fp, "Sum: %d ", ret);
fclose(fp); fclose(fp);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册