Skip to content

Memory leak from getaddrinfo() function #1

Description

@kurz-m

Nice work Azer!

However, you are leaking memory from this line, because of getaddrinfo():

if (getaddrinfo(argv[1], NULL, &hints, &res) != 0) {

From the man page:

The getaddrinfo() function allocates and initializes a linked list of addrinfo structures, one for each network address that matches node and service, subject to any restrictions imposed by hints, and returns a pointer to the start of the list in res. The items in the linked list are linked by the ai_next field.

Fix:
Call freeaddrinfo() on error and before exiting the process.

The freeaddrinfo() function frees the memory that was allocated for the dynamically allocated linked list res.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions