net/accept: alloc the accept fd after accept success

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2021-06-11 12:24:37 +08:00 committed by Xiang Xiao
parent 0d8e5b66fd
commit b6bf9cf44b
1 changed files with 10 additions and 10 deletions

View File

@ -267,6 +267,13 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
goto errout; goto errout;
} }
ret = psock_accept(psock, addr, addrlen, newsock);
if (ret < 0)
{
errcode = -ret;
goto errout_with_alloc;
}
/* Allocate a socket descriptor for the new connection now (so that it /* Allocate a socket descriptor for the new connection now (so that it
* cannot fail later) * cannot fail later)
*/ */
@ -275,21 +282,14 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
if (newfd < 0) if (newfd < 0)
{ {
errcode = ENFILE; errcode = ENFILE;
goto errout_with_alloc; goto errout_with_psock;
}
ret = psock_accept(psock, addr, addrlen, newsock);
if (ret < 0)
{
errcode = -ret;
goto errout_with_socket;
} }
leave_cancellation_point(); leave_cancellation_point();
return newfd; return newfd;
errout_with_socket: errout_with_psock:
nx_close(newfd); psock_close(newsock);
errout_with_alloc: errout_with_alloc:
kmm_free(newsock); kmm_free(newsock);