Skip to content

Commit b467289

Browse files
committed
fix potential double free
double free could occur if patch allocated the dst_data, then an error occured later on, causing the jump to fail, which frees the dst_data. however because dst_data is a **, then the user will have a non-null pointer so they may assume it should be freed. patch will always free and NULL the dst_data (if allocated) on error. only on success will dst_data remain allocated.
1 parent 530b18d commit b467289

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

patch.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66

77
enum PatchError patch(
8-
enum PatchType type,
8+
const enum PatchType type,
99
uint8_t** dst_data, size_t* dst_size,
10-
const uint8_t* src_data, size_t src_size,
11-
const uint8_t* patch_data, size_t patch_size
10+
const uint8_t* src_data, const size_t src_size,
11+
const uint8_t* patch_data, const size_t patch_size
1212
) {
1313
if (!dst_data || !dst_size || !src_data || !src_size || !patch_data || !patch_size)
1414
{
@@ -76,6 +76,7 @@ enum PatchError patch(
7676
if (*dst_data)
7777
{
7878
free(*dst_data);
79+
*dst_data = NULL;
7980
}
8081

8182
return error;

patch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ enum PatchError
2525
PatchError_PATCH,
2626
};
2727

28+
// dst_data will be allocated by this function using malloc.
29+
// dst_data will not be allocated if patch returns an error.
2830
enum PatchError patch(
2931
enum PatchType type,
3032
uint8_t** dst_data, size_t* dst_size,

0 commit comments

Comments
 (0)