Merge pull request #1771 from Lawlieta/chenyong

[net][sal] Fix close/shutdown memory leak issue.
This commit is contained in:
Bernard Xiong 2018-09-05 11:18:51 +08:00 committed by GitHub
commit 6abe7e4ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 12 deletions

View File

@ -35,6 +35,8 @@
#define DBG_COLOR #define DBG_COLOR
#include <rtdbg.h> #include <rtdbg.h>
#define SOCKET_TABLE_STEP_LEN 4
/* the socket table used to dynamic allocate sockets */ /* the socket table used to dynamic allocate sockets */
struct sal_socket_table struct sal_socket_table
{ {
@ -52,19 +54,29 @@ static rt_bool_t init_ok = RT_FALSE;
/** /**
* SAL (Socket Abstraction Layer) initialize. * SAL (Socket Abstraction Layer) initialize.
* *
* @return result * @return result 0: initialize success
* >= 0: initialize success * -1: initialize failed
*/ */
int sal_init(void) int sal_init(void)
{ {
int cn;
if(init_ok) if(init_ok)
{ {
LOG_D("Socket Abstraction Layer is already initialized."); LOG_D("Socket Abstraction Layer is already initialized.");
return 0; return 0;
} }
/* clean sal socket table */ /* init sal socket table */
rt_memset(&socket_table, 0, sizeof(socket_table)); cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
socket_table.max_socket = cn;
socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
if (socket_table.sockets == RT_NULL)
{
LOG_E("No memory for socket table.\n");
return -1;
}
/* create sal socket lock */ /* create sal socket lock */
rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO); rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
@ -80,7 +92,7 @@ INIT_COMPONENT_EXPORT(sal_init);
* *
* @param pf protocol family object * @param pf protocol family object
* *
* @return >=0 : protocol family object index * @return 0 : protocol family object register success
* -1 : the global array of available protocol families is full * -1 : the global array of available protocol families is full
*/ */
int sal_proto_family_register(const struct proto_family *pf) int sal_proto_family_register(const struct proto_family *pf)
@ -331,8 +343,8 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
int cnt, index; int cnt, index;
struct sal_socket **sockets; struct sal_socket **sockets;
/* increase the number of FD with 4 step length */ /* increase the number of socket with 4 step length */
cnt = st->max_socket + 4; cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt; cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *)); sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
@ -352,7 +364,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket)
/* allocate 'struct sal_socket' */ /* allocate 'struct sal_socket' */
if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL) if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
{ {
st->sockets[idx] = rt_malloc(sizeof(struct sal_socket)); st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
if (st->sockets[idx] == RT_NULL) if (st->sockets[idx] == RT_NULL)
{ {
idx = st->max_socket; idx = st->max_socket;
@ -476,7 +488,9 @@ int sal_shutdown(int socket, int how)
if (sock->ops->shutdown((int) sock->user_data, how) == 0) if (sock->ops->shutdown((int) sock->user_data, how) == 0)
{ {
rt_memset(sock, 0x00, sizeof(struct sal_socket)); rt_free(sock);
sock = RT_NULL;
return 0; return 0;
} }
@ -683,7 +697,9 @@ int sal_closesocket(int socket)
if (sock->ops->closesocket((int) sock->user_data) == 0) if (sock->ops->closesocket((int) sock->user_data) == 0)
{ {
rt_memset(sock, 0x00, sizeof(struct sal_socket)); rt_free(sock);
sock = RT_NULL;
return 0; return 0;
} }