Fixed misc potential bugs as detected by coverity scan

This commit is contained in:
bennylp 2023-03-15 10:06:02 +07:00
parent 8c2f28ee8a
commit 7615589583
10 changed files with 43 additions and 25 deletions

View File

@ -151,14 +151,17 @@ pj_status_t pj_log_init(void)
if (thread_suspended_tls_id == -1) {
pj_status_t status;
status = pj_thread_local_alloc(&thread_suspended_tls_id);
if (status != PJ_SUCCESS)
if (status != PJ_SUCCESS) {
thread_suspended_tls_id = -1;
return status;
}
# if PJ_LOG_ENABLE_INDENT
status = pj_thread_local_alloc(&thread_indent_tls_id);
if (status != PJ_SUCCESS) {
pj_thread_local_free(thread_suspended_tls_id);
thread_suspended_tls_id = -1;
thread_indent_tls_id = -1;
return status;
}
# endif

View File

@ -918,6 +918,7 @@ PJ_DEF(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var,
*/
PJ_DEF(pj_status_t) pj_thread_local_alloc(long *index)
{
DWORD tls_index;
PJ_ASSERT_RETURN(index != NULL, PJ_EINVAL);
//Can't check stack because this function is called in the
@ -925,15 +926,17 @@ PJ_DEF(pj_status_t) pj_thread_local_alloc(long *index)
//PJ_CHECK_STACK();
#if defined(PJ_WIN32_WINPHONE8) && PJ_WIN32_WINPHONE8
*index = TlsAllocRT();
tls_index = TlsAllocRT();
#else
*index = TlsAlloc();
tls_index = TlsAlloc();
#endif
if (*index == TLS_OUT_OF_INDEXES)
if (tls_index == TLS_OUT_OF_INDEXES)
return PJ_RETURN_OS_ERROR(GetLastError());
else
else {
*index = (long)tls_index;
return PJ_SUCCESS;
}
}
/*

View File

@ -17,6 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <pj/pool.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/string.h>
#include <pj/assert.h>
@ -55,6 +56,7 @@ PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
{
int i;
pj_pool_t *pool;
pj_status_t status;
PJ_CHECK_STACK();
@ -77,7 +79,10 @@ PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp,
cp->factory.on_block_free = &cpool_on_block_free;
pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf));
pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
status = pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock);
/* This mostly serves to silent coverity warning about unchecked
* return value. There's not much we can do if it fails. */
PJ_ASSERT_ON_FAIL(status==PJ_SUCCESS, return);
}
PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp )

View File

@ -275,11 +275,10 @@ PJ_DEF(pj_status_t) pj_strtol2(const pj_str_t *str, long *value)
PJ_CHECK_STACK();
PJ_ASSERT_RETURN(str->slen >= 0, PJ_EINVAL);
if (!str || !value) {
return PJ_EINVAL;
}
PJ_ASSERT_RETURN(str->slen >= 0, PJ_EINVAL);
s = *str;
pj_strltrim(&s);
@ -380,12 +379,11 @@ PJ_DEF(pj_status_t) pj_strtoul3(const pj_str_t *str, unsigned long *value,
PJ_CHECK_STACK();
PJ_ASSERT_RETURN(str->slen >= 0, PJ_EINVAL);
if (!str || !value) {
return PJ_EINVAL;
}
PJ_ASSERT_RETURN(str->slen >= 0, PJ_EINVAL);
s = *str;
pj_strltrim(&s);

View File

@ -98,7 +98,6 @@ static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
struct udp_echo_srv **p_srv)
{
struct udp_echo_srv *srv;
pj_sock_t sock_fd = PJ_INVALID_SOCKET;
pj_sockaddr addr;
pj_activesock_cb activesock_cb;
pj_status_t status;
@ -114,7 +113,6 @@ static pj_status_t udp_echo_srv_create(pj_pool_t *pool,
status = pj_activesock_create_udp(pool, &addr, NULL, ioqueue, &activesock_cb,
srv, &srv->asock, &addr);
if (status != PJ_SUCCESS) {
pj_sock_close(sock_fd);
udp_echo_err("pj_activesock_create()", status);
return status;
}

View File

@ -728,8 +728,10 @@ static int perform_test(test_desc *test)
on_return:
if (test->state.ioq)
pj_ioqueue_destroy(test->state.ioq);
pj_grp_lock_dec_ref(test->state.grp_lock);
test->state.grp_lock = NULL;
if (test->state.grp_lock) {
pj_grp_lock_dec_ref(test->state.grp_lock);
test->state.grp_lock = NULL;
}
if (test->state.pool)
pj_pool_release(test->state.pool);

View File

@ -1153,9 +1153,9 @@ static int bench_test(const pj_ioqueue_cfg *cfg, int bufsize,
on_error:
PJ_LOG(1,(THIS_FILE, "...ERROR: %s",
pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf))));
if (ssock)
if (ssock > 0)
pj_sock_close(ssock);
if (csock)
if (csock > 0)
pj_sock_close(csock);
for (i=0; i<inactive_sock_count && inactive_sock &&
inactive_sock[i]!=PJ_INVALID_SOCKET; ++i)

View File

@ -122,6 +122,10 @@ static int sleep_duration_test(void)
/* Mark end of test. */
rc = pj_gettimeofday(&stop);
if (rc != PJ_SUCCESS) {
app_perror("...error: pj_gettimeofday()", rc);
return -22;
}
/* Calculate duration (store in stop). */
PJ_TIME_VAL_SUB(stop, start);

View File

@ -512,8 +512,10 @@ static int send_recv_test(int sock_type,
return -151;
if (pj_sockaddr_cmp(&addr, srcaddr) != 0) {
char srcaddr_str[32], addr_str[32];
strcpy(srcaddr_str, pj_inet_ntoa(srcaddr->sin_addr));
strcpy(addr_str, pj_inet_ntoa(addr.sin_addr));
pj_ansi_strncpy(srcaddr_str, pj_inet_ntoa(srcaddr->sin_addr),
sizeof(srcaddr_str));
pj_ansi_strncpy(addr_str, pj_inet_ntoa(addr.sin_addr),
sizeof(addr_str));
PJ_LOG(3,("test", "...error: src address mismatch (original=%s, "
"recvfrom addr=%s)",
srcaddr_str, addr_str));

View File

@ -436,6 +436,7 @@ static int poll_worker(void *arg)
return 0;
}
#if ST_CANCEL_THREAD_COUNT
/* Cancel worker thread function. */
static int cancel_worker(void *arg)
{
@ -466,6 +467,7 @@ static int cancel_worker(void *arg)
return 0;
}
#endif
static int timer_stress_test(void)
{
@ -627,11 +629,11 @@ static int timer_stress_test(void)
}
/* Start cancel worker threads */
if (ST_CANCEL_THREAD_COUNT) {
cancel_threads = (pj_thread_t**)
pj_pool_calloc(pool, ST_CANCEL_THREAD_COUNT,
sizeof(pj_thread_t*));
}
#if ST_CANCEL_THREAD_COUNT
cancel_threads = (pj_thread_t**)
pj_pool_calloc(pool, ST_CANCEL_THREAD_COUNT,
sizeof(pj_thread_t*));
for (i=0; i<ST_CANCEL_THREAD_COUNT; ++i) {
status = pj_thread_create( pool, "cancel", &cancel_worker, &tparam,
0, 0, &cancel_threads[i]);
@ -641,6 +643,7 @@ static int timer_stress_test(void)
goto on_return;
}
}
#endif
#if SIMULATE_CRASH
tmp_pool = pj_pool_create( mem, NULL, 4096, 128, NULL);
@ -668,7 +671,7 @@ on_return:
tparam.stopping = PJ_TRUE;
for (i=0; i<ST_STRESS_THREAD_COUNT; ++i) {
if (!stress_threads[i])
if (!stress_threads || !stress_threads[i])
continue;
pj_thread_join(stress_threads[i]);
pj_thread_destroy(stress_threads[i]);