镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[Bug] simplex-talk server: addr_len passed to accept() is uninitialized (Undefined Behavior) #100

Open

Description

@pseudonymzjt

Description
In the simplex-talk server code (Chapter 1.4, Software), the variable addr_len is passed to accept() without being initialized. Reading this uninitialized variable is Undefined Behavior (UB) in C.

Location

· Release Version 6.1 PDF Page 36-37

Code Snippet

int buf_len, addr_len;
// ...
while(1) {
    if ((new_s = accept(s, (struct sockaddr *)&sin, &addr_len)) < 0) {
        perror("simplex-talk: accept");
        exit(1);
    }
    // ...
}

Issue
accept()'s third argument (addrlen) is a value-result parameter. As an input, it must be initialized to the size of the sockaddr buffer (sizeof(sin)). Passing an uninitialized int means the OS receives an indeterminate value as the buffer length. This is a latent bug that can cause accept() to fail (e.g., EINVAL) or lead to memory corruption depending on the stack state.

Suggested Fix
Initialize addr_len at declaration (or reset it before each accept() call if sin is reused):

int buf_len;
socklen_t addr_len = sizeof(sin); // Initialize here
// ...
while(1) {
    // Optional but recommended: addr_len = sizeof(sin);
    if ((new_s = accept(s, (struct sockaddr *)&sin, &addr_len)) < 0) {
    // ...
}

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