Skip to content

Commit 07e0f1c

Browse files
weihsinyehidoleat
andcommitted
Avoid memory leak in the bbp function
1. When allocating memory for the product, if the allocation fails, it returns NULL. Co-authored-by: Chih-Wei Chien <[email protected]> Signed-off-by: Wei-Hsin Yeh <[email protected]>
1 parent 597a90d commit 07e0f1c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

examples/rmw_example.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ static void *bbp(void *arg)
162162
double sum = (4.0 / (8 * k + 1)) - (2.0 / (8 * k + 4)) -
163163
(1.0 / (8 * k + 5)) - (1.0 / (8 * k + 6));
164164
double *product = malloc(sizeof(double));
165-
if (product)
166-
*product = 1 / pow(16, k) * sum;
165+
if (!product)
166+
return NULL;
167+
168+
*product = 1 / pow(16, k) * sum;
167169
return (void *)product;
168170
}
169171

0 commit comments

Comments
 (0)