diff --git a/src/debug_log.cc b/src/debug_log.cc index 28f2f6ff..ac240bf0 100644 --- a/src/debug_log.cc +++ b/src/debug_log.cc @@ -29,7 +29,8 @@ DebugLog::~DebugLog() { DebugLogWriter::getInstance().close(m_fileName); } -void DebugLog::setDebugLogFile(const std::string& fileName, std::string *error) { +void DebugLog::setDebugLogFile(const std::string& fileName, + std::string *error) { if (isLogFileSet()) { DebugLogWriter::getInstance().close(m_fileName); } diff --git a/src/debug_log_writer.cc b/src/debug_log_writer.cc index eb6d57e9..9c89f7d7 100644 --- a/src/debug_log_writer.cc +++ b/src/debug_log_writer.cc @@ -17,9 +17,7 @@ #include #include -#include #include -#include #include #include #include @@ -27,6 +25,8 @@ #include #include +#include + namespace modsecurity { @@ -37,7 +37,7 @@ debug_log_file_handler_t *DebugLogWriter::find_handler( if (current->file_name == fileName) { return current; } - current = (debug_log_file_handler_t *) current->next; + current = reinterpret_cast(current->next); } return NULL; @@ -55,7 +55,7 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler( FILE *fp; fp = fopen(fileName.c_str(), "a"); - if (fp <= 0) { + if (fp == 0) { error->assign("Failed to open file: " + fileName); goto err_fh; } @@ -64,14 +64,14 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler( if (mem_key_structure < 0) { error->assign("Failed to select key for the shared memory (1): "); error->append(strerror(errno)); - goto err_mem_key1; + goto err_mem_key; } mem_key_file_name = ftok(fileName.c_str(), 2); if (mem_key_file_name < 0) { error->assign("Failed to select key for the shared memory (2): "); error->append(strerror(errno)); - goto err_mem_key1; + goto err_mem_key; } shm_id = shmget(mem_key_structure, sizeof (debug_log_file_handler_t), @@ -82,8 +82,9 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler( goto err_shmget1; } - new_debug_log = (debug_log_file_handler_t *) shmat(shm_id, NULL, 0); - if (((char *)new_debug_log)[0] == -1) { + new_debug_log = reinterpret_cast( + shmat(shm_id, NULL, 0)); + if ((reinterpret_cast(new_debug_log)[0]) == -1) { error->assign("Failed to attach shared memory (1): "); error->append(strerror(errno)); goto err_shmat1; @@ -104,14 +105,14 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler( goto err_shmget2; } new_debug_log->shm_id_file_name = shm_id; - shm_ptr2 = (char *) shmat(shm_id, NULL, 0); + shm_ptr2 = reinterpret_cast(shmat(shm_id, NULL, 0)); if (shm_ptr2[0] == -1) { error->assign("Failed to attach shared memory (2): "); error->append(strerror(errno)); goto err_shmat2; } - memset(shm_ptr2, '\0', sizeof (debug_log_file_handler_t)); memcpy(shm_ptr2, fileName.c_str(), fileName.size()); + shm_ptr2[fileName.size()] = '\0'; new_debug_log->file_name = shm_ptr2; @@ -126,7 +127,8 @@ debug_log_file_handler_t *DebugLogWriter::add_new_handler( new_debug_log->next = NULL; current = NULL; } else { - current = (debug_log_file_handler_t *) current->next; + current = reinterpret_cast( + current->next); } } } @@ -139,8 +141,7 @@ err_shmat2: err_shmget1: err_shmat1: shmdt(new_debug_log); -err_mem_key2: -err_mem_key1: +err_mem_key: err_fh: return NULL; } @@ -167,7 +168,6 @@ int DebugLogWriter::open(const std::string& fileName, std::string *error) { void DebugLogWriter::close(const std::string& fileName) { debug_log_file_handler_t *a; - bool first = false; if (fileName.empty()) { return; @@ -181,19 +181,20 @@ void DebugLogWriter::close(const std::string& fileName) { a->using_it--; if (a->using_it == 0) { + bool first = false; int shm_id1 = a->shm_id_structure; int shm_id2 = a->shm_id_file_name; debug_log_file_handler_t *p , *n; pthread_mutex_lock(&a->lock); fclose(a->fp); - p = (debug_log_file_handler_t *) a->previous; - n = (debug_log_file_handler_t *) a->next; + p = reinterpret_cast(a->previous); + n = reinterpret_cast(a->next); if (p != NULL) { - p->next = (debug_log_file_handler_t *) n; + p->next = reinterpret_cast(n); } if (n != NULL) { - n->previous = (debug_log_file_handler_t *) p; + n->previous = reinterpret_cast(p); } a->previous = NULL; a->next = NULL; @@ -211,7 +212,7 @@ void DebugLogWriter::close(const std::string& fileName) { shmctl(shm_id2, IPC_RMID, NULL); if (first) { - m_first == NULL; + m_first = NULL; } a = NULL; } @@ -230,7 +231,8 @@ void DebugLogWriter::write_log(const std::string& fileName, } pthread_mutex_lock(&a->lock); - wrote = write(a->file_handler, (void *)lmsg.c_str(), lmsg.size()); + wrote = write(a->file_handler, reinterpret_cast(lmsg.c_str()), + lmsg.size()); if (wrote < msg.size()) { std::cerr << "failed to write debug log: " << msg; } diff --git a/src/debug_log_writer.h b/src/debug_log_writer.h index 04d33f83..73993eb1 100644 --- a/src/debug_log_writer.h +++ b/src/debug_log_writer.h @@ -13,17 +13,17 @@ * */ -#include -#include -#include - -#include -#include #include #include #include #include +#include +#include +#include +#include + + #ifndef SRC_DEBUG_LOG_WRITER_H_ #define SRC_DEBUG_LOG_WRITER_H_