Module: check_mk
Branch: master
Commit: e40a965783c86e64ad2b695e6b0323c036d04b44
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e40a965783c86e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Dec 17 09:16:56 2015 +0100
Various const-correctness fixes and associated changes/fixes.
This might fix various segfaults, but there is much more to do in this area.
Some notes:
* We should never use C-style casts, only C++-style casts, the former
contain almost no information what the cast is actually doing.
* Older Nagios headers seem to have bugs regarding const-correctness, so
we sometimes have to const_cast. :-P
---
livestatus/src/AttributelistColumn.cc | 2 +-
livestatus/src/HostFileColumn.cc | 2 +-
livestatus/src/LogCache.cc | 2 +-
livestatus/src/LogEntry.cc | 13 +++++++------
livestatus/src/LogEntry.h | 8 ++++----
livestatus/src/Logfile.cc | 2 +-
livestatus/src/OutputBuffer.cc | 2 +-
livestatus/src/Query.cc | 5 ++++-
livestatus/src/TableServices.cc | 5 +++--
livestatus/src/strutil.cc | 10 +++-------
livestatus/src/strutil.h | 22 +++++++++-------------
11 files changed, 35 insertions(+), 38 deletions(-)
diff --git a/livestatus/src/AttributelistColumn.cc
b/livestatus/src/AttributelistColumn.cc
index c176b34..31f1bc1 100644
--- a/livestatus/src/AttributelistColumn.cc
+++ b/livestatus/src/AttributelistColumn.cc
@@ -115,7 +115,7 @@ Filter *AttributelistColumn::createFilter(int opid, char *value)
else {
char *scan = value;
char *t;
- while ((t = next_token(&scan))) {
+ while ((t = next_token(&scan, ','))) {
unsigned i = 0;
while (al_entries[i].name) {
if (!strcmp(t, al_entries[i].name)) {
diff --git a/livestatus/src/HostFileColumn.cc b/livestatus/src/HostFileColumn.cc
index abd5165..a996ae2 100644
--- a/livestatus/src/HostFileColumn.cc
+++ b/livestatus/src/HostFileColumn.cc
@@ -79,7 +79,7 @@ char *HostFileColumn::getBlob(void *data, int *size)
}
lseek(fd, 0, SEEK_SET);
- char *buffer = (char *)malloc(*size);
+ char *buffer = static_cast<char *>(malloc(*size));
if (!buffer) {
close(fd);
return 0;
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index 68f3189..6ca5d0a 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -147,7 +147,7 @@ void LogCache::updateLogfileIndex()
struct dirent *ent, *result;
int len = offsetof(struct dirent, d_name)
+ pathconf(log_archive_path, _PC_NAME_MAX) + 1;
- ent = (struct dirent *)malloc(len);
+ ent = static_cast<struct dirent *>(malloc(len));
while (0 == readdir_r(dir, ent, &result) && result != 0)
{
diff --git a/livestatus/src/LogEntry.cc b/livestatus/src/LogEntry.cc
index bc6ba13..c1ecae7 100644
--- a/livestatus/src/LogEntry.cc
+++ b/livestatus/src/LogEntry.cc
@@ -303,7 +303,7 @@ bool LogEntry::handleNotificationEntry()
if ((svc && _state == 4) ||
(!svc && _state == 3))
{
- char *swap = _state_type;
+ const char *swap = _state_type;
_state_type = _command_name;
_command_name = swap;
if (svc)
@@ -401,12 +401,12 @@ bool LogEntry::handleProgrammEntry()
}
-int LogEntry::serviceStateToInt(char *s)
+int LogEntry::serviceStateToInt(const char *s)
{
if (!s)
return 3; // can happen at garbled log line
- char *last = s + strlen(s) - 1;
+ const char *last = s + strlen(s) - 1;
if (*last == ')')
last--;
@@ -422,12 +422,12 @@ int LogEntry::serviceStateToInt(char *s)
}
-int LogEntry::hostStateToInt(char *s)
+int LogEntry::hostStateToInt(const char *s)
{
if (!s)
return 2; // can happen at garbled log line
- char *last = s + strlen(s) - 1;
+ const char *last = s + strlen(s) - 1;
if (*last == ')') // handle CUSTOM (UP) and DOWNTIMESTOPPED (DOWN)
last--;
@@ -458,7 +458,8 @@ unsigned LogEntry::updateReferences()
updated++;
}
if (_command_name) {
- _command = find_command(_command_name);
+ // Older Nagios headers are not const-correct... :-P
+ _command = find_command(const_cast<char *>(_command_name));
updated++;
}
return updated;
diff --git a/livestatus/src/LogEntry.h b/livestatus/src/LogEntry.h
index 99a6459..88df07e 100644
--- a/livestatus/src/LogEntry.h
+++ b/livestatus/src/LogEntry.h
@@ -78,10 +78,10 @@ struct LogEntry
char *_text; // points into msg
char *_host_name; // points into msg or is 0
char *_svc_desc; // points into msg or is 0
- char *_command_name;
+ const char *_command_name;
char *_contact_name;
int _state;
- char *_state_type;
+ const char *_state_type;
int _attempt;
char *_check_output;
char *_comment;
@@ -94,8 +94,8 @@ struct LogEntry
LogEntry(unsigned lineno, char *line);
~LogEntry();
unsigned updateReferences();
- static int serviceStateToInt(char *s);
- static int hostStateToInt(char *s);
+ static int serviceStateToInt(const char *s);
+ static int hostStateToInt(const char *s);
private:
bool handleStatusEntry();
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index 5ac21f9..6520312 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -307,7 +307,7 @@ char *Logfile::readIntoBuffer(int *size)
*size = o;
lseek(fd, 0, SEEK_SET);
- char *buffer = (char *)malloc(*size + 2); // add space for binary 0 at beginning and
end
+ char *buffer = static_cast<char *>(malloc(*size + 2)); // add space for binary
0 at beginning and end
if (!buffer) {
logger(LOG_WARNING, "Cannot malloc buffer for reading %s: %s", _path,
strerror(errno));
close(fd);
diff --git a/livestatus/src/OutputBuffer.cc b/livestatus/src/OutputBuffer.cc
index 3bd5f88..2c3da84 100644
--- a/livestatus/src/OutputBuffer.cc
+++ b/livestatus/src/OutputBuffer.cc
@@ -41,7 +41,7 @@
OutputBuffer::OutputBuffer()
: _max_size(INITIAL_OUTPUT_BUFFER_SIZE)
{
- _buffer = (char *)malloc(_max_size);
+ _buffer = static_cast<char *>(malloc(_max_size));
_end = _buffer + _max_size;
reset();
}
diff --git a/livestatus/src/Query.cc b/livestatus/src/Query.cc
index 6c05e35..86e9b85 100644
--- a/livestatus/src/Query.cc
+++ b/livestatus/src/Query.cc
@@ -31,6 +31,7 @@
#include <sys/time.h>
#include <syslog.h>
#include <utility>
+#include <vector>
#include "Aggregator.h"
#include "Column.h"
#include "Filter.h"
@@ -53,6 +54,7 @@ extern unsigned long g_max_response_size;
extern int g_data_encoding;
using std::string;
+using std::vector;
Query::Query(InputBuffer *input, OutputBuffer *output, Table *table) :
_output(output),
@@ -77,7 +79,8 @@ Query::Query(InputBuffer *input, OutputBuffer *output, Table *table) :
while (input->moreLines())
{
string line = input->nextLine();
- char *buffer = (char *)line.c_str();
+ vector<char> hurz(line.begin(), line.end());
+ char *buffer = &hurz[0];
rstrip(buffer);
if (g_debug_level > 0)
logger(LG_INFO, "Query: %s", buffer);
diff --git a/livestatus/src/TableServices.cc b/livestatus/src/TableServices.cc
index b43d4b6..3fe7745 100644
--- a/livestatus/src/TableServices.cc
+++ b/livestatus/src/TableServices.cc
@@ -131,9 +131,10 @@ void TableServices::answerQuery(Query *query)
// do we know the host?
- char *host_name = (char *)query->findIndexFilter("host_name");
+ const char *host_name = static_cast<const
char*>(query->findIndexFilter("host_name"));
if (host_name) {
- host *host = find_host(host_name);
+ // Older Nagios headers are not const-correct... :-P
+ host *host = find_host(const_cast<char*>(host_name));
if (host) {
servicesmember *mem = host->services;
while (mem) {
diff --git a/livestatus/src/strutil.cc b/livestatus/src/strutil.cc
index fedb373..1f09f1e 100644
--- a/livestatus/src/strutil.cc
+++ b/livestatus/src/strutil.cc
@@ -86,15 +86,11 @@ char *next_token(char **c, char delim)
/* same as next_token() but returns "" instead of 0 if
no tokens has been found */
-char *save_next_token(char **c, char delim)
+const char *save_next_token(char **c, char delim)
{
- if (!*c) return (char *)"";
-
+ if (!*c) return "";
char *result = next_token(c, delim);
- if (result == 0)
- return (char *)"";
- else
- return result;
+ return result ? result : "";
}
diff --git a/livestatus/src/strutil.h b/livestatus/src/strutil.h
index c632063..a5203f9 100644
--- a/livestatus/src/strutil.h
+++ b/livestatus/src/strutil.h
@@ -25,25 +25,21 @@
#ifndef strutil_h
#define strutil_h
-#include "config.h" // IWYU pragma: keep
+#include "config.h" // IWYU pragma: keep
#ifdef __cplusplus
extern "C" {
#endif
- char *rstrip(char *);
- char *lstrip(char *);
- int ends_with(const char *a, const char *b);
-#ifdef __cplusplus
- char *next_token(char **c, char delim = ',');
- char *save_next_token(char **c, char delim = ',');
-#else
- char *next_token(char **c, char delim);
- char *save_next_token(char **c, char delim);
-#endif
- char *next_field(char **line);
+
+char *rstrip(char *);
+char *lstrip(char *);
+int ends_with(const char *a, const char *b);
+char *next_token(char **c, char delim);
+const char *save_next_token(char **c, char delim);
+char *next_field(char **line);
+
#ifdef __cplusplus
}
#endif
#endif // strutil_h
-