mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-28 11:43:50 +00:00
Refine memory management (#579)
- Added one macro to avoid dangling pointers - Added two functions to perform NULL pointer check since the allocation is not guaranteed by C library, although it is a rare case, just for sanity - Add NULL pointer check to brealloc() and bfree() and for sanity as well Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
+10
-8
@@ -76,6 +76,7 @@
|
||||
#include "cache.h"
|
||||
#include "encrypt.h"
|
||||
#include "utils.h"
|
||||
#include "mm-wrapper.h"
|
||||
|
||||
#define OFFSET_ROL(p, o) ((uint64_t)(*(p + o)) << (8 * o))
|
||||
|
||||
@@ -213,16 +214,17 @@ static int safe_memcmp(const void *s1, const void *s2, size_t n)
|
||||
int balloc(buffer_t *ptr, size_t capacity)
|
||||
{
|
||||
memset(ptr, 0, sizeof(buffer_t));
|
||||
ptr->array = malloc(capacity);
|
||||
ptr->array = SS_SAFEMALLOC(capacity);
|
||||
ptr->capacity = capacity;
|
||||
return capacity;
|
||||
}
|
||||
|
||||
int brealloc(buffer_t *ptr, size_t len, size_t capacity)
|
||||
{
|
||||
if (ptr == NULL) return -1;
|
||||
size_t real_capacity = max(len, capacity);
|
||||
if (ptr->capacity < real_capacity) {
|
||||
ptr->array = realloc(ptr->array, real_capacity);
|
||||
ptr->array = SS_SAFEREALLOC(ptr->array, real_capacity);
|
||||
ptr->capacity = real_capacity;
|
||||
}
|
||||
return real_capacity;
|
||||
@@ -230,12 +232,12 @@ int brealloc(buffer_t *ptr, size_t len, size_t capacity)
|
||||
|
||||
void bfree(buffer_t *ptr)
|
||||
{
|
||||
if (ptr == NULL) return;
|
||||
ptr->idx = 0;
|
||||
ptr->len = 0;
|
||||
ptr->capacity = 0;
|
||||
if (ptr->array != NULL) {
|
||||
free(ptr->array);
|
||||
ptr->array = NULL;
|
||||
SS_SAFEFREE(ptr->array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,8 +308,8 @@ static void merge(uint8_t *left, int llength, uint8_t *right,
|
||||
}
|
||||
}
|
||||
|
||||
free(ltmp);
|
||||
free(rtmp);
|
||||
SS_SAFEFREE(ltmp);
|
||||
SS_SAFEFREE(rtmp);
|
||||
}
|
||||
|
||||
static void merge_sort(uint8_t array[], int length,
|
||||
@@ -365,8 +367,8 @@ void enc_table_init(const char *pass)
|
||||
uint64_t key = 0;
|
||||
uint8_t *digest;
|
||||
|
||||
enc_table = malloc(256);
|
||||
dec_table = malloc(256);
|
||||
enc_table = SS_SAFEMALLOC(256);
|
||||
dec_table = SS_SAFEMALLOC(256);
|
||||
|
||||
digest = enc_md5((const uint8_t *)pass, strlen(pass), NULL);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user