Skip to content
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

Fix a memory leak in hstox interface #277

Merged
merged 1 commit into from
Nov 21, 2016
Merged

Conversation

nurupo
Copy link
Member

@nurupo nurupo commented Nov 15, 2016

transformed is leaking.


This change is Reviewable

@nurupo nurupo added the bug Bug fix for the user, not a fix to a build script label Nov 15, 2016
@nurupo nurupo added this to the v0.0.5 milestone Nov 15, 2016
@iphydf
Copy link
Member

iphydf commented Nov 15, 2016

Reviewed 1 of 1 files at r1.
Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


testing/hstox/methods.c, line 127 at r1 (raw file):

int method_cmp(char const *ptr, char const *expected, size_t max_size)
{
    char *transformed = malloc(max_size);

Use __attribute__((__destructor__)) here, instead. We're using that in other places. Yes, it's a GNU extension, but this code has a very specific use-case that is not supposed to be portable.


Comments from Reviewable

@nurupo
Copy link
Member Author

nurupo commented Nov 20, 2016

Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


testing/hstox/methods.c, line 127 at r1 (raw file):
I'm confused.

We don't use constuctor and destructor attributes anywhere in toxcore, unless grep lies to me.

Also, are you sure you want to use function attribute destructor here? Do you know what it does?

Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called.

A function attributed as destructor is called only once, when the program exits. transformed is allocated every single time method_cmp() is called, so free()'ing it only once won't work. I would need to pull transformed out of the function scope and create an appropriate constructor for it, like so:

I would need to create a functions

#define MAX_SIZE ???
static char *transformed;

static __attribute__((constructor)) void allocate_transformed(void)
{
    transformed = malloc(MAX_SIZE);
}

static __attribute__((destructor)) void deallocate_transformed(void)
{
    free(transformed);
}

int method_cmp(char const *ptr, char const *expected, size_t max_size)
{
...

Is this really better than my proposed change?


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Nov 20, 2016

Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


testing/hstox/methods.c, line 127 at r1 (raw file):

Previously, nurupo wrote…

I'm confused.

We don't use constuctor and destructor attributes anywhere in toxcore, unless grep lies to me.

Also, are you sure you want to use function attribute destructor here? Do you know what it does?

Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called.

A function attributed as destructor is called only once, when the program exits. transformed is allocated every single time method_cmp() is called, so free()'ing it only once won't work. I would need to pull transformed out of the function scope and create an appropriate constructor for it, like so:

I would need to create a functions

#define MAX_SIZE ???
static char *transformed;

static __attribute__((constructor)) void allocate_transformed(void)
{
    transformed = malloc(MAX_SIZE);
}

static __attribute__((destructor)) void deallocate_transformed(void)
{
    free(transformed);
}

int method_cmp(char const *ptr, char const *expected, size_t max_size)
{
...

Is this really better than my proposed change?

Sorry, I meant `cleanup`.

Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Nov 21, 2016

Never mind the cleanup. This is fine.

@nurupo
Copy link
Member Author

nurupo commented Nov 21, 2016

If you say so. Was about to add the cleanup function which would wrap around free().

@iphydf iphydf changed the title Fix a memory leak in toxhs Fix a memory leak in hstox interface Nov 21, 2016
@iphydf
Copy link
Member

iphydf commented Nov 21, 2016

:lgtm_strong:


Review status: all files reviewed at latest revision, 1 unresolved discussion.


Comments from Reviewable

@iphydf iphydf merged commit a403c99 into TokTok:master Nov 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fix for the user, not a fix to a build script
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants