Checking memory allocation return values helps prevent crashes
authorGé van Geldorp <ge@gse.nl>
Fri, 23 Dec 2005 20:47:54 +0000 (20:47 +0000)
committerGé van Geldorp <ge@gse.nl>
Fri, 23 Dec 2005 20:47:54 +0000 (20:47 +0000)
svn path=/trunk/; revision=20316

reactos/drivers/lib/oskittcp/include/freebsd/src/sys/sys/mbuf.h
reactos/drivers/lib/oskittcp/oskittcp/ip_output.c

index 37653b2..c421987 100644 (file)
@@ -304,9 +304,12 @@ union mcluster {
 #ifdef __REACTOS__
 #define MCLGET(m, how) { \
           OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
 #ifdef __REACTOS__
 #define MCLGET(m, how) { \
           OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
-          (m)->m_data = (m)->m_ext.ext_buf = malloc(MCLBYTES); \
-          (m)->m_flags |= M_EXT; \
-          (m)->m_ext.ext_size = MCLBYTES; \
+          (m)->m_ext.ext_buf = malloc(MCLBYTES); \
+         if ((m)->m_ext.ext_buf != NULL) { \
+              (m)->m_data = (m)->m_ext.ext_buf; \
+              (m)->m_flags |= M_EXT; \
+              (m)->m_ext.ext_size = MCLBYTES; \
+          } \
         }
 
 #define MCLFREE(p) { \
         }
 
 #define MCLFREE(p) { \
index 8a519ff..a43def5 100644 (file)
@@ -366,8 +366,17 @@ sendit:
 #ifdef __REACTOS__
            if( OtcpEvent.PacketSend ) {
                struct mbuf *new_m;
 #ifdef __REACTOS__
            if( OtcpEvent.PacketSend ) {
                struct mbuf *new_m;
-               MGET( new_m, M_DONTWAIT, 0 );
+               new_m = m_get( M_DONTWAIT, 0 );
+               if ( NULL == new_m ) {
+                   error = ENOBUFS;
+                   goto done;
+               }
                MCLGET( new_m, M_DONTWAIT );
                MCLGET( new_m, M_DONTWAIT );
+               if (0 == (new_m->m_flags & M_EXT)) {
+                   m_free( new_m );
+                   error = ENOBUFS;
+                   goto done;
+               }
                m_copydata( m, 0, htons(ip->ip_len), new_m->m_data );
                new_m->m_len = htons(ip->ip_len);
                error = OtcpEvent.PacketSend( OtcpEvent.ClientData,
                m_copydata( m, 0, htons(ip->ip_len), new_m->m_data );
                new_m->m_len = htons(ip->ip_len);
                error = OtcpEvent.PacketSend( OtcpEvent.ClientData,
@@ -498,7 +507,16 @@ sendorfree:
        if( error == 0 && OtcpEvent.PacketSend ) {
            struct mbuf *new_m;
            MGET( new_m, M_DONTWAIT, 0 );
        if( error == 0 && OtcpEvent.PacketSend ) {
            struct mbuf *new_m;
            MGET( new_m, M_DONTWAIT, 0 );
+           if ( NULL == new_m ) {
+               error = ENOBUFS;
+               goto done;
+           }
            MCLGET( new_m, M_DONTWAIT );
            MCLGET( new_m, M_DONTWAIT );
+           if (0 == (new_m->m_flags & M_EXT)) {
+               m_free( new_m );
+               error = ENOBUFS;
+               goto done;
+           }
            m_copydata( m, 0, htons(ip->ip_len), new_m->m_data );
            new_m->m_len = htons(ip->ip_len);
            error = OtcpEvent.PacketSend( OtcpEvent.ClientData,
            m_copydata( m, 0, htons(ip->ip_len), new_m->m_data );
            new_m->m_len = htons(ip->ip_len);
            error = OtcpEvent.PacketSend( OtcpEvent.ClientData,