Skip to content

ASPRINTF macro does not set the output pointer to NULL on failure #115

Description

Received below comments in #112 from Copilot.

Comment 1:
port_str is built via ASPRINTF(&port_str, ...), but the ASPRINTF macro does not set the output pointer to NULL on failure and asprintf() leaves *strp undefined on error. On the getaddrinfo failure path you then free(port_str), which can free an invalid pointer if formatting failed (and getaddrinfo() would also be called with an undefined string). Use asprintf() directly and check its return value before calling getaddrinfo()/free().

Comment 2:
In the bind-failure path, the code appends errno to log after calling ASPRINTF, but asprintf() (and other libc calls) may modify errno. This can log the wrong error code. Also, ASPRINTF hides allocation failure, which makes log handling brittle. Capture errno immediately after bind() fails and use asprintf() return codes to manage log/old_log safely.

Comment 3:

Image

Issue

Code in logger.h:

#define ASPRINTF(...) { \
    int nc = asprintf(__VA_ARGS__); \
    if (nc < 0) \
        PRINT_ERR("error occurs in asprintf"); \
}
  • When asprintf() fails (nc < 0), it prints an error but continues execution, leaving port_str undefined.

The code then:

  • Passes undefined port_str to getaddrinfo()
  • Attempts to free(port_str) with an undefined pointe

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions