ambrop7 15 лет назад
Родитель
Сommit
521f7db90f

+ 1 - 1
bstruct_generator/bstruct_functions.php

@@ -217,7 +217,7 @@ EOD;
 
 EOD;
 
-            $off = "BALIGN_UP_N(o->len, cur_align)";
+            $off = "balign_up(o->len, cur_align)";
             $len = "(cur_count * cur_size)";
 
             echo <<<EOD

+ 1 - 1
dhcpclient/BDHCPClientCore.c

@@ -697,7 +697,7 @@ int BDHCPClientCore_GetDNS (BDHCPClientCore *o, uint32_t *out_dns_servers, size_
     ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
     DebugObject_Access(&o->d_obj);
     
-    int num_return = BMIN(o->acked.domain_name_servers_count, max_dns_servers);
+    int num_return = bmin_int(o->acked.domain_name_servers_count, max_dns_servers);
     
     memcpy(out_dns_servers, o->acked.domain_name_servers, num_return * sizeof(uint32_t));
     return num_return;

+ 2 - 2
flow/FragmentProtoDisassembler.c

@@ -42,9 +42,9 @@ static void write_chunks (FragmentProtoDisassembler *o)
     // write chunks to output packet
     do {
         // calculate chunk length
-        int chunk_len = BMIN(IN_AVAIL, OUT_AVAIL);
+        int chunk_len = bmin_int(IN_AVAIL, OUT_AVAIL);
         if (o->chunk_mtu > 0) {
-            chunk_len = BMIN(chunk_len, o->chunk_mtu);
+            chunk_len = bmin_int(chunk_len, o->chunk_mtu);
         }
         
         // write chunk header

+ 1 - 1
flow/PacketPassFairQueue.c

@@ -159,7 +159,7 @@ static void input_handler_send (PacketPassFairQueueFlow *flow, uint8_t *data, in
         m->previous_flow = NULL;
     } else {
         // raise time
-        flow->time = BMAX(flow->time, get_current_time(m));
+        flow->time = bmax_uint64(flow->time, get_current_time(m));
     }
     
     // queue flow

+ 1 - 1
flow/PacketProtoDecoder.c

@@ -140,7 +140,7 @@ int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, Str
     PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
     
     // set output MTU, limit by maximum payload size
-    enc->output_mtu = BMIN(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
+    enc->output_mtu = bmin_int(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
     
     // init buffer state
     enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);

+ 1 - 1
flow/SPProtoDecoder.c

@@ -199,7 +199,7 @@ int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct
     
     // allocate plaintext buffer
     if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
-        int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
+        int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->output_mtu + 1), o->enc_block_size);
         if (!(o->buf = malloc(buf_size))) {
             goto fail0;
         }

+ 2 - 2
flow/SPProtoEncoder.c

@@ -83,7 +83,7 @@ static void encode_packet (SPProtoEncoder *o)
     
     if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
         // encrypting pad(header + payload)
-        int cyphertext_len = BALIGN_UP_N((plaintext_len + 1), o->enc_block_size);
+        int cyphertext_len = balign_up((plaintext_len + 1), o->enc_block_size);
         
         // write padding
         plaintext[plaintext_len] = 1;
@@ -226,7 +226,7 @@ int SPProtoEncoder_Init (SPProtoEncoder *o, PacketRecvInterface *input, struct s
     
     // allocate plaintext buffer
     if (SPPROTO_HAVE_ENCRYPTION(o->sp_params)) {
-        int buf_size = BALIGN_UP_N((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
+        int buf_size = balign_up((SPPROTO_HEADER_LEN(o->sp_params) + o->input_mtu + 1), o->enc_block_size);
         if (!(o->buf = malloc(buf_size))) {
             goto fail1;
         }

+ 3 - 3
generated/bstruct_OTPChecker.h

@@ -39,7 +39,7 @@ static void oc_tableParams_Init (oc_tableParams *o, int num_entries)
     cur_size = (sizeof(uint16_t));
     cur_align = (__alignof__(uint16_t));
     cur_count = (1);
-    o->id_off = BALIGN_UP_N(o->len, cur_align);
+    o->id_off = balign_up(o->len, cur_align);
     o->id_size = cur_size;
     #ifndef NDEBUG
     o->id_count = cur_count;
@@ -50,7 +50,7 @@ static void oc_tableParams_Init (oc_tableParams *o, int num_entries)
     cur_size = (sizeof(struct OTPChecker_entry));
     cur_align = (__alignof__(struct OTPChecker_entry));
     cur_count = (num_entries);
-    o->entries_off = BALIGN_UP_N(o->len, cur_align);
+    o->entries_off = balign_up(o->len, cur_align);
     o->entries_size = cur_size;
     #ifndef NDEBUG
     o->entries_count = cur_count;
@@ -113,7 +113,7 @@ static void oc_tablesParams_Init (oc_tablesParams *o, int num_tables, int num_en
     cur_size = o->tables_params.len;
     cur_align = o->tables_params.align;
     cur_count = (num_tables);
-    o->tables_off = BALIGN_UP_N(o->len, cur_align);
+    o->tables_off = balign_up(o->len, cur_align);
     o->tables_size = cur_size;
     #ifndef NDEBUG
     o->tables_count = cur_count;

+ 5 - 5
generated/bstruct_bstruct_test.h

@@ -33,7 +33,7 @@ static void str0Params_Init (str0Params *o, int n)
     cur_size = (sizeof(int));
     cur_align = (__alignof__(int));
     cur_count = (n);
-    o->x_off = BALIGN_UP_N(o->len, cur_align);
+    o->x_off = balign_up(o->len, cur_align);
     o->x_size = cur_size;
     #ifndef NDEBUG
     o->x_count = cur_count;
@@ -100,7 +100,7 @@ static void str1Params_Init (str1Params *o, int nb, int nc, int m)
     cur_size = (sizeof(int));
     cur_align = (__alignof__(int));
     cur_count = (1);
-    o->a_off = BALIGN_UP_N(o->len, cur_align);
+    o->a_off = balign_up(o->len, cur_align);
     o->a_size = cur_size;
     #ifndef NDEBUG
     o->a_count = cur_count;
@@ -111,7 +111,7 @@ static void str1Params_Init (str1Params *o, int nb, int nc, int m)
     cur_size = (sizeof(char));
     cur_align = (__alignof__(char));
     cur_count = (nb);
-    o->b_off = BALIGN_UP_N(o->len, cur_align);
+    o->b_off = balign_up(o->len, cur_align);
     o->b_size = cur_size;
     #ifndef NDEBUG
     o->b_count = cur_count;
@@ -122,7 +122,7 @@ static void str1Params_Init (str1Params *o, int nb, int nc, int m)
     cur_size = (sizeof(double));
     cur_align = (__alignof__(double));
     cur_count = (nc);
-    o->c_off = BALIGN_UP_N(o->len, cur_align);
+    o->c_off = balign_up(o->len, cur_align);
     o->c_size = cur_size;
     #ifndef NDEBUG
     o->c_count = cur_count;
@@ -134,7 +134,7 @@ static void str1Params_Init (str1Params *o, int nb, int nc, int m)
     cur_size = o->d_params.len;
     cur_align = o->d_params.align;
     cur_count = (1);
-    o->d_off = BALIGN_UP_N(o->len, cur_align);
+    o->d_off = balign_up(o->len, cur_align);
     o->d_size = cur_size;
     #ifndef NDEBUG
     o->d_count = cur_count;

+ 18 - 24
misc/balign.h

@@ -27,38 +27,32 @@
 #ifndef BADVPN_MISC_BALIGN_H
 #define BADVPN_MISC_BALIGN_H
 
-#include <stdlib.h>
+#include <stddef.h>
 
 /**
- * Aligns the integer x up to n bytes.
+ * Aligns x up to n.
  */
-#define BALIGN_UP_N(x,n) \
-    ({\
-        typeof (x) _x = (x);\
-        typeof (n) _n = (n);\
-        typeof (x) _r = _x % _n;\
-        _r ? _x + (_n - _r) : _x;\
-    })
+static size_t balign_up (size_t x, size_t n)
+{
+    size_t r = x % n;
+    return (r ? x + (n - r) : x);
+}
 
 /**
- * Aligns the integer x down to n bytes.
+ * Aligns x down to n.
  */
-#define BALIGN_DOWN_N(x,n) \
-    ({\
-        typeof (x) _x = (x);\
-        typeof (n) _n = (n);\
-        _x - (_x % _n);\
-    })
+static size_t balign_down (size_t x, size_t n)
+{
+    return (x - (x % n));
+}
 
 /**
- * Calculates the quotient of integers a and b, rounded up.
+ * Calculates the quotient of a and b, rounded up.
  */
-#define BDIVIDE_UP(a,b) \
-    ({\
-        typeof (a) _a = (a);\
-        typeof (b) _b = (b);\
-        typeof (a) _r = _a % _b;\
-        _r > 0 ? _a / _b + 1 : _a / _b;\
-    })
+static size_t bdivide_up (size_t a, size_t b)
+{
+    size_t r = a % b;
+    return (r > 0 ? a / b + 1 : a / b);
+}
 
 #endif

+ 10 - 13
misc/dead.h

@@ -73,19 +73,18 @@ typedef int *dead_t;
 /**
  * Initializes a dead variable.
  */
-#define DEAD_INIT(ptr) ({ptr = NULL;})
+#define DEAD_INIT(ptr) { ptr = NULL; }
 
 /**
  * Kills the dead variable,
  */
-#define DEAD_KILL(ptr) ({if (ptr) *(ptr) = 1;})
+#define DEAD_KILL(ptr) { if (ptr) *(ptr) = 1; }
 
 /**
  * Kills the dead variable with the given value, or does nothing
- * if the value is 0. The value will seen by {@link DEAD_LEAVE} and
- * {@link DEAD_KILLED}.
+ * if the value is 0. The value will seen by {@link DEAD_KILLED}.
  */
-#define DEAD_KILL_WITH(ptr, val) ({if (ptr) *(ptr) = (val);})
+#define DEAD_KILL_WITH(ptr, val) { if (ptr) *(ptr) = (val); }
 
 /**
  * Declares dead catching variables.
@@ -96,24 +95,22 @@ typedef int *dead_t;
  * Enters a dead catching using already declared dead catching variables.
  * The dead variable must have been initialized with {@link DEAD_INIT},
  * and {@link DEAD_KILL} must not have been called yet.
- * {@link DEAD_LEAVE} must be called before the current scope is left.
+ * {@link DEAD_LEAVE2} must be called before the current scope is left.
  */
-#define DEAD_ENTER2(ptr) {__dead = 0; __prev_ptr = ptr; ptr = &__dead;}
+#define DEAD_ENTER2(ptr) { __dead = 0; __prev_ptr = ptr; ptr = &__dead; }
 
 /**
  * Declares dead catching variables and enters a dead catching.
  * The dead variable must have been initialized with {@link DEAD_INIT},
  * and {@link DEAD_KILL} must not have been called yet.
- * {@link DEAD_LEAVE} must be called before the current scope is left.
+ * {@link DEAD_LEAVE2} must be called before the current scope is left.
  */
 #define DEAD_ENTER(ptr) DEAD_DECLARE DEAD_ENTER2(ptr)
 
 /**
  * Leaves a dead catching.
- * Returns 1 if {@link DEAD_KILL} was called for the dead variable, 0 otherwise.
- * Must be called after entering a dead catching and before leaving it.
  */
-#define DEAD_LEAVE(ptr) ({if (!__dead) ptr = __prev_ptr; if (__prev_ptr) *__prev_ptr = __dead; __dead;})
+#define DEAD_LEAVE2(ptr) { if (!__dead) ptr = __prev_ptr; if (__prev_ptr) *__prev_ptr = __dead; }
 
 /**
  * Returns 1 if {@link DEAD_KILL} was called for the dead variable, 0 otherwise.
@@ -122,9 +119,9 @@ typedef int *dead_t;
 #define DEAD_KILLED (__dead)
 
 #define DEAD_DECLARE_N(n) int __dead##n; dead_t __prev_ptr##n;
-#define DEAD_ENTER2_N(n, ptr) {__dead##n = 0; __prev_ptr##n = ptr; ptr = &__dead##n;}
+#define DEAD_ENTER2_N(n, ptr) { __dead##n = 0; __prev_ptr##n = ptr; ptr = &__dead##n;}
 #define DEAD_ENTER_N(n, ptr) DEAD_DECLARE_N(n) DEAD_ENTER2_N(n, ptr)
-#define DEAD_LEAVE_N(n, ptr) ({if (!__dead##n) ptr = __prev_ptr##n; if (__prev_ptr##n) *__prev_ptr##n = __dead##n; __dead##n;})
+#define DEAD_LEAVE2_N(n, ptr) { if (!__dead##n) ptr = __prev_ptr##n; if (__prev_ptr##n) *__prev_ptr##n = __dead##n; }
 #define DEAD_KILLED_N(n) (__dead##n)
 
 #endif

+ 6 - 14
misc/debug.h

@@ -38,8 +38,6 @@
  * Macro for forced assertions.
  * Evaluates the argument and terminates the program abnormally
  * if the result is false.
- * Expands to an expression with the type and value of the result
- * of the evaluation.
  */
 
 /**
@@ -48,7 +46,6 @@
  * Macro for assertions.
  * The argument may or may not be evaluated.
  * If the argument is evaluated, it must not evaluate to false.
- * Expands to an expression of type void.
  */
 
 /**
@@ -57,8 +54,6 @@
  * Macro for always-evaluated assertions.
  * The argument is evaluated.
  * The argument must not evaluate to false.
- * Expands to an expression with the type and value of the result
- * of the evaluation.
  */
 
 /**
@@ -94,30 +89,27 @@
 
 #define ASSERT_FORCE(e) \
     { \
-        typeof (e) _assert_res = (e); \
-        if (!_assert_res) { \
+        if (!(e)) { \
             fprintf(stderr, "%s:%d Assertion failed\n", __FILE__, __LINE__); \
             abort(); \
         } \
-        _assert_res; \
     }
 
 #ifdef NDEBUG
-    #define DEBUG_ZERO_MEMORY(buf, len)
-    #define ASSERT(e) { ; }
+    #define DEBUG_ZERO_MEMORY(buf, len) {}
+    #define ASSERT(e) {}
     #define ASSERT_EXECUTE(e) { (e); }
 #else
     #define DEBUG_ZERO_MEMORY(buf, len) { memset((buf), 0, (len)); }
     #ifdef BADVPN_USE_C_ASSERT
-        #define ASSERT(e) { assert(e); ; }
+        #define ASSERT(e) { assert(e); }
         #define ASSERT_EXECUTE(e) \
             { \
-                typeof (e) _assert_res = (e); \
+                int _assert_res = !!(e); \
                 assert(_assert_res); \
-                _assert_res; \
             }
     #else
-        #define ASSERT(e) { ASSERT_FORCE(e); ; }
+        #define ASSERT(e) ASSERT_FORCE(e)
         #define ASSERT_EXECUTE(e) ASSERT_FORCE(e)
     #endif
 #endif

+ 17 - 12
misc/minmax.h

@@ -27,18 +27,23 @@
 #ifndef BADVPN_MISC_MINMAX_H
 #define BADVPN_MISC_MINMAX_H
 
-#define BMIN(a,b) \
-    ({\
-        typeof (a) _a = (a);\
-        typeof (b) _b = (b);\
-        (_a < _b ? _a : _b);\
-    })
+#include <stddef.h>
+#include <stdint.h>
 
-#define BMAX(a,b) \
-    ({\
-        typeof (a) _a = (a);\
-        typeof (b) _b = (b);\
-        (_a > _b ? _a : _b);\
-    })
+#define DEFINE_BMINMAX(name, type) \
+static type bmin ## name (type a, type b) { return (a < b ? a : b); } \
+static type bmax ## name (type a, type b) { return (a > b ? a : b); }
+
+DEFINE_BMINMAX(_size, size_t)
+DEFINE_BMINMAX(_int, int)
+DEFINE_BMINMAX(_int8, int8_t)
+DEFINE_BMINMAX(_int16, int16_t)
+DEFINE_BMINMAX(_int32, int32_t)
+DEFINE_BMINMAX(_int64, int64_t)
+DEFINE_BMINMAX(_uint, unsigned int)
+DEFINE_BMINMAX(_uint8, uint8_t)
+DEFINE_BMINMAX(_uint16, uint16_t)
+DEFINE_BMINMAX(_uint32, uint32_t)
+DEFINE_BMINMAX(_uint64, uint64_t)
 
 #endif

+ 12 - 12
misc/overflow.h

@@ -30,13 +30,13 @@
 #include <limits.h>
 #include <stdint.h>
 
-#define __DEFINE_UNSIGNED_OVERFLOW(_name, _type, _max) \
+#define DEFINE_UNSIGNED_OVERFLOW(_name, _type, _max) \
 static int add_ ## _name ## _overflows (_type a, _type b) \
 {\
     return (b > _max - a); \
 }
 
-#define __DEFINE_SIGNED_OVERFLOW(_name, _type, _min, _max) \
+#define DEFINE_SIGNED_OVERFLOW(_name, _type, _min, _max) \
 static int add_ ## _name ## _overflows (_type a, _type b) \
 {\
     if ((a < 0) ^ (b < 0)) return 0; \
@@ -44,16 +44,16 @@ static int add_ ## _name ## _overflows (_type a, _type b) \
     return (a > _max - b); \
 }
 
-__DEFINE_UNSIGNED_OVERFLOW(uint, unsigned int, UINT_MAX)
-__DEFINE_UNSIGNED_OVERFLOW(uint8, uint8_t, UINT8_MAX)
-__DEFINE_UNSIGNED_OVERFLOW(uint16, uint16_t, UINT16_MAX)
-__DEFINE_UNSIGNED_OVERFLOW(uint32, uint32_t, UINT32_MAX)
-__DEFINE_UNSIGNED_OVERFLOW(uint64, uint64_t, UINT64_MAX)
+DEFINE_UNSIGNED_OVERFLOW(uint, unsigned int, UINT_MAX)
+DEFINE_UNSIGNED_OVERFLOW(uint8, uint8_t, UINT8_MAX)
+DEFINE_UNSIGNED_OVERFLOW(uint16, uint16_t, UINT16_MAX)
+DEFINE_UNSIGNED_OVERFLOW(uint32, uint32_t, UINT32_MAX)
+DEFINE_UNSIGNED_OVERFLOW(uint64, uint64_t, UINT64_MAX)
 
-__DEFINE_SIGNED_OVERFLOW(int, int, INT_MIN, INT_MAX)
-__DEFINE_SIGNED_OVERFLOW(int8, int8_t, INT8_MIN, INT8_MAX)
-__DEFINE_SIGNED_OVERFLOW(int16, int16_t, INT16_MIN, INT16_MAX)
-__DEFINE_SIGNED_OVERFLOW(int32, int32_t, INT32_MIN, INT32_MAX)
-__DEFINE_SIGNED_OVERFLOW(int64, int64_t, INT64_MIN, INT64_MAX)
+DEFINE_SIGNED_OVERFLOW(int, int, INT_MIN, INT_MAX)
+DEFINE_SIGNED_OVERFLOW(int8, int8_t, INT8_MIN, INT8_MAX)
+DEFINE_SIGNED_OVERFLOW(int16, int16_t, INT16_MIN, INT16_MAX)
+DEFINE_SIGNED_OVERFLOW(int32, int32_t, INT32_MIN, INT32_MAX)
+DEFINE_SIGNED_OVERFLOW(int64, int64_t, INT64_MIN, INT64_MAX)
 
 #endif

+ 1 - 1
protocol/fragmentproto.h

@@ -84,7 +84,7 @@ static int fragmentproto_max_chunks_for_frame (int carrier_mtu, int frame_mtu)
     ASSERT(carrier_mtu > sizeof(struct fragmentproto_chunk_header))
     ASSERT(frame_mtu >= 0)
     
-    return (BDIVIDE_UP(frame_mtu, (carrier_mtu - sizeof(struct fragmentproto_chunk_header))) + 1);
+    return (bdivide_up(frame_mtu, (carrier_mtu - sizeof(struct fragmentproto_chunk_header))) + 1);
 }
 
 #endif

+ 2 - 2
protocol/spproto.h

@@ -148,7 +148,7 @@ static int spproto_payload_mtu_for_carrier_mtu (struct spproto_security_params p
         return (carrier_mtu - SPPROTO_HEADER_LEN(params));
     } else {
         int block_size = BEncryption_cipher_block_size(params.encryption_mode);
-        return (BALIGN_DOWN_N(carrier_mtu, block_size) - block_size - SPPROTO_HEADER_LEN(params) - 1);
+        return (balign_down(carrier_mtu, block_size) - block_size - SPPROTO_HEADER_LEN(params) - 1);
     }
 }
 
@@ -178,7 +178,7 @@ static int spproto_carrier_mtu_for_payload_mtu (struct spproto_security_params p
             return -1;
         }
         
-        return (block_size + BALIGN_UP_N((SPPROTO_HEADER_LEN(params) + payload_mtu + 1), block_size));
+        return (block_size + balign_up((SPPROTO_HEADER_LEN(params) + payload_mtu + 1), block_size));
     }
 }
 

+ 7 - 1
security/OTPCalculator.c

@@ -20,6 +20,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <limits.h>
+
 #include <security/OTPCalculator.h>
 
 int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher)
@@ -31,11 +33,15 @@ int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher)
     calc->num_otps = num_otps;
     calc->cipher = cipher;
     
+    if (calc->num_otps > SIZE_MAX / sizeof(otp_t)) {
+        goto fail0;
+    }
+    
     // remember block size
     calc->block_size = BEncryption_cipher_block_size(calc->cipher);
     
     // calculate number of blocks
-    calc->num_blocks = BDIVIDE_UP(calc->num_otps * sizeof(otp_t), calc->block_size);
+    calc->num_blocks = bdivide_up(calc->num_otps * sizeof(otp_t), calc->block_size);
     
     // allocate buffer
     calc->data = malloc(calc->num_blocks * calc->block_size);

+ 6 - 6
structure/ChunkBuffer2.h

@@ -55,7 +55,7 @@ typedef struct {
 // 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))) * \
+        (1 + bdivide_up((_clen), sizeof(struct ChunkBuffer2_block))) * \
         ((_cnum) + 1) \
     )
 
@@ -126,7 +126,7 @@ static void _ChunkBuffer2_assert_io (ChunkBuffer2 *buf)
     if (buf->used > 0) {
         int datalen = buf->buffer[buf->start].len;
         ASSERT(datalen >= 0)
-        int blocklen = BDIVIDE_UP(datalen, sizeof(struct ChunkBuffer2_block));
+        int blocklen = bdivide_up(datalen, sizeof(struct ChunkBuffer2_block));
         ASSERT(blocklen <= buf->used - 1)
         ASSERT(blocklen <= buf->wrap - buf->start - 1)
         ASSERT(buf->output_dest == (uint8_t *)&buf->buffer[buf->start + 1])
@@ -173,7 +173,7 @@ static void _ChunkBuffer2_update_output (ChunkBuffer2 *buf)
     if (buf->used > 0) {
         int datalen = buf->buffer[buf->start].len;
         ASSERT(datalen >= 0)
-        int blocklen = BDIVIDE_UP(datalen, sizeof(struct ChunkBuffer2_block));
+        int blocklen = bdivide_up(datalen, sizeof(struct ChunkBuffer2_block));
         ASSERT(blocklen <= buf->used - 1)
         ASSERT(blocklen <= buf->wrap - buf->start - 1)
         buf->output_dest = (uint8_t *)&buf->buffer[buf->start + 1];
@@ -194,7 +194,7 @@ void ChunkBuffer2_Init (ChunkBuffer2 *buf, struct ChunkBuffer2_block *buffer, in
     buf->wrap = blocks;
     buf->start = 0;
     buf->used = 0;
-    buf->mtu = BDIVIDE_UP(mtu, sizeof(struct ChunkBuffer2_block));
+    buf->mtu = bdivide_up(mtu, sizeof(struct ChunkBuffer2_block));
     
     CHUNKBUFFER2_ASSERT_BUFFER(buf)
     
@@ -214,7 +214,7 @@ void ChunkBuffer2_SubmitPacket (ChunkBuffer2 *buf, int len)
     CHUNKBUFFER2_ASSERT_IO(buf)
     
     int end = _ChunkBuffer2_end(buf);
-    int blocklen = BDIVIDE_UP(len, sizeof(struct ChunkBuffer2_block));
+    int blocklen = bdivide_up(len, sizeof(struct ChunkBuffer2_block));
     
     ASSERT(blocklen <= buf->size - end - 1)
     ASSERT(buf->used < buf->wrap - buf->start || blocklen <= buf->start - end - 1)
@@ -249,7 +249,7 @@ void ChunkBuffer2_ConsumePacket (ChunkBuffer2 *buf)
     ASSERT(1 <= buf->wrap - buf->start)
     ASSERT(1 <= buf->used)
     
-    int blocklen = BDIVIDE_UP(buf->buffer[buf->start].len, sizeof(struct ChunkBuffer2_block));
+    int blocklen = bdivide_up(buf->buffer[buf->start].len, sizeof(struct ChunkBuffer2_block));
     
     ASSERT(blocklen <= buf->wrap - buf->start - 1)
     ASSERT(blocklen <= buf->used - 1)

+ 7 - 4
tun2socks/tun2socks.c

@@ -858,7 +858,8 @@ err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err)
     
     DEAD_ENTER(client->dead_client)
     SYNC_COMMIT
-    if (DEAD_LEAVE(client->dead_client) == -1) {
+    DEAD_LEAVE2(client->dead_client)
+    if (DEAD_KILLED == -1) {
         return ERR_ABRT;
     }
     
@@ -1066,7 +1067,8 @@ err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t e
         client_send_to_socks(client);
         DEAD_ENTER(client->dead_client)
         SYNC_COMMIT
-        if (DEAD_LEAVE(client->dead_client) == -1) {
+        DEAD_LEAVE2(client->dead_client)
+        if (DEAD_KILLED == -1) {
             return ERR_ABRT;
         }
     }
@@ -1246,7 +1248,7 @@ int client_socks_recv_send_out (struct tcp_client *client)
     // 0 means it wasn't and the client (pcb) is still up
     
     do {
-        int to_write = BMIN(client->socks_recv_buf_used - client->socks_recv_buf_sent, tcp_sndbuf(client->pcb));
+        int to_write = bmin_int(client->socks_recv_buf_used - client->socks_recv_buf_sent, tcp_sndbuf(client->pcb));
         if (to_write == 0) {
             break;
         }
@@ -1331,7 +1333,8 @@ err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len)
             client_socks_recv_initiate(client);
             DEAD_ENTER(client->dead)
             SYNC_COMMIT
-            if (DEAD_LEAVE(client->dead)) {
+            DEAD_LEAVE2(client->dead)
+            if (DEAD_KILLED) {
                 return ERR_ABRT;
             }
         }