8000 [fix] Add check on failed allocation in legacy/zstd_v06 by Adenilson · Pull Request #4050 · facebook/zstd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[fix] Add check on failed allocation in legacy/zstd_v06 #4050

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

Merged
merged 1 commit into from
May 21, 2024

Conversation

Adenilson
Copy link

As reported by Ben Hawkes in #4026, a failure to allocate a zstd context would lead to a dereference of a NULL pointer due to a missing check on the returned result of ZSTDv06_createDCtx().

This patch fix the issue by adding a check for valid returned pointer.

@Adenilson
Copy link
Author

I'm trying to follow the same coding style used in:
https://github.com/facebook/zstd/blob/dev/lib/legacy/zstd_v06.c#L3919

@Adenilson Adenilson force-pushed the fix_legacy_nullptr01 branch from 4f71e74 to 30546c1 Compare May 17, 2024 20:48
@@ -3919,6 +3919,7 @@ ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void)
if (zbd==NULL) return NULL;
memset(zbd, 0, sizeof(*zbd));
zbd->zd = ZSTDv06_createDCtx();
if (zbd->zd==NULL) return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning immediately here would result in a memory leak, because zbd has been allocated at that point.
Free zbd, using ZBUFFv06_freeDCtx(), before returning.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed. Thanks a lot for the review, I will fix that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Adenilson Adenilson force-pushed the fix_legacy_nullptr01 branch from 30546c1 to 0e37337 Compare May 17, 2024 22:37
@@ -3919,6 +3919,7 @@ ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void)
if (zbd==NULL) return NULL;
memset(zbd, 0, sizeof(*zbd));
zbd->zd = ZSTDv06_createDCtx();
if (zbd->zd == NULL) return NULL;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty spaces around ==.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -3919,6 +3919,7 @@ ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void)
if (zbd==NULL) return NULL;
memset(zbd, 0, sizeof(*zbd));
zbd->zd = ZSTDv06_createDCtx();
if (zbd->zd==NULL) return NULL;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed. Thanks a lot for the review, I will fix that.

@@ -3919,6 +3919,7 @@ ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void)
if (zbd==NULL) return NULL;
memset(zbd, 0, sizeof(*zbd));
zbd->zd = ZSTDv06_createDCtx();
< 8000 /td> if (zbd->zd==NULL) return NULL;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

As reported by Ben Hawkes in facebook#4026, a failure to allocate a zstd context
would lead to a dereference of a NULL pointer due to a missing check
on the returned result of ZSTDv06_createDCtx().

This patch fix the issue by adding a check for valid returned pointer.
@Adenilson Adenilson force-pushed the fix_legacy_nullptr01 branch from 0e37337 to 1872688 Compare May 17, 2024 22:40
@Cyan4973 Cyan4973 self-assigned this May 18, 2024
@adenilsoncavalcanti
Copy link

@Cyan4973 anything else you like to see changed in the patch?
:-)

@Cyan4973
Copy link
Contributor

Nope, it's good!

@Cyan4973 Cyan4973 merged commit 0e2ceb2 into facebook:dev May 21, 2024
94 checks passed
@Adenilson Adenilson deleted the fix_legacy_nullptr01 branch June 19, 2024 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0