use -Wanalyzer in gcc to check some warning in acl

This commit is contained in:
zhengshuxin 2021-02-13 15:15:14 +08:00
parent 4ce5e20917
commit dbf91ceca8
11 changed files with 58 additions and 43 deletions

View File

@ -22,7 +22,7 @@ extern "C" {
# endif # endif
#endif #endif
ACL_API void acl_memory_debug_start(void); ACL_API int *acl_memory_debug_start(void);
ACL_API void acl_memory_debug_stop(void); ACL_API void acl_memory_debug_stop(void);
ACL_API void acl_memory_debug_stack(int onoff); ACL_API void acl_memory_debug_stack(int onoff);
ACL_API void acl_memory_stat(void); ACL_API void acl_memory_stat(void);

View File

@ -91,6 +91,8 @@ ACL_FIFO *private_fifo_new(void)
ACL_FIFO *fifo; ACL_FIFO *fifo;
fifo = (ACL_FIFO *) malloc(sizeof(*fifo)); fifo = (ACL_FIFO *) malloc(sizeof(*fifo));
assert(fifo);
fifo->head = NULL; fifo->head = NULL;
fifo->tail = NULL; fifo->tail = NULL;
fifo->cnt = 0; fifo->cnt = 0;
@ -119,6 +121,8 @@ ACL_FIFO_INFO *private_fifo_push(ACL_FIFO *fifo, void *data)
ACL_FIFO_INFO *info; ACL_FIFO_INFO *info;
info = (ACL_FIFO_INFO *) malloc(sizeof(*info)); info = (ACL_FIFO_INFO *) malloc(sizeof(*info));
assert(info);
info->data = data; info->data = data;
if (fifo->tail == NULL) { if (fifo->tail == NULL) {

View File

@ -499,8 +499,7 @@ int private_vstream_fflush(ACL_VSTREAM *stream)
n = __vstream_sys_write(stream, ptr, (int) stream->wbuf_dlen); n = __vstream_sys_write(stream, ptr, (int) stream->wbuf_dlen);
if (n <= 0) { if (n <= 0) {
if (acl_last_error() == ACL_EINTR if (acl_last_error() == ACL_EINTR
|| acl_last_error() == ACL_EAGAIN) || acl_last_error() == ACL_EAGAIN) {
{
continue; continue;
} }
return (ACL_VSTREAM_EOF); return (ACL_VSTREAM_EOF);
@ -541,7 +540,7 @@ ACL_VSTREAM *private_vstream_fdopen(ACL_SOCKET fd, unsigned int oflags,
ACL_VSTREAM *stream = NULL; ACL_VSTREAM *stream = NULL;
stream = (ACL_VSTREAM *) calloc(1, sizeof(ACL_VSTREAM)); stream = (ACL_VSTREAM *) calloc(1, sizeof(ACL_VSTREAM));
acl_assert(stream); assert(stream);
if (buflen < ACL_VSTREAM_DEF_MAXLEN) if (buflen < ACL_VSTREAM_DEF_MAXLEN)
buflen = ACL_VSTREAM_DEF_MAXLEN; buflen = ACL_VSTREAM_DEF_MAXLEN;
@ -549,15 +548,14 @@ ACL_VSTREAM *private_vstream_fdopen(ACL_SOCKET fd, unsigned int oflags,
/* XXX: 只有非监听流才需要有读缓冲区 */ /* XXX: 只有非监听流才需要有读缓冲区 */
if ((fdtype & ACL_VSTREAM_TYPE_LISTEN_INET) if ((fdtype & ACL_VSTREAM_TYPE_LISTEN_INET)
|| (fdtype & ACL_VSTREAM_TYPE_LISTEN_UNIX)) || (fdtype & ACL_VSTREAM_TYPE_LISTEN_UNIX)) {
{
fdtype |= ACL_VSTREAM_TYPE_LISTEN; fdtype |= ACL_VSTREAM_TYPE_LISTEN;
stream->read_buf = NULL; stream->read_buf = NULL;
} else {
stream->read_buf = (unsigned char *) malloc(buflen + 1);
acl_assert(stream->read_buf != NULL);
} }
stream->read_buf = (unsigned char *) malloc(buflen + 1);
assert(stream->read_buf);
if (fdtype == 0) if (fdtype == 0)
fdtype = ACL_VSTREAM_TYPE_SOCK; fdtype = ACL_VSTREAM_TYPE_SOCK;
@ -594,13 +592,9 @@ ACL_VSTREAM *private_vstream_fdopen(ACL_SOCKET fd, unsigned int oflags,
stream->path = NULL; stream->path = NULL;
stream->close_handle_lnk = private_array_create(5); stream->close_handle_lnk = private_array_create(5);
if (stream->close_handle_lnk == NULL) { assert(stream->close_handle_lnk);
free(stream->read_buf);
free(stream);
return (NULL);
}
return (stream); return stream;
} }
ACL_VSTREAM *private_vstream_fopen(const char *path, unsigned int oflags, ACL_VSTREAM *private_vstream_fopen(const char *path, unsigned int oflags,
@ -747,8 +741,9 @@ void private_vstream_free(ACL_VSTREAM *stream)
break; break;
if (close_handle->close_fn == NULL) if (close_handle->close_fn == NULL)
continue; continue;
/* 只所将此调用放在 close_fn 前面,是为了防止有人误在 close_fn /* 只所将此调用放在 close_fn 前面,是为了防止有人误在
* * close_fn
*
*/ */
private_array_delete(stream->close_handle_lnk, i, NULL); private_array_delete(stream->close_handle_lnk, i, NULL);
close_handle->close_fn(stream, close_handle->context); close_handle->close_fn(stream, close_handle->context);

View File

@ -118,6 +118,8 @@ static void init_log_mutex(acl_pthread_mutex_t *lock)
int n1, n2; int n1, n2;
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
assert(lock);
n1 = pthread_mutexattr_init(&attr); n1 = pthread_mutexattr_init(&attr);
/* 使用了 pthread_atfork() 来避免 fork 后的死锁,因为在 fork 前调用过 /* 使用了 pthread_atfork() 来避免 fork 后的死锁,因为在 fork 前调用过
@ -232,6 +234,7 @@ static int open_file_log(const char *filename, const char *logpre)
#endif #endif
log = (ACL_LOG*) calloc(1, sizeof(ACL_LOG)); log = (ACL_LOG*) calloc(1, sizeof(ACL_LOG));
assert(log);
log->last_open = time(NULL); log->last_open = time(NULL);
log->fp = fp; log->fp = fp;
log->path = strdup(filename); log->path = strdup(filename);

View File

@ -56,6 +56,7 @@ static ACL_XINETD_NV_PAIR *__nv_pair_new(const char *name, const char *value)
acl_array_destroy(pair->values, acl_myfree_fn); \ acl_array_destroy(pair->values, acl_myfree_fn); \
acl_myfree(pair); \ acl_myfree(pair); \
} \ } \
return (x); \
} }
pair = (ACL_XINETD_NV_PAIR *) acl_mycalloc(1, sizeof(ACL_XINETD_NV_PAIR)); pair = (ACL_XINETD_NV_PAIR *) acl_mycalloc(1, sizeof(ACL_XINETD_NV_PAIR));

View File

@ -246,12 +246,15 @@ static void debug_dump_atexit(void)
ACL_DEBUG_MEM *acl_debug_malloc_init(ACL_DEBUG_MEM *debug_mem_ptr, const char *dump_file) ACL_DEBUG_MEM *acl_debug_malloc_init(ACL_DEBUG_MEM *debug_mem_ptr, const char *dump_file)
{ {
ACL_DEBUG_MEM *mem;
if (debug_mem_ptr != NULL) { if (debug_mem_ptr != NULL) {
__debug_mem = debug_mem_ptr; __debug_mem = mem = debug_mem_ptr;
} else { } else {
ASSERT(dump_file && *dump_file); ASSERT(dump_file && *dump_file);
__debug_mem = (ACL_DEBUG_MEM*) calloc(1, sizeof(ACL_DEBUG_MEM)); mem = (ACL_DEBUG_MEM*) calloc(1, sizeof(ACL_DEBUG_MEM));
assert(mem);
__debug_mem = mem;
__debug_mem->dump_fp = fopen(dump_file, "wb+"); __debug_mem->dump_fp = fopen(dump_file, "wb+");
ASSERT(__debug_mem->dump_fp); ASSERT(__debug_mem->dump_fp);
__debug_mem->table = debug_htable_create(1000); __debug_mem->table = debug_htable_create(1000);
@ -272,5 +275,5 @@ ACL_DEBUG_MEM *acl_debug_malloc_init(ACL_DEBUG_MEM *debug_mem_ptr, const char *d
acl_debug_memdup, acl_debug_memdup,
acl_debug_free); acl_debug_free);
return (__debug_mem); return mem;
} }

View File

@ -251,6 +251,7 @@ void *acl_default_calloc(const char *filename, int line,
#endif #endif
n = (int) (nmemb * size); n = (int) (nmemb * size);
ptr = acl_default_malloc(filename, line, n); ptr = acl_default_malloc(filename, line, n);
assert(ptr);
memset(ptr, FILLER, n); memset(ptr, FILLER, n);
return ptr; return ptr;
} }

View File

@ -41,12 +41,15 @@ static int __mem_stack = 0;
#define MSTAT_LOCK thread_mutex_lock(&__fastmutex_stat) #define MSTAT_LOCK thread_mutex_lock(&__fastmutex_stat)
#define MSTAT_UNLOCK thread_mutex_unlock(&__fastmutex_stat) #define MSTAT_UNLOCK thread_mutex_unlock(&__fastmutex_stat)
void acl_memory_debug_start(void) int *acl_memory_debug_start(void)
{ {
__alloc_stat = calloc(__alloc_max, sizeof(int)); int *ptr;
acl_assert(__alloc_stat); ptr = calloc(__alloc_max, sizeof(int));
__alloc_stat = ptr;
assert(__alloc_stat);
thread_mutex_init(&__fastmutex_stat, NULL); thread_mutex_init(&__fastmutex_stat, NULL);
__debug_mem = 1; __debug_mem = 1;
return ptr;
} }
void acl_memory_debug_stop(void) void acl_memory_debug_stop(void)

View File

@ -1127,7 +1127,9 @@ ACL_SLICE *acl_slice_create(const char *name, int page_size,
void acl_slice_destroy(ACL_SLICE *slice) void acl_slice_destroy(ACL_SLICE *slice)
{ {
slice->slice_destroy(slice); if (slice) {
slice->slice_destroy(slice);
}
} }
int acl_slice_used(ACL_SLICE *slice) int acl_slice_used(ACL_SLICE *slice)

View File

@ -528,6 +528,8 @@ static int rfc1035_header_unpack(const char *buf, size_t sz, size_t *off,
unsigned short s; unsigned short s;
unsigned short t; unsigned short t;
assert(h);
if (*off != 0) { if (*off != 0) {
msg_error("%s: *off(%d) != 0", myname, (int) *off); msg_error("%s: *off(%d) != 0", myname, (int) *off);
return -RFC1035_UNPACK_ERROR; return -RFC1035_UNPACK_ERROR;
@ -634,7 +636,7 @@ static int rfc1035_query_unpack(const char *buf, size_t sz, size_t *off,
* *
* Returns 0 (success) or 1 (error) * Returns 0 (success) or 1 (error)
*/ */
static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR *RR) static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR *rr)
{ {
const char *myname = "rfc1035_rr_unpack"; const char *myname = "rfc1035_rr_unpack";
unsigned short s; unsigned short s;
@ -642,11 +644,11 @@ static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR
unsigned short rdlength; unsigned short rdlength;
size_t rdata_off; size_t rdata_off;
if (rfc1035_name_unpack(buf, sz, off, NULL, RR->name, if (rfc1035_name_unpack(buf, sz, off, NULL, rr->name,
RFC1035_MAXHOSTNAMESZ, 0)) { RFC1035_MAXHOSTNAMESZ, 0)) {
RFC1035_UNPACK_DEBUG; RFC1035_UNPACK_DEBUG;
memset(RR, '\0', sizeof(*RR)); memset(rr, '\0', sizeof(*rr));
return 1; return 1;
} }
@ -655,21 +657,21 @@ static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR
*/ */
if ((*off) + 10 > sz) { if ((*off) + 10 > sz) {
RFC1035_UNPACK_DEBUG; RFC1035_UNPACK_DEBUG;
memset(RR, '\0', sizeof(*RR)); memset(rr, '\0', sizeof(*rr));
return 1; return 1;
} }
memcpy(&s, buf + (*off), sizeof(s)); memcpy(&s, buf + (*off), sizeof(s));
(*off) += sizeof(s); (*off) += sizeof(s);
RR->type = ntohs(s); rr->type = ntohs(s);
memcpy(&s, buf + (*off), sizeof(s)); memcpy(&s, buf + (*off), sizeof(s));
(*off) += sizeof(s); (*off) += sizeof(s);
RR->tclass = ntohs(s); rr->tclass = ntohs(s);
memcpy(&i, buf + (*off), sizeof(i)); memcpy(&i, buf + (*off), sizeof(i));
(*off) += sizeof(i); (*off) += sizeof(i);
RR->ttl = ntohl(i); rr->ttl = ntohl(i);
memcpy(&s, buf + (*off), sizeof(s)); memcpy(&s, buf + (*off), sizeof(s));
(*off) += sizeof(s); (*off) += sizeof(s);
@ -680,24 +682,24 @@ static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR
* replies at 512 octets, as per RFC 1035. * replies at 512 octets, as per RFC 1035.
*/ */
RFC1035_UNPACK_DEBUG; RFC1035_UNPACK_DEBUG;
memset(RR, '\0', sizeof(*RR)); memset(rr, '\0', sizeof(*rr));
return 1; return 1;
} }
RR->rdlength = rdlength; rr->rdlength = rdlength;
switch (RR->type) { switch (rr->type) {
case RFC1035_TYPE_CNAME: case RFC1035_TYPE_CNAME:
case RFC1035_TYPE_NS: case RFC1035_TYPE_NS:
case RFC1035_TYPE_TXT: case RFC1035_TYPE_TXT:
case RFC1035_TYPE_PTR: case RFC1035_TYPE_PTR:
case RFC1035_TYPE_WKS: case RFC1035_TYPE_WKS:
RR->rdata = (char*) malloc(RFC1035_MAXHOSTNAMESZ); rr->rdata = (char*) malloc(RFC1035_MAXHOSTNAMESZ);
rdata_off = *off; rdata_off = *off;
RR->rdlength = 0; /* Filled in by rfc1035_name_unpack */ rr->rdlength = 0; /* Filled in by rfc1035_name_unpack */
if (rfc1035_name_unpack(buf, sz, &rdata_off, &RR->rdlength, if (rfc1035_name_unpack(buf, sz, &rdata_off, &rr->rdlength,
RR->rdata, RFC1035_MAXHOSTNAMESZ, 0)) { rr->rdata, RFC1035_MAXHOSTNAMESZ, 0)) {
return 1; return 1;
} }
@ -707,8 +709,8 @@ static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR
* the RDATA area. * the RDATA area.
*/ */
RFC1035_UNPACK_DEBUG; RFC1035_UNPACK_DEBUG;
free(RR->rdata); free(rr->rdata);
memset(RR, '\0', sizeof(*RR)); memset(rr, '\0', sizeof(*rr));
return 1; return 1;
} }
break; break;
@ -716,8 +718,8 @@ static int rfc1035_rr_unpack(const char *buf, size_t sz, size_t *off, RFC1035_RR
case RFC1035_TYPE_AAAA: case RFC1035_TYPE_AAAA:
case RFC1035_TYPE_MX: case RFC1035_TYPE_MX:
default: default:
RR->rdata = (char*) malloc(rdlength); rr->rdata = (char*) malloc(rdlength);
memcpy(RR->rdata, buf + (*off), rdlength); memcpy(rr->rdata, buf + (*off), rdlength);
break; break;
} }
@ -900,7 +902,7 @@ RFC1035_MESSAGE *rfc1035_request_unpack(const char *buf, size_t sz)
/****************************************************************************/ /****************************************************************************/
static void rfc1035_rr_destroy(RFC1035_RR * rr, int n) static void rfc1035_rr_destroy(RFC1035_RR *rr, int n)
{ {
const char *myname = "rfc1035_rr_destroy"; const char *myname = "rfc1035_rr_destroy";

View File

@ -59,6 +59,7 @@ EVENT *event_create(int size)
break; break;
} }
assert(ev);
ring_init(&ev->events); ring_init(&ev->events);
ev->timeout = -1; ev->timeout = -1;
ev->setsize = size; ev->setsize = size;