From f0da2f2cd4c764f32d000c4272ceef8325f848f0 Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Thu, 18 Jul 2019 20:39:49 +0400 Subject: [PATCH] FS-11903: Fix errors reported by PVS-Studio Static Code Analyzer for libs/esl --- libs/esl/fs_cli.c | 59 ++++++++++++++++++++-------------- libs/esl/src/cJSON.c | 10 +----- libs/esl/src/cJSON_Utils.c | 13 +++++--- libs/esl/src/esl.c | 46 +++++++------------------- libs/esl/src/esl_buffer.c | 8 +++-- libs/esl/src/esl_threadmutex.c | 4 +-- 6 files changed, 62 insertions(+), 78 deletions(-) diff --git a/libs/esl/fs_cli.c b/libs/esl/fs_cli.c index a77ce01575..99db32764b 100644 --- a/libs/esl/fs_cli.c +++ b/libs/esl/fs_cli.c @@ -689,7 +689,10 @@ static void redisplay(void) * our own implementation instead. */ const LineInfo *lf = el_line(el); const char *c = lf->buffer; - if (global_profile->batch_mode) return; + if (global_profile->batch_mode) { + esl_mutex_unlock(MUTEX); + return; + } printf("%s",prompt_str); while (c < lf->lastchar && *c) { putchar(*c); @@ -898,7 +901,7 @@ static int process_command(esl_handle_t *handle, const char *cmd) r = -1; goto end; } else if (!strncasecmp(cmd, "logfilter", 9)) { cmd += 9; - while (*cmd && *cmd == ' ') { + while (cmd && *cmd && *cmd == ' ') { cmd++; } if (!esl_strlen_zero(cmd)) { @@ -910,7 +913,7 @@ static int process_command(esl_handle_t *handle, const char *cmd) output_printf("Logfilter %s\n", logfilter ? "enabled" : "disabled"); } else if (!strncasecmp(cmd, "uuid", 4)) { cmd += 4; - while (*cmd && *cmd == ' ') { + while (cmd && *cmd && *cmd == ' ') { cmd++; } if (!esl_strlen_zero(cmd)) { @@ -957,7 +960,7 @@ static int process_command(esl_handle_t *handle, const char *cmd) if (handle->last_sr_event) { if (handle->last_sr_event->body) { output_printf("%s\n", handle->last_sr_event->body); - } else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) { + } else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 4)) { output_printf("Error: %s!\n", err + 4); } } @@ -1127,7 +1130,10 @@ static char* end_of_str(char *s) { return (*s == '\0' ? s : s + strlen(s) - 1); static char* _strndup(const char *s, int n) { - char *r = (char*)malloc(n + 1), *d=r; + char *r = (char*)malloc(n + 1), *d; + + assert(r); + d = r; while (n > 0 && *s) { *d = *s; d++; s++; n--; @@ -1141,9 +1147,13 @@ static unsigned char esl_console_complete(const char *buffer, const char *cursor char cmd_str[2048] = ""; unsigned char ret = CC_REDISPLAY; char *dup = _strndup(buffer, (int)(lastchar - buffer)); - char *buf = dup; + char *buf; int sc = 0, offset = (int)(cursor - buffer), pos = (offset > 0) ? offset : 0; char *p; + + assert(dup); + buf = dup; + if (pos > 0) { *(buf + pos) = '\0'; } @@ -1184,31 +1194,30 @@ static unsigned char esl_console_complete(const char *buffer, const char *cursor if (global_handle->last_sr_event && global_handle->last_sr_event->body) { char *r = global_handle->last_sr_event->body; char *w, *p1; - if (r) { - if ((w = strstr(r, "\n\nwrite="))) { - int len = 0; - *w = '\0'; - w += 8; - len = atoi(w); - if ((p1= strchr(w, ':'))) { - w = p1+ 1; - } - printf("%s\n\n\n", r); + + if ((w = strstr(r, "\n\nwrite="))) { + int len = 0; + *w = '\0'; + w += 8; + len = atoi(w); + if ((p1= strchr(w, ':'))) { + w = p1+ 1; + } + printf("%s\n\n\n", r); #ifdef HAVE_LIBEDIT - el_deletestr(el, len); - el_insertstr(el, w); + el_deletestr(el, len); + el_insertstr(el, w); #else #ifdef _MSC_VER - console_bufferInput(0, len, (char*)buffer, DELETE_REFRESH_OP); - console_bufferInput(w, (int)strlen(w), (char*)buffer, 0); + console_bufferInput(0, len, (char*)buffer, DELETE_REFRESH_OP); + console_bufferInput(w, (int)strlen(w), (char*)buffer, 0); #endif #endif - } else { - printf("%s\n", r); + } else { + printf("%s\n", r); #ifdef _MSC_VER - console_bufferInput(0, 0, (char*)buffer, DELETE_REFRESH_OP); + console_bufferInput(0, 0, (char*)buffer, DELETE_REFRESH_OP); #endif - } } fflush(stdout); } @@ -1710,7 +1719,7 @@ int main(int argc, char *argv[]) if (handle.last_sr_event) { if (handle.last_sr_event->body) { printf("%s\n", handle.last_sr_event->body); - } else if ((err = esl_event_get_header(handle.last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) { + } else if ((err = esl_event_get_header(handle.last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 4)) { printf("Error: %s!\n", err + 4); } } diff --git a/libs/esl/src/cJSON.c b/libs/esl/src/cJSON.c index c804b8a476..3206091a5b 100644 --- a/libs/esl/src/cJSON.c +++ b/libs/esl/src/cJSON.c @@ -295,10 +295,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna return NULL; } - if (newbuffer) - { - memcpy(newbuffer, p->buffer, p->offset + 1); - } + memcpy(newbuffer, p->buffer, p->offset + 1); hooks->deallocate(p->buffer); } p->length = newsize; @@ -890,11 +887,6 @@ fail: hooks->deallocate(buffer->buffer); } - if (printed != NULL) - { - hooks->deallocate(printed); - } - return NULL; } diff --git a/libs/esl/src/cJSON_Utils.c b/libs/esl/src/cJSON_Utils.c index 0a9fabd1be..395aa73692 100644 --- a/libs/esl/src/cJSON_Utils.c +++ b/libs/esl/src/cJSON_Utils.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "esl_cJSON_Utils.h" @@ -137,6 +138,7 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *ta { /* reserve enough memory for a 64 bit integer + '/' and '\0' */ unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + 23); + assert(ret); /* check if conversion to unsigned long is valid * This should be eliminated at compile time by dead code elimination * if size_t is an alias of unsigned long, or if it is bigger */ @@ -153,6 +155,7 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *ta else if ((type & 0xFF) == cJSON_Object) { unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + cJSONUtils_PointerEncodedstrlen((unsigned char*)obj->string) + 2); + assert(ret); *ret = '/'; cJSONUtils_PointerEncodedstrcpy(ret + 1, (unsigned char*)obj->string); strcat((char*)ret, (char*)found); @@ -513,10 +516,9 @@ CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches) /* malformed patches. */ return 1; } - if (patches) - { - patches = patches->child; - } + + patches = patches->child; + while (patches) { if ((err = cJSONUtils_ApplyPatch(object, patches))) @@ -536,6 +538,7 @@ static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, co if (suffix) { unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2); + assert(newpath); cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix); cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath)); free(newpath); @@ -589,6 +592,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, { size_t c = 0; unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */ + assert(newpath); /* generate patches for all array elements that exist in "from" and "to" */ for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++) { @@ -643,6 +647,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, { /* both object keys are the same */ unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2); + assert(newpath); cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string); /* create a patch for the element */ cJSONUtils_CompareToPatch(patches, newpath, a, b); diff --git a/libs/esl/src/esl.c b/libs/esl/src/esl.c index 01a27f490e..e23144b9c0 100644 --- a/libs/esl/src/esl.c +++ b/libs/esl/src/esl.c @@ -734,9 +734,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list end: - if (server_sock != ESL_SOCK_INVALID) { - closesocket(server_sock); - } + closesocket(server_sock); return status; @@ -802,9 +800,7 @@ ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, end: - if (server_sock != ESL_SOCK_INVALID) { - closesocket(server_sock); - } + closesocket(server_sock); return status; @@ -1150,9 +1146,7 @@ ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle) while(ep) { esl_event_t *e = ep; ep = ep->next; - if (e) { - esl_event_destroy(&e); - } + esl_event_destroy(&e); } esl_event_safe_destroy(&handle->last_event); @@ -1214,10 +1208,7 @@ ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms return ESL_BREAK; } - if (activity < 0) { - handle->connected = 0; - status = ESL_FAIL; - } else if (activity > 0 && (activity & ESL_POLL_READ)) { + if ((activity & ESL_POLL_READ)) { if (esl_recv_event(handle, check_q, save_event)) { status = ESL_FAIL; } @@ -1237,9 +1228,7 @@ static esl_ssize_t handle_recv(esl_handle_t *handle, void *data, esl_size_t data if (handle->connected) { if ((activity = esl_wait_sock(handle->sock, 1000, ESL_POLL_READ|ESL_POLL_ERROR)) > 0) { - if (activity < 0) { - activity = -1; - } else if ((activity & ESL_POLL_ERROR)) { + if ((activity & ESL_POLL_ERROR)) { activity = -1; } else if ((activity & ESL_POLL_READ)) { if (!(activity = recv(handle->sock, data, datalen, 0))) { @@ -1272,10 +1261,6 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_ esl_mutex_lock(handle->mutex); - if (!handle->connected || handle->sock == ESL_SOCK_INVALID) { - goto fail; - } - esl_event_safe_destroy(&handle->last_ievent); if (check_q && handle->race_event) { @@ -1313,7 +1298,7 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_ *e++ = '\0'; while(*e == '\n' || *e == '\r') e++; - if (hname && hval) { + if (hval) { esl_url_decode(hval); esl_log(ESL_LOG_DEBUG, "RECV HEADER [%s] = [%s]\n", hname, hval); if (!strncmp(hval, "ARRAY::", 7)) { @@ -1428,7 +1413,7 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_ hname = beg; hval = col = NULL; - if (hname && (col = strchr(hname, ':'))) { + if ((col = strchr(hname, ':'))) { hval = col + 1; *col = '\0'; while(*hval == ' ') hval++; @@ -1436,7 +1421,7 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_ *c = '\0'; - if (hname && hval) { + if (hval) { esl_url_decode(hval); esl_log(ESL_LOG_DEBUG, "RECV INNER HEADER [%s] = [%s]\n", hname, hval); if (!strcasecmp(hname, "event-name")) { @@ -1541,13 +1526,6 @@ ESL_DECLARE(esl_status_t) esl_send_recv_timed(esl_handle_t *handle, const char * esl_mutex_lock(handle->mutex); - - if (!handle->connected || handle->sock == ESL_SOCK_INVALID) { - handle->connected = 0; - esl_mutex_unlock(handle->mutex); - return ESL_FAIL; - } - esl_event_safe_destroy(&handle->last_sr_event); *handle->last_sr_reply = '\0'; @@ -1593,12 +1571,10 @@ ESL_DECLARE(esl_status_t) esl_send_recv_timed(esl_handle_t *handle, const char * goto recv; } - if (handle->last_sr_event) { - hval = esl_event_get_header(handle->last_sr_event, "reply-text"); + hval = esl_event_get_header(handle->last_sr_event, "reply-text"); - if (!esl_strlen_zero(hval)) { - snprintf(handle->last_sr_reply, sizeof(handle->last_sr_reply), "%s", hval); - } + if (!esl_strlen_zero(hval)) { + snprintf(handle->last_sr_reply, sizeof(handle->last_sr_reply), "%s", hval); } } diff --git a/libs/esl/src/esl_buffer.c b/libs/esl/src/esl_buffer.c index 198c5df41a..d8e206078e 100644 --- a/libs/esl/src/esl_buffer.c +++ b/libs/esl/src/esl_buffer.c @@ -205,11 +205,13 @@ ESL_DECLARE(esl_size_t) esl_buffer_read(esl_buffer_t *buffer, void *data, esl_si ESL_DECLARE(esl_size_t) esl_buffer_packet_count(esl_buffer_t *buffer) { - char *pe, *p, *e, *head = (char *) buffer->head; + char *pe, *p, *e, *head; esl_size_t x = 0; esl_assert(buffer != NULL); + head = (char *) buffer->head; + e = (head + buffer->used); for (p = head; p && *p && p < e; p++) { @@ -228,12 +230,14 @@ ESL_DECLARE(esl_size_t) esl_buffer_packet_count(esl_buffer_t *buffer) ESL_DECLARE(esl_size_t) esl_buffer_read_packet(esl_buffer_t *buffer, void *data, esl_size_t maxlen) { - char *pe, *p, *e, *head = (char *) buffer->head; + char *pe, *p, *e, *head; esl_size_t datalen = 0; esl_assert(buffer != NULL); esl_assert(data != NULL); + head = (char *) buffer->head; + e = (head + buffer->used); for (p = head; p && *p && p < e; p++) { diff --git a/libs/esl/src/esl_threadmutex.c b/libs/esl/src/esl_threadmutex.c index ca1b5bc4cb..cb65b2a627 100644 --- a/libs/esl/src/esl_threadmutex.c +++ b/libs/esl/src/esl_threadmutex.c @@ -126,9 +126,7 @@ esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *dat #endif fail: - if (thread) { - free(thread); - } + free(thread); done: return status; }