Skip to content

Commit

Permalink
dlt: Fix warning in source
Browse files Browse the repository at this point in the history
This refactoring work is a preparation for v2.19.x

+ Replace strncpy and strncat with snprintf
+ Increase integrity by bounding and checking
+ Handle strings for file names and paths as null-terminated strings.
+ Git workflow: upload-artifact v3 -> v4
+ Utests disabled, will be handled in next commits

Signed-off-by: minminlittleshrimp <[email protected]>
  • Loading branch information
minminlittleshrimp authored and LUU QUANG MINH committed Feb 20, 2025
1 parent d5b425b commit f91cb9b
Show file tree
Hide file tree
Showing 30 changed files with 590 additions and 831 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake-ctest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
BUILD_TYPE: Release
WITH_DLT_COVERAGE: ON
WITH_DLT_COVERAGE: OFF
BUILD_GMOCK: OFF

jobs:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
run: bash util/dlt_coverage_report/lcov_report_generator.sh build -xe

- name: Archive coverage results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4 # Updated to v4
with:
name: dlt_coverage_report
path: ${{github.workspace}}/dlt_lcov_report
Expand Down
10 changes: 4 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
#######

# Set minimum Cmake version and setup policy behavior
cmake_minimum_required(VERSION 3.3)
cmake_minimum_required(VERSION 3.10)

if(${CMAKE_VERSION} VERSION_GREATER "3.20" OR ${CMAKE_VERSION} VERSION_EQUAL "3.20")
cmake_policy(SET CMP0115 OLD)
endif()
project(automotive-dlt VERSION 2.18.10)

project(automotive-dlt VERSION 2.18.10)

mark_as_advanced(CMAKE_BACKWARDS_COMPATIBILITY)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
Expand Down Expand Up @@ -190,7 +187,7 @@ if(WITH_DLT_DAEMON_VSOCK_IPC OR WITH_DLT_LIB_VSOCK_IPC)
endif()

if(NOT DLT_USER_IPC_PATH)
set(DLT_USER_IPC_PATH "/tmp")
set(DLT_USER_IPC_PATH "/tmp" CACHE STRING "IPC Path for DLT")
endif()

