Ver código fonte

ChunkBuffer2: add ChunkBuffer2_calc_blocks for computing the number of blocks, checking for overflows

ambrop7 15 anos atrás
pai
commit
6f95696566
2 arquivos alterados com 32 adições e 9 exclusões
  1. 7 3
      flow/PacketBuffer.c
  2. 25 6
      structure/ChunkBuffer2.h

+ 7 - 3
flow/PacketBuffer.c

@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <stdlib.h>
 
 
 #include <misc/debug.h>
 #include <misc/debug.h>
+#include <misc/balloc.h>
 
 
 #include <flow/PacketBuffer.h>
 #include <flow/PacketBuffer.h>
 
 
@@ -92,8 +93,11 @@ int PacketBuffer_Init (PacketBuffer *buf, PacketRecvInterface *input, PacketPass
     PacketPassInterface_Sender_Init(buf->output, (PacketPassInterface_handler_done)output_handler_done, buf);
     PacketPassInterface_Sender_Init(buf->output, (PacketPassInterface_handler_done)output_handler_done, buf);
     
     
     // allocate buffer
     // allocate buffer
-    int num_blocks = CHUNKBUFFER2_MAKE_NUMBLOCKS(buf->input_mtu, num_packets);
-    if (!(buf->buf_data = malloc(num_blocks * sizeof(struct ChunkBuffer2_block)))) {
+    int num_blocks = ChunkBuffer2_calc_blocks(buf->input_mtu, num_packets);
+    if (num_blocks < 0) {
+        goto fail0;
+    }
+    if (!(buf->buf_data = BAllocArray(num_blocks, sizeof(struct ChunkBuffer2_block)))) {
         goto fail0;
         goto fail0;
     }
     }
     
     
@@ -116,5 +120,5 @@ void PacketBuffer_Free (PacketBuffer *buf)
     DebugObject_Free(&buf->d_obj);
     DebugObject_Free(&buf->d_obj);
     
     
     // free buffer
     // free buffer
-    free(buf->buf_data);
+    BFree(buf->buf_data);
 }
 }

+ 25 - 6
structure/ChunkBuffer2.h

@@ -23,6 +23,7 @@
 
 
 #include <stdint.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <limits.h>
 
 
 #include <misc/balign.h>
 #include <misc/balign.h>
 #include <misc/debug.h>
 #include <misc/debug.h>
@@ -52,12 +53,8 @@ typedef struct {
     int output_avail;
     int output_avail;
 } ChunkBuffer2;
 } ChunkBuffer2;
 
 
-// calculates a buffer size needed to hold at least 'cnum' packets long at least 'clen'
-#define CHUNKBUFFER2_MAKE_NUMBLOCKS(_clen, _cnum) \
-    ( \
-        (1 + bdivide_up((_clen), sizeof(struct ChunkBuffer2_block))) * \
-        ((_cnum) + 1) \
-    )
+// calculates a buffer size needed to hold at least 'num' packets long at least 'chunk_len'
+static int ChunkBuffer2_calc_blocks (int chunk_len, int num);
 
 
 // initialize
 // initialize
 static void ChunkBuffer2_Init (ChunkBuffer2 *buf, struct ChunkBuffer2_block *buffer, int blocks, int mtu);
 static void ChunkBuffer2_Init (ChunkBuffer2 *buf, struct ChunkBuffer2_block *buffer, int blocks, int mtu);
@@ -184,6 +181,28 @@ static void _ChunkBuffer2_update_output (ChunkBuffer2 *buf)
     }
     }
 }
 }
 
 
+int ChunkBuffer2_calc_blocks (int chunk_len, int num)
+{
+    int chunk_data_blocks = bdivide_up(chunk_len, sizeof(struct ChunkBuffer2_block));
+    
+    if (chunk_data_blocks > INT_MAX - 1) {
+        return -1;
+    }
+    int chunk_blocks = 1 + chunk_data_blocks;
+    
+    if (num > INT_MAX - 1) {
+        return -1;
+    }
+    int num_chunks = num + 1;
+    
+    if (chunk_blocks > INT_MAX / num_chunks) {
+        return -1;
+    }
+    int blocks = chunk_blocks * num_chunks;
+    
+    return blocks;
+}
+
 void ChunkBuffer2_Init (ChunkBuffer2 *buf, struct ChunkBuffer2_block *buffer, int blocks, int mtu)
 void ChunkBuffer2_Init (ChunkBuffer2 *buf, struct ChunkBuffer2_block *buffer, int blocks, int mtu)
 {
 {
     ASSERT(blocks > 0)
     ASSERT(blocks > 0)