api.h, api_lib.c, sockets.c: netbuf_ref doesn't check its internal pbuf_alloc call result and can cause a crash. lwip_send now check netbuf_ref result.

This commit is contained in:
fbernon 2007-03-28 17:26:06 +00:00
parent d956a39fec
commit 913a99dd35
4 changed files with 24 additions and 23 deletions

View File

@ -98,6 +98,10 @@ HISTORY
++ Bug fixes: ++ Bug fixes:
2007-03-28 Frédéric Bernon
* api.h, api_lib.c, sockets.c: netbuf_ref doesn't check its internal pbuf_alloc call
result and can cause a crash. lwip_send now check netbuf_ref result.
2007-03-28 Simon Goldschmidt 2007-03-28 Simon Goldschmidt
* sockets.c Remove "#include <errno.h>" from sockets.c to avoid multiple * sockets.c Remove "#include <errno.h>" from sockets.c to avoid multiple
definition of macros (in errno.h and lwip/arch.h) if LWIP_PROVIDE_ERRNO is definition of macros (in errno.h and lwip/arch.h) if LWIP_PROVIDE_ERRNO is

View File

@ -91,16 +91,21 @@ netbuf_free(struct netbuf *buf)
buf->p = buf->ptr = NULL; buf->p = buf->ptr = NULL;
} }
void err_t
netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
{ {
if (buf->p != NULL) { if (buf->p != NULL) {
pbuf_free(buf->p); pbuf_free(buf->p);
} }
buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
if (buf->p == NULL) {
buf->ptr = NULL;
return ERR_MEM;
}
buf->p->payload = (void*)dataptr; buf->p->payload = (void*)dataptr;
buf->p->len = buf->p->tot_len = size; buf->p->len = buf->p->tot_len = size;
buf->ptr = buf->p; buf->ptr = buf->p;
return ERR_OK;
} }
void void
@ -320,8 +325,7 @@ netconn_type(struct netconn *conn)
} }
err_t err_t
netconn_peer(struct netconn *conn, struct ip_addr *addr, netconn_peer(struct netconn *conn, struct ip_addr *addr, u16_t *port)
u16_t *port)
{ {
switch (conn->type) { switch (conn->type) {
case NETCONN_RAW: case NETCONN_RAW:
@ -347,8 +351,7 @@ netconn_peer(struct netconn *conn, struct ip_addr *addr,
} }
err_t err_t
netconn_addr(struct netconn *conn, struct ip_addr **addr, netconn_addr(struct netconn *conn, struct ip_addr **addr, u16_t *port)
u16_t *port)
{ {
switch (conn->type) { switch (conn->type) {
case NETCONN_RAW: case NETCONN_RAW:
@ -370,8 +373,7 @@ netconn_addr(struct netconn *conn, struct ip_addr **addr,
} }
err_t err_t
netconn_bind(struct netconn *conn, struct ip_addr *addr, netconn_bind(struct netconn *conn, struct ip_addr *addr, u16_t port)
u16_t port)
{ {
struct api_msg msg; struct api_msg msg;
@ -396,8 +398,7 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr,
err_t err_t
netconn_connect(struct netconn *conn, struct ip_addr *addr, netconn_connect(struct netconn *conn, struct ip_addr *addr, u16_t port)
u16_t port)
{ {
struct api_msg msg; struct api_msg msg;
@ -445,8 +446,7 @@ netconn_listen(struct netconn *conn)
} }
if (conn->acceptmbox == SYS_MBOX_NULL) { if (conn->acceptmbox == SYS_MBOX_NULL) {
conn->acceptmbox = sys_mbox_new(); if ((conn->acceptmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
if (conn->acceptmbox == SYS_MBOX_NULL) {
return ERR_MEM; return ERR_MEM;
} }
} }
@ -657,8 +657,7 @@ netconn_close(struct netconn *conn)
msg.type = API_MSG_CLOSE; msg.type = API_MSG_CLOSE;
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
if (conn->err == ERR_MEM && if (conn->err == ERR_MEM && conn->sem != SYS_SEM_NULL) {
conn->sem != SYS_SEM_NULL) {
sys_sem_wait(conn->sem); sys_sem_wait(conn->sem);
goto again; goto again;
} }

View File

@ -484,12 +484,11 @@ lwip_send(int s, const void *data, int size, unsigned int flags)
return -1; return -1;
} }
/* make the buffer point to the data that should /* make the buffer point to the data that should be sent */
be sent */ if ((err = netbuf_ref(buf, data, size))==ERR_OK) {
netbuf_ref(buf, data, size);
/* send the data */ /* send the data */
err = netconn_send(sock->conn, buf); err = netconn_send(sock->conn, buf);
}
/* deallocated the buffer */ /* deallocated the buffer */
netbuf_delete(buf); netbuf_delete(buf);

View File

@ -82,7 +82,6 @@ struct netbuf {
struct pbuf *p, *ptr; struct pbuf *p, *ptr;
struct ip_addr *fromaddr; struct ip_addr *fromaddr;
u16_t fromport; u16_t fromport;
err_t err;
}; };
struct netconn { struct netconn {
@ -111,7 +110,7 @@ struct netbuf * netbuf_new (void);
void netbuf_delete (struct netbuf *buf); void netbuf_delete (struct netbuf *buf);
void * netbuf_alloc (struct netbuf *buf, u16_t size); void * netbuf_alloc (struct netbuf *buf, u16_t size);
void netbuf_free (struct netbuf *buf); void netbuf_free (struct netbuf *buf);
void netbuf_ref (struct netbuf *buf, err_t netbuf_ref (struct netbuf *buf,
const void *dataptr, u16_t size); const void *dataptr, u16_t size);
void netbuf_chain (struct netbuf *head, void netbuf_chain (struct netbuf *head,
struct netbuf *tail); struct netbuf *tail);