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

[Coverity CID :215391] Unchecked return value from library in samples/net/mdns_responder/src/service.c #33093

Closed
zephyrbot opened this issue Mar 7, 2021 · 4 comments · Fixed by #33162
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug Coverity A Coverity detected issue or its fix priority: low Low impact/importance bug

Comments

@zephyrbot
Copy link
Collaborator

Static code scan issues found in file:

https:/zephyrproject-rtos/zephyr/tree/bd97359a5338b2542d19011b6d6aa1d8d1b9cc3f/samples/net/mdns_responder/src/service.c

Category: Error handling issues
Function: welcome
Component: Samples
CID: 215391

Details:

send(fd, msg, sizeof(msg), 0);

Please fix or provide comments in coverity using the link:

https://scan9.coverity.com/reports.htm#v32951/p12996.

Note: This issue was created automatically. Priority was set based on classification
of the file affected and the impact field in coverity. Assignees were set using the CODEOWNERS file.

@zephyrbot zephyrbot added bug The issue is a bug, or the PR is fixing a bug Coverity A Coverity detected issue or its fix priority: low Low impact/importance bug labels Mar 7, 2021
@gudnimg
Copy link
Contributor

gudnimg commented Mar 7, 2021

@pfalcon @jukkar @tbursztyka I see you are assigned to this, what do you think of my proposed solution below? 😃

I don't think a simple (void) cast is a good solution here.

From here I can see information about the return value: https://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html

Upon successful completion, send() shall return the number of bytes sent. Otherwise, -1 shall be returned and errno set to indicate the error.

So my solution would be something like this:

static void welcome(int fd)
{
    static const char msg[] = "Bonjour, Zephyr world!\n";

    int r = send(fd, msg, sizeof(msg), 0);
    if (r == -1) {
        NET_DBG("send() failed (%d)", errno);
        close(fd);
    }
}

I would also change the return type of welcome() from void to int so that we can terminate the process in service(). Where -1 indicates error, and 0 indicates success.

void service(void)
{
    // ... some code above ...
    /* send a banner */
    if(welcome(client_fd) == -1) {
        return;
    }
   // ... some code below ...
}
static int welcome(int fd)
{
    static const char msg[] = "Bonjour, Zephyr world!\n";

    int r = send(fd, msg, sizeof(msg), 0);

    if (r == -1) {
        NET_DBG("send() failed (%d)", errno);
        close(fd);
        return r;
    }

    return 0;
}

@jukkar
Copy link
Member

jukkar commented Mar 8, 2021

Hi @GunZi200, your proposal looks fine. Can you send a PR for it?

After looking the application, it tends to quit far too easy. So it works only once, and quits for any errors. It would need some more work to be more usable. Anyway, that is for another PR.

@jukkar
Copy link
Member

jukkar commented Mar 8, 2021

@GunZi200 If you are at it, could you also fix #33094 which leaks server_fd, by not closing it if there is an error in bind() or listen() in service.c. Thanks in advance!

@gudnimg
Copy link
Contributor

gudnimg commented Mar 8, 2021

Sure I’ll create a PR for both issues this evening :)

jukkar pushed a commit that referenced this issue Mar 9, 2021
Check return value if sendto() fails.

Coverity-CID: 215391
Fixes #33093

Signed-off-by: Guðni Már Gilbert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug Coverity A Coverity detected issue or its fix priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants