Skip to content

REFTABLE_REALLOC_ARRAY: fix potential memory leak if realloc failed #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brandb97
Copy link
Contributor

@brandb97 brandb97 commented May 8, 2025

REFTABLE_REALLOC_ARRAY doesn't free origin pointer when reftable_realloc failed. This leak can be fixed by add a free(x) before set x to NULL.

cc: René Scharfe [email protected]
cc: Patrick Steinhardt [email protected]

@brandb97
Copy link
Contributor Author

brandb97 commented May 8, 2025

/submit

Copy link

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v1

To fetch this version to local tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v1

Copy link

On the Git mailing list, René Scharfe wrote (reply to this):

Am 08.05.25 um 15:39 schrieb Lidong Yan via GitGitGadget:
> From: Lidong Yan <[email protected]>
> 
> REFTABLE_REALLOC_ARRAY doesn't free origin pointer when reftable_realloc
> failed. This leak can be fixed by add a free(x) before set x to NULL.

Hmm, this macro is unused.  Perhaps remove it?

René

Copy link

User René Scharfe <[email protected]> has been added to the cc: list.

Copy link

On the Git mailing list, lidongyan wrote (reply to this):

Ok, I will remove it in the next patch.

> 2025年5月9日 05:42,René Scharfe <[email protected]> 写道:
> 
> Am 08.05.25 um 15:39 schrieb Lidong Yan via GitGitGadget:
>> From: Lidong Yan <[email protected]>
>> 
>> REFTABLE_REALLOC_ARRAY doesn't free origin pointer when reftable_realloc
>> failed. This leak can be fixed by add a free(x) before set x to NULL.
> 
> Hmm, this macro is unused.  Perhaps remove it?
> 
> René
> 
> 

@brandb97 brandb97 force-pushed the fix-REFTABLE-REALLOC-leak branch from 4d786a0 to 6cc191f Compare May 9, 2025 02:01
@brandb97
Copy link
Contributor Author

brandb97 commented May 9, 2025

/submit

Copy link

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v2

To fetch this version to local tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v2

Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

René Scharfe <[email protected]> writes:

> Am 08.05.25 um 15:39 schrieb Lidong Yan via GitGitGadget:
>> From: Lidong Yan <[email protected]>
>> 
>> REFTABLE_REALLOC_ARRAY doesn't free origin pointer when reftable_realloc
>> failed. This leak can be fixed by add a free(x) before set x to NULL.
>
> Hmm, this macro is unused.  Perhaps remove it?

Or let the sleeping dog alone, perhaps?

Copy link

On the Git mailing list, Patrick Steinhardt wrote (reply to this):

On Fri, May 09, 2025 at 02:04:22AM +0000, Lidong Yan via GitGitGadget wrote:
> diff --git a/reftable/basics.h b/reftable/basics.h
> index d8888c12629..667feffd935 100644
> --- a/reftable/basics.h
> +++ b/reftable/basics.h
> @@ -199,16 +199,8 @@ static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)
>  			(x) = reftable_malloc(alloc_size); \
>  		} \
>  	} while (0)
> -#define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))
> -#define REFTABLE_REALLOC_ARRAY(x, alloc) do { \
> -		size_t alloc_size; \
> -		if (reftable_alloc_size(sizeof(*(x)), (alloc), &alloc_size) < 0) { \
> -			errno = ENOMEM; \
> -			(x) = NULL; \
> -		} else { \
> -			(x) = reftable_realloc((x), alloc_size); \
> -		} \
> -	} while (0)
> +#define REFTABLE_CALLOC_ARRAY(x, alloc) \
> +	(x) = reftable_calloc((alloc), sizeof(*(x)))

Let's avoid reformatting unrelated macros. But other than that I fully
agree -- we should remove stuff that we don't use in the first place.

Thanks!

Patrick

Copy link

User Patrick Steinhardt <[email protected]> has been added to the cc: list.

REFTABLE_REALLOC_ARRAY will cause memory leak if realloc failed.
Since it is unused, remove this unsafe macro.

Signed-off-by: Lidong Yan <[email protected]>
@brandb97 brandb97 force-pushed the fix-REFTABLE-REALLOC-leak branch from 6cc191f to 107f9ce Compare May 9, 2025 07:41
Copy link

On the Git mailing list, lidongyan wrote (reply to this):

2025年5月9日 14:51,Patrick Steinhardt <[email protected]> 写道:
> 
> On Fri, May 09, 2025 at 02:04:22AM +0000, Lidong Yan via GitGitGadget wrote:
>> diff --git a/reftable/basics.h b/reftable/basics.h
>> index d8888c12629..667feffd935 100644
>> --- a/reftable/basics.h
>> +++ b/reftable/basics.h
>> @@ -199,16 +199,8 @@ static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)
>> (x) = reftable_malloc(alloc_size); \
>> } \
>> } while (0)
>> -#define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))
>> -#define REFTABLE_REALLOC_ARRAY(x, alloc) do { \
>> - size_t alloc_size; \
>> - if (reftable_alloc_size(sizeof(*(x)), (alloc), &alloc_size) < 0) { \
>> - errno = ENOMEM; \
>> - (x) = NULL; \
>> - } else { \
>> - (x) = reftable_realloc((x), alloc_size); \
>> - } \
>> - } while (0)
>> +#define REFTABLE_CALLOC_ARRAY(x, alloc) \
>> + (x) = reftable_calloc((alloc), sizeof(*(x)))
> 
> Let's avoid reformatting unrelated macros. But other than that I fully
> agree -- we should remove stuff that we don't use in the first place.
> 
> Thanks!
> 
> Patrick
> 

Ok, I will restore REFTABLE_CALLOC_ARRAY in the next patch.

@brandb97
Copy link
Contributor Author

brandb97 commented May 9, 2025

/submit

Copy link

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v3

To fetch this version to local tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v3:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1955/brandb97/fix-REFTABLE-REALLOC-leak-v3

Copy link

On the Git mailing list, Patrick Steinhardt wrote (reply to this):

On Fri, May 09, 2025 at 07:44:46AM +0000, Lidong Yan via GitGitGadget wrote:
> From: Lidong Yan <[email protected]>
> 
> REFTABLE_REALLOC_ARRAY will cause memory leak if realloc failed.
> Since it is unused, remove this unsafe macro.

This looks good to me, thanks!

Patrick

Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

Patrick Steinhardt <[email protected]> writes:

> On Fri, May 09, 2025 at 07:44:46AM +0000, Lidong Yan via GitGitGadget wrote:
>> From: Lidong Yan <[email protected]>
>> 
>> REFTABLE_REALLOC_ARRAY will cause memory leak if realloc failed.
>> Since it is unused, remove this unsafe macro.
>
> This looks good to me, thanks!

Alright.  As long as we do not foresee adding a new caller of this
in the near future, removal is always preferred ;-)

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant