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

Make a separate struct Tox containing the Messenger. #1030

Merged
merged 1 commit into from
Aug 4, 2018

Conversation

iphydf
Copy link
Member

@iphydf iphydf commented Jul 25, 2018

This allows Tox to contain additional data on top of Messenger, making
Messenger not necessarily the most top-level object. E.g. groups are
built on Messenger and currently awkwardly void-pointered into it to
pretend there is no cyclic dependency.


This change is Reviewable

@iphydf iphydf added this to the v0.2.x milestone Jul 25, 2018
@hugbubby
Copy link
Member

hugbubby commented Jul 25, 2018

You accidentally set your shiftwidth to 2 or something when making this commit. The spacing is triggering Travis.

@iphydf
Copy link
Member Author

iphydf commented Jul 28, 2018

Not accidental, but this PR was also WIP, i.e. not ready for review.

@iphydf iphydf changed the title WIP: Make a separate struct Tox containing the Messenger. Make a separate struct Tox containing the Messenger. Jul 28, 2018
@iphydf iphydf force-pushed the struct-tox branch 7 times, most recently from 7953bfd to 2d7f792 Compare July 28, 2018 17:17
Copy link

@sudden6 sudden6 left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 5 files at r1.
Reviewable status: 0 of 2 LGTMs obtained (waiting on @hugbubby and @sudden6)


toxcore/tox.c, line 344 at r1 (raw file):

            case TOX_ERR_OPTIONS_NEW_MALLOC:
                SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
                free(tox);

since Tox has pointer members, I'd prefer a free_tox function instead of free


toxcore/tox.c, line 446 at r1 (raw file):

    unsigned int m_error;
    Messenger *const m = new_messenger(&m_options, &m_error);

Can new_messenger fail and return a nullptr or an invalid pointer? If so consequences would be bad. Check/assert it's valid?

Copy link
Member Author

@iphydf iphydf left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 LGTMs obtained (waiting on @hugbubby and @sudden6)


toxcore/tox.c, line 344 at r1 (raw file):

Previously, sudden6 wrote…

since Tox has pointer members, I'd prefer a free_tox function instead of free

That would be asymmetric. We create tox using calloc, so we free it using free. tox_new is the constructor function for Tox, so it should deal with appropriately freeing incomplete/inconsistent instances on error.

Fun fact: the DHT module does this the other way around, relying on kill_dht to clean up, but kill_dht doesn't know how to deal with incomplete instances, so does it wrong, causing a null pointer dereference on one of the error paths (feel free to go find the path I'm talking about, but don't fix it, this is something we should fix after having a test for it).


toxcore/tox.c, line 446 at r1 (raw file):

Previously, sudden6 wrote…

Can new_messenger fail and return a nullptr or an invalid pointer? If so consequences would be bad. Check/assert it's valid?

It can not return an invalid non-null pointer. There would also be no way to find out in standard C (there is, on Linux). It can return nullptr, which will in turn make new_groupchats return nullptr, which will enter the error path below. Clarifying this code is out of scope for this PR.

Copy link
Member Author

@iphydf iphydf left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 LGTMs obtained (waiting on @hugbubby and @sudden6)


toxcore/tox.c, line 344 at r1 (raw file):

Previously, iphydf wrote…

That would be asymmetric. We create tox using calloc, so we free it using free. tox_new is the constructor function for Tox, so it should deal with appropriately freeing incomplete/inconsistent instances on error.

Fun fact: the DHT module does this the other way around, relying on kill_dht to clean up, but kill_dht doesn't know how to deal with incomplete instances, so does it wrong, causing a null pointer dereference on one of the error paths (feel free to go find the path I'm talking about, but don't fix it, this is something we should fix after having a test for it).

I'm not strongly against teaching all the destructor functions how to deal with incomplete instances and then using them for all error paths, but that should be a deliberate, documented, global, and ideally tested change in toxcore.

Copy link

@sudden6 sudden6 left a comment

Choose a reason for hiding this comment

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

:lgtm_strong:

Reviewed 1 of 5 files at r1.
Reviewable status: 1 of 2 LGTMs obtained (waiting on @hugbubby)


toxcore/tox.c, line 344 at r1 (raw file):

Previously, iphydf wrote…

I'm not strongly against teaching all the destructor functions how to deal with incomplete instances and then using them for all error paths, but that should be a deliberate, documented, global, and ideally tested change in toxcore.

Ok, I see you have thought this through a bit more than me. I think using the destructor functions on error paths in the constructor is a good idea, since then we have to handle an incomplete type at exactly one place. Needs the precautions you mentioned though.


toxcore/tox.c, line 446 at r1 (raw file):

Previously, iphydf wrote…

It can not return an invalid non-null pointer. There would also be no way to find out in standard C (there is, on Linux). It can return nullptr, which will in turn make new_groupchats return nullptr, which will enter the error path below. Clarifying this code is out of scope for this PR.

ok, can you then maybe put a TODO or similar here to check for non-nullptr?

This allows Tox to contain additional data on top of Messenger, making
Messenger not necessarily the most top-level object. E.g. groups are
built on Messenger and currently awkwardly void-pointered into it to
pretend there is no cyclic dependency.
@iphydf iphydf merged commit 064ffe5 into TokTok:master Aug 4, 2018
@iphydf iphydf deleted the struct-tox branch August 4, 2018 09:45
@iphydf iphydf modified the milestones: v0.2.x, v0.2.5 Aug 4, 2018
This pull request was closed.
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.

3 participants