media: v4l2-tpg: fix some memleaks in tpg_alloc
[ Upstream commit 8cf9c5051076e0eb958f4361d50d8b0c3ee6691c ]
In tpg_alloc, resources should be deallocated in each and every
error-handling paths, since they are allocated in for statements.
Otherwise there would be memleaks because tpg_free is called only when
tpg_alloc return 0.
Fixes: 63881df94d ("[media] vivid: add the Test Pattern Generator")
Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit 0de691ff547d86dd54c24b40a81f9c925df8dd77)
[Harshit: fix conflict due to missing commit: fad953ce0b22 ("treewide:
Use array_size() in vzalloc()") in 4.14.y]
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
This commit is contained in:
committed by
Harshit Mogalapalli
parent
e2bc2e2b68
commit
fdec9e17f5
@@ -125,6 +125,7 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w)
|
||||
{
|
||||
unsigned pat;
|
||||
unsigned plane;
|
||||
int ret = 0;
|
||||
|
||||
tpg->max_line_width = max_w;
|
||||
for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++) {
|
||||
@@ -132,29 +133,60 @@ int tpg_alloc(struct tpg_data *tpg, unsigned max_w)
|
||||
unsigned pixelsz = plane ? 2 : 4;
|
||||
|
||||
tpg->lines[pat][plane] = vzalloc(max_w * 2 * pixelsz);
|
||||
if (!tpg->lines[pat][plane])
|
||||
return -ENOMEM;
|
||||
if (!tpg->lines[pat][plane]) {
|
||||
ret = -ENOMEM;
|
||||
goto free_lines;
|
||||
}
|
||||
if (plane == 0)
|
||||
continue;
|
||||
tpg->downsampled_lines[pat][plane] = vzalloc(max_w * 2 * pixelsz);
|
||||
if (!tpg->downsampled_lines[pat][plane])
|
||||
return -ENOMEM;
|
||||
if (!tpg->downsampled_lines[pat][plane]) {
|
||||
ret = -ENOMEM;
|
||||
goto free_lines;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
|
||||
unsigned pixelsz = plane ? 2 : 4;
|
||||
|
||||
tpg->contrast_line[plane] = vzalloc(max_w * pixelsz);
|
||||
if (!tpg->contrast_line[plane])
|
||||
return -ENOMEM;
|
||||
if (!tpg->contrast_line[plane]) {
|
||||
ret = -ENOMEM;
|
||||
goto free_contrast_line;
|
||||
}
|
||||
tpg->black_line[plane] = vzalloc(max_w * pixelsz);
|
||||
if (!tpg->black_line[plane])
|
||||
return -ENOMEM;
|
||||
if (!tpg->black_line[plane]) {
|
||||
ret = -ENOMEM;
|
||||
goto free_contrast_line;
|
||||
}
|
||||
tpg->random_line[plane] = vzalloc(max_w * 2 * pixelsz);
|
||||
if (!tpg->random_line[plane])
|
||||
return -ENOMEM;
|
||||
if (!tpg->random_line[plane]) {
|
||||
ret = -ENOMEM;
|
||||
goto free_contrast_line;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
free_contrast_line:
|
||||
for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
|
||||
vfree(tpg->contrast_line[plane]);
|
||||
vfree(tpg->black_line[plane]);
|
||||
vfree(tpg->random_line[plane]);
|
||||
tpg->contrast_line[plane] = NULL;
|
||||
tpg->black_line[plane] = NULL;
|
||||
tpg->random_line[plane] = NULL;
|
||||
}
|
||||
free_lines:
|
||||
for (pat = 0; pat < TPG_MAX_PAT_LINES; pat++)
|
||||
for (plane = 0; plane < TPG_MAX_PLANES; plane++) {
|
||||
vfree(tpg->lines[pat][plane]);
|
||||
tpg->lines[pat][plane] = NULL;
|
||||
if (plane == 0)
|
||||
continue;
|
||||
vfree(tpg->downsampled_lines[pat][plane]);
|
||||
tpg->downsampled_lines[pat][plane] = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(tpg_alloc);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user