From 15a9db7342fabeb7c322dec657e0b4b0855a9a01 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 4 Oct 2026 13:00:19 +0300 Subject: [PATCH] perf: updated error checks --- README.md | 13 +++++------- include/zh_syslog.h | 25 ++++++++++++----------- zh_syslog.c | 48 +++++++++++++++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 31e6500..ea635f6 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,5 @@ # ESP32 ESP-IDF component for Syslog UDP client -## SAST Tools - -[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) — static analyzer for C, C++, C#, and Java code. - ## Features 1. Support for all standard syslog facility codes (0-23) per RFC 5424. @@ -51,11 +47,11 @@ Messages are built using the format defined in [RFC 5424](https://datatracker.ie #define SYSLOG_IP "8.8.8.8" -esp_timer_handle_t wifi_reconnect_timer = {0}; +esp_timer_handle_t wifi_reconnect_timer = NULL; uint8_t wifi_reconnect_retry_num = 0; -EventGroupHandle_t event_group_handle = {0}; +EventGroupHandle_t event_group_handle = NULL; -bool flag = false; +volatile bool flag = false; void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); @@ -75,8 +71,8 @@ void app_main(void) esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL); esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL); - esp_wifi_start(); event_group_handle = xEventGroupCreate(); + esp_wifi_start(); xEventGroupWaitBits(event_group_handle, WIFI_CONNECT_SUCCESS, pdTRUE, pdTRUE, portMAX_DELAY); zh_syslog_init_config_t config = ZH_SYSLOG_INIT_CONFIG_DEFAULT(); strcpy(config.syslog_ip, SYSLOG_IP); @@ -121,6 +117,7 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id { esp_timer_stop(wifi_reconnect_timer); esp_timer_delete(wifi_reconnect_timer); + wifi_reconnect_timer = NULL; } esp_timer_create_args_t wifi_reconnect_timer_args = { .callback = (void *)esp_wifi_connect, diff --git a/include/zh_syslog.h b/include/zh_syslog.h index 41a586d..3581596 100644 --- a/include/zh_syslog.h +++ b/include/zh_syslog.h @@ -10,8 +10,8 @@ * - Support for all standard syslog facility codes (0-23) * - Support for all standard syslog severity levels (0-7) * - RFC 5424 format message generation - * - Thread-safe initialization and deinitialization - * - MALLOC_CAP_8BIT memory allocation for DMA compatibility + * - Short messages are formatted into a stack buffer, longer ones fall back + * to a heap allocation with the exact required size * * @note Connection to the network must be established before calling zh_syslog_init(). * @warning This library does not guarantee message delivery (UDP is connectionless). @@ -19,17 +19,15 @@ #pragma once +#include "stdio.h" +#include "string.h" +#include "stdint.h" #include "stdbool.h" +#include "esp_heap_caps.h" #include "esp_err.h" #include "esp_log.h" -#include "lwip/err.h" #include "lwip/sockets.h" -#include "lwip/sys.h" -#include "lwip/netdb.h" -/** - * @brief Default syslog client initialization configuration. - */ #define ZH_SYSLOG_INIT_CONFIG_DEFAULT() \ { \ .syslog_ip = "192.168.0.1", \ @@ -116,10 +114,11 @@ extern "C" * Creates a UDP socket and configures connection parameters * for the remote syslog server. * - * @param[in] config Pointer to syslog client initialization configuration structure. Can point to a temporary variable (must not be NULL) + * @param[in] config Pointer to syslog client initialization configuration structure (must not be NULL) * * @return ESP_OK on success - * @return ESP_ERR_INVALID_ARG if config is NULL + * @return ESP_ERR_INVALID_ARG if any parameter is NULL, port is out of range, or IP is malformed + * @return ESP_ERR_INVALID_STATE if client is already initialized * @return ESP_FAIL if socket creation fails * * @note Connection to the network must be established before calling this function. @@ -147,12 +146,12 @@ extern "C" * * @param[in] facility Syslog facility code (0-23) * @param[in] severity Syslog severity code (0-7) - * @param[in] hostname Pointer to device hostname that generated the message. Only alphanumeric characters without spaces (must not be NULL) - * @param[in] app_name Pointer to application name that generated the message. Only alphanumeric characters without spaces (must not be NULL) + * @param[in] hostname Pointer to device hostname that generated the message (must not be NULL) + * @param[in] app_name Pointer to application name that generated the message (must not be NULL) * @param[in] message Pointer to the message payload to send (must not be NULL) * * @return ESP_OK on success - * @return ESP_ERR_INVALID_ARG if hostname, app_name, or message is NULL + * @return ESP_ERR_INVALID_ARG if hostname, app_name, or message is NULL, or facility/severity is out of range * @return ESP_ERR_INVALID_STATE if zh_syslog_init() was not called previously * @return ESP_ERR_NO_MEM if memory allocation fails * @return ESP_FAIL if UDP send operation fails diff --git a/zh_syslog.c b/zh_syslog.c index d7cd4c1..e2d22f6 100644 --- a/zh_syslog.c +++ b/zh_syslog.c @@ -13,19 +13,22 @@ static const char *TAG = "zh_syslog"; -static zh_syslog_init_config_t _init_config = {0}; /*!< Syslog client initialization configuration (internal copy) */ -static struct sockaddr_in _syslog_server = {0}; /*!< Pre-configured syslog server address */ -volatile static int _socket = 0; /*!< UDP socket file descriptor */ -volatile static bool _is_initialized = false; /*!< Initialization status flag */ +static struct sockaddr_in _syslog_server = {0}; +volatile static int _socket = -1; +volatile static bool _is_initialized = false; esp_err_t zh_syslog_init(const zh_syslog_init_config_t *config) { ESP_LOGI(TAG, "Syslog client initialization begin."); ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument."); - _init_config = *config; - _syslog_server.sin_addr.s_addr = inet_addr(_init_config.syslog_ip); + ZH_ERROR_CHECK(_is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Syslog client initialization fail. Syslog client already initialized."); + ZH_ERROR_CHECK(config->syslog_port != 0 && config->syslog_port <= 65535, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument."); + ZH_ERROR_CHECK(memchr(config->syslog_ip, '\0', sizeof(config->syslog_ip)) != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid IP."); + struct in_addr addr; + ZH_ERROR_CHECK(inet_pton(AF_INET, config->syslog_ip, &addr) == 1, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument."); + _syslog_server.sin_addr = addr; _syslog_server.sin_family = AF_INET; - _syslog_server.sin_port = htons(_init_config.syslog_port); + _syslog_server.sin_port = htons((uint16_t)config->syslog_port); _socket = socket(AF_INET, SOCK_DGRAM, 0); ZH_ERROR_CHECK(_socket >= 0, ESP_FAIL, NULL, "Syslog client initialization fail. Create socket error."); _is_initialized = true; @@ -37,24 +40,39 @@ esp_err_t zh_syslog_deinit(void) { ESP_LOGI(TAG, "Syslog client deinitialization begin."); ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client deinitialization fail. Syslog client not initialized."); - shutdown(_socket, 0); close(_socket); + _socket = -1; _is_initialized = false; ESP_LOGI(TAG, "Syslog client deinitialization success."); return ESP_OK; } -esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message) // -V2008 +esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message) { ESP_LOGI(TAG, "Syslog client send begin."); ZH_ERROR_CHECK(hostname != NULL && app_name != NULL && message != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument."); ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client send fail. Syslog client not initialized."); - char *payload = heap_caps_calloc(1, 1024, MALLOC_CAP_8BIT); - ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap."); - sprintf(payload, "<%d>1 - %s %s - - - %s", ((uint8_t)facility * 8) + (uint8_t)severity, hostname, app_name, message); - int temp = sendto(_socket, payload, strlen(payload), 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server)); // -V641 - ZH_ERROR_CHECK(temp >= 0, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Socket error."); - heap_caps_free(payload); + ZH_ERROR_CHECK(facility >= ZH_KERN && facility <= ZH_LOCAL7 && severity >= ZH_EMERG && severity <= ZH_DEBUG, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument."); + const int pri = ((uint8_t)facility * 8) + (uint8_t)severity; + char stack_buf[256]; + int len = snprintf(stack_buf, sizeof(stack_buf), "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message); + ZH_ERROR_CHECK(len >= 0, ESP_FAIL, NULL, "Syslog client send fail. Format error."); + char *payload = stack_buf; + bool heap_allocated = false; + if (len >= sizeof(stack_buf)) + { + payload = heap_caps_calloc(1, len + 1, MALLOC_CAP_8BIT); + ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap."); + int written = snprintf(payload, len + 1, "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message); + ZH_ERROR_CHECK(written == len, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Format error."); + heap_allocated = true; + } + int sent = sendto(_socket, payload, len, 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server)); + if (heap_allocated == true) + { + heap_caps_free(payload); + } + ZH_ERROR_CHECK(sent == len, ESP_FAIL, NULL, "Syslog client send fail. Socket error."); ESP_LOGI(TAG, "Syslog client send success."); return ESP_OK; } \ No newline at end of file