add_definitions(-DDLT_USER_IPC_PATH="${DLT_USER_IPC_PATH}")
Expand Down Expand Up @@ -245,6 +242,7 @@ add_compile_options(
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++14>
-Wall
-Wextra
-Werror
# -pedantic
-Wno-variadic-macros
-Wno-strict-aliasing
Expand Down
6 changes: 3 additions & 3 deletions include/dlt/dlt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void dlt_client_register_fetch_next_message_callback(bool (*registerd_callback)(
* @param verbose if set to true verbose information is printed out.
* @return negative value if there was an error
*/
int dlt_client_init_port(DltClient *client, int port, int verbose);
DltReturnValue dlt_client_init_port(DltClient *client, int port, int verbose);

/**
* Initialising dlt client structure
Expand Down Expand Up @@ -196,7 +196,7 @@ DltReturnValue dlt_client_send_log_level(DltClient *client, char *apid, char *ct
* @param client pointer to dlt client structure
* @return negative value if there was an error
*/
int dlt_client_get_log_info(DltClient *client);
DltReturnValue dlt_client_get_log_info(DltClient *client);
/**
* Send an request to get default log level to the dlt daemon
* @param client pointer to dlt client structure
Expand All @@ -208,7 +208,7 @@ DltReturnValue dlt_client_get_default_log_level(DltClient *client);
* @param client pointer to dlt client structure
* @return negative value if there was an error
*/
int dlt_client_get_software_version(DltClient *client);
DltReturnValue dlt_client_get_software_version(DltClient *client);
/**
* Initialise get log info structure
* @return void
Expand Down
2 changes: 1 addition & 1 deletion src/console/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if(NOT WITH_DLT_CONSOLE_WO_SBTM)
endif()

foreach(target IN LISTS TARGET_LIST)
set(target_SRCS ${target})
set(target_SRCS ${target}.c)
add_executable(${target} ${target_SRCS})
target_link_libraries(${target} dlt dlt_control_common_lib)
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C)
Expand Down
2 changes: 1 addition & 1 deletion src/console/dlt-passive-node-ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void set_node_id(char *id)
exit(-1);
}
else {
strncpy(g_options.node_id, id, DLT_ID_SIZE);
snprintf(g_options.node_id, sizeof(g_options.node_id), "%s", id);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if (WITH_SYSTEMD_SOCKET_ACTIVATION)
target_link_libraries(dlt-daemon systemd)
endif()
if (WITH_DLT_LOGSTORAGE_GZIP)
target_link_libraries(dlt-daemon ${ZLIB_LIBRARY})
target_link_libraries(dlt-daemon ZLIB::ZLIB)
endif()

install(TARGETS dlt-daemon
Expand All @@ -86,6 +86,7 @@ if (WITH_DLT_UNIT_TESTS)
endif(WITH_SYSTEMD_WATCHDOG OR WITH_SYSTEMD)

add_library(dlt_daemon ${library_SRCS})
target_compile_definitions(dlt_daemon PRIVATE DLT_USER_IPC_PATH="${DLT_USER_IPC_PATH}")
target_link_libraries(dlt_daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})

install(TARGETS dlt_daemon
Expand Down
41 changes: 22 additions & 19 deletions src/daemon/dlt-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,19 @@ int option_handling(DltDaemonLocal *daemon_local, int argc, char *argv[])
/* switch() */

#ifdef DLT_DAEMON_USE_FIFO_IPC
snprintf(daemon_local->flags.userPipesDir, DLT_PATH_MAX,
int str_ret = snprintf(daemon_local->flags.userPipesDir, DLT_PATH_MAX,
"%s/dltpipes", dltFifoBaseDir);
snprintf(daemon_local->flags.daemonFifoName, DLT_PATH_MAX,
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}

str_ret = snprintf(daemon_local->flags.daemonFifoName, DLT_PATH_MAX,
"%s/dlt", dltFifoBaseDir);
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}
#endif

#ifdef DLT_SHM_ENABLE
Expand All @@ -387,7 +396,7 @@ int option_file_parser(DltDaemonLocal *daemon_local)
char value[value_length];
char *pch;
const char *filename;
ssize_t n;
int str_ret;

/* set default values for configuration */
daemon_local->flags.sharedMemorySize = DLT_SHM_SIZE;
Expand All @@ -400,18 +409,18 @@ int option_file_parser(DltDaemonLocal *daemon_local)
daemon_local->flags.loggingLevel = LOG_INFO;

#ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
n = snprintf(daemon_local->flags.loggingFilename,
str_ret = snprintf(daemon_local->flags.loggingFilename,
sizeof(daemon_local->flags.loggingFilename),
"%s/dlt.log", DLT_USER_IPC_PATH);
#else /* DLT_DAEMON_USE_FIFO_IPC */
n = snprintf(daemon_local->flags.loggingFilename,
str_ret = snprintf(daemon_local->flags.loggingFilename,
sizeof(daemon_local->flags.loggingFilename),
"%s/dlt.log", dltFifoBaseDir);
#endif

if (n < 0 || (size_t)n > sizeof(daemon_local->flags.loggingFilename)) {
dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%ld) %s\n",
__func__, n, daemon_local->flags.loggingFilename);
if (str_ret < 0 || (unsigned long)str_ret > sizeof(daemon_local->flags.loggingFilename)) {
dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%d) %s\n",
__func__, str_ret, daemon_local->flags.loggingFilename);
}
daemon_local->flags.enableLoggingFileLimit = false;
daemon_local->flags.loggingFileSize = 250000;
Expand Down Expand Up @@ -1308,7 +1317,7 @@ int trace_load_config_file_parser(DltDaemon *daemon, DltDaemonLocal *daemon_loca
}
#endif

static int dlt_mkdir_recursive(const char *dir)
int dlt_mkdir_recursive(const char *dir)
{
int ret = 0;
char tmp[PATH_MAX + 1];
Expand Down Expand Up @@ -1340,8 +1349,6 @@ static int dlt_mkdir_recursive(const char *dir)
*p = '/';
}



if ((ret == 0) || ((ret == -1) && (errno == EEXIST)))
ret = mkdir(tmp,
#ifdef DLT_DAEMON_USE_FIFO_IPC
Expand Down Expand Up @@ -1464,18 +1471,15 @@ int main(int argc, char *argv[])

/* Make sure the parent user directory is created */
#ifdef DLT_DAEMON_USE_FIFO_IPC

if (dlt_mkdir_recursive(dltFifoBaseDir) != 0) {
dlt_vlog(LOG_ERR, "Base dir %s cannot be created!\n", dltFifoBaseDir);
return -1;
}

#else
if (dlt_mkdir_recursive(DLT_USER_IPC_PATH) != 0) {
dlt_vlog(LOG_ERR, "Base dir %s cannot be created!\n", daemon_local.flags.appSockPath);
return -1;
}

#endif

/* --- Daemon init phase 1 begin --- */
Expand Down Expand Up @@ -2410,11 +2414,10 @@ void dlt_daemon_exit_trigger()
#ifdef DLT_DAEMON_USE_FIFO_IPC
char tmp[DLT_PATH_MAX] = { 0 };

ssize_t n;
n = snprintf(tmp, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
if (n < 0 || (size_t)n > DLT_PATH_MAX) {
dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%ld) %s\n",
__func__, n, tmp);
int str_ret = snprintf(tmp, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
if (str_ret < 0 || str_ret > DLT_PATH_MAX) {
dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%d) %s\n",
__func__, str_ret, tmp);
}

(void)unlink(tmp);
Expand Down
6 changes: 5 additions & 1 deletion src/daemon/dlt_daemon_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,15 @@ DltDaemonApplication *dlt_daemon_application_add(DltDaemon *daemon,
#endif
#ifdef DLT_DAEMON_USE_FIFO_IPC
if (dlt_user_handle < DLT_FD_MINIMUM) {
snprintf(filename,
int str_ret = snprintf(filename,
DLT_DAEMON_COMMON_TEXTBUFSIZE,
"%s/dltpipes/dlt%d",
dltFifoBaseDir,
pid);
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return (DltDaemonApplication *)NULL;
}

dlt_user_handle = open(filename, O_WRONLY | O_NONBLOCK);

Expand Down
2 changes: 1 addition & 1 deletion src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ endif()

foreach(TARGET IN LISTS TARGET_LIST)
set(TARGET_SRCS ${TARGET})
add_executable(${TARGET} ${TARGET_SRCS})
add_executable(${TARGET} ${TARGET_SRCS}.c)
target_link_libraries(${TARGET} dlt)
set_target_properties(${TARGET} PROPERTIES LINKER_LANGUAGE C)
install(TARGETS ${TARGET}
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/dlt_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ int dlt_gateway_forward_control_message(DltGateway *gateway,

int dlt_gateway_process_on_demand_request(DltGateway *gateway,
DltDaemonLocal *daemon_local,
char node_id[DLT_ID_SIZE],
char *node_id,
int connection_status,
int verbose)
{
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/dlt_gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ DltReceiver *dlt_gateway_get_connection_receiver(DltGateway *g, int fd);
* @param verbose verbose flag
* @return 0 on success, -1 otherwise
*/
int dlt_gateway_process_passive_node_messages(DltDaemon *daemon,
DltReturnValue dlt_gateway_process_passive_node_messages(DltDaemon *daemon,
DltDaemonLocal *daemon_local,
DltReceiver *recv,
int verbose);
Expand Down
27 changes: 21 additions & 6 deletions src/lib/dlt_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,16 @@ static DltReturnValue dlt_initialize_fifo_connection(void)
char filename[DLT_PATH_MAX];
int ret;

snprintf(dlt_user_dir, DLT_PATH_MAX, "%s/dltpipes", dltFifoBaseDir);
snprintf(dlt_daemon_fifo, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
int str_ret = snprintf(dlt_user_dir, DLT_PATH_MAX, "%s/dltpipes", dltFifoBaseDir);
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}
str_ret = snprintf(dlt_daemon_fifo, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}
ret = mkdir(dlt_user_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX);

if ((ret == -1) && (errno != EEXIST)) {
Expand All @@ -424,8 +432,11 @@ static DltReturnValue dlt_initialize_fifo_connection(void)
}

/* create and open DLT user FIFO */
snprintf(filename, DLT_PATH_MAX, "%s/dlt%d", dlt_user_dir, getpid());

str_ret = snprintf(filename, DLT_PATH_MAX, "%s/dlt%d", dlt_user_dir, getpid());
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}
/* Try to delete existing pipe, ignore result of unlink */
unlink(filename);

Expand Down Expand Up @@ -761,7 +772,7 @@ DltReturnValue dlt_init_common(void)
}

/* Binary semaphore for threads */
if ((pthread_attr_init(&dlt_mutex_attr) != 0) ||
if ((pthread_mutexattr_init(&dlt_mutex_attr) != 0) ||
(pthread_mutexattr_settype(&dlt_mutex_attr, PTHREAD_MUTEX_ERRORCHECK) != 0) ||
(pthread_mutex_init(&dlt_mutex, &dlt_mutex_attr) != 0)) {
dlt_user_init_state = INIT_UNITIALIZED;
Expand Down Expand Up @@ -1079,7 +1090,11 @@ DltReturnValue dlt_free(void)
if (dlt_user.dlt_user_handle != DLT_FD_INIT) {
close(dlt_user.dlt_user_handle);
dlt_user.dlt_user_handle = DLT_FD_INIT;
snprintf(filename, DLT_PATH_MAX, "%s/dlt%d", dlt_user_dir, getpid());
int str_ret = snprintf(filename, DLT_PATH_MAX, "%s/dlt%d", dlt_user_dir, getpid());
if (str_ret < 0 || str_ret >= DLT_PATH_MAX) {
fprintf(stderr, "Error: Path truncated or snprintf failed.\n");
return -1;
}
unlink(filename);
}

Expand Down
Loading

0 comments on commit f91cb9b

Please sign in to comment.