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 potential Null pointer dereference in test-fdleak.c #1610

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

Conversation

icy17
Copy link
Contributor

@icy17 icy17 commented Apr 10, 2024

No description provided.

@@ -111,6 +111,9 @@ listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock,
struct event_base *base = evconnlistener_get_base(listener);
struct bufferevent *bev = bufferevent_socket_new(base, sock,
BEV_OPT_CLOSE_ON_FREE);
if (bev == NULL) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

fd leaked here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'll fix it

@@ -209,6 +212,10 @@ start_client(struct event_base *base)
{
struct bufferevent *bev = bufferevent_socket_new(base, -1,
BEV_OPT_CLOSE_ON_FREE);
if (bev == NULL) {
Copy link
Member

Choose a reason for hiding this comment

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

I wish that the main would have a normal error path, i.e.:

if (!start_client)
    goto cleanup;
...
cleanup:
    if (listener)
        evconnlistener_free(listener);
    if (base)
        event_base_free(base);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, thanks for the example

@icy17
Copy link
Contributor Author

icy17 commented Apr 22, 2024

I previously misunderstood; while modifying the code, I noticed that I hadn't altered the main function, so perhaps adding a cleanup path is unnecessary? Also, regarding the 'fd leaked' issue, there seems to be no file descriptor here. I believe there are no newly allocated variables within this function, thus no need for cleanup. If I've overlooked something, please highlight it for me.

@azat
Copy link
Member

azat commented Apr 29, 2024

I noticed that I hadn't altered the main function, so perhaps adding a cleanup path is unnecessary

You didn't, but you modified start_client, and main calls it (start_loop -> start_client), so if it can fail it should propagate error to main, since the whole point of this PR to make avoid any leaks on exit AFAIU

Also, regarding the 'fd leaked' issue, there seems to be no file descriptor here

listener_accept_cb accepts socket, if the bufferevent fails you need to close the socket

@icy17
Copy link
Contributor Author

icy17 commented Apr 29, 2024

You didn't, but you modified start_client, and main calls it (start_loop -> start_client), so if it can fail it should propagate error to main, since the whole point of this PR to make avoid any leaks on exit AFAIU

Perhaps I should handle the cleanup in start_loop not in main? Also, I’m not sure whether to change the exit to return in start_client. By doing so, resources could be properly released if start_client fails.

@azat
Copy link
Member

azat commented Apr 29, 2024

Perhaps I should handle the cleanup in start_loop not in main?

This is OK

Also, I’m not sure whether to change the exit to return in start_client. By doing so, resources could be properly released if start_client fails.

Yes, you should not call exit from start_client

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

Successfully merging this pull request may close these issues.

None yet

2 participants