Module: check_mk
Branch: master
Commit: 39643f27f6339e442a5d24258462f4c953e1f9bb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=39643f27f6339e…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Sep 1 09:53:39 2017 +0200
Make access of row data const-correct.
Again, this is an important step: The compiler statically verifies that the
Livestatus threads don't accidentally modify the contents of table rows.
Change-Id: Ifeb9d5bb882113f4434ba1ace6dcb73fdd5c5279
---
livestatus/src/Column.cc | 15 ++++++++-------
livestatus/src/Column.h | 2 +-
livestatus/src/Row.h | 8 ++++----
livestatus/src/Table.h | 2 +-
livestatus/src/TableColumns.h | 3 ++-
livestatus/src/TableEventConsole.cc | 4 ++--
livestatus/src/TableEventConsole.h | 14 ++++++++------
livestatus/src/auth.cc | 13 +++++++------
livestatus/src/auth.h | 2 +-
9 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/livestatus/src/Column.cc b/livestatus/src/Column.cc
index a0b14de..f8b7789 100644
--- a/livestatus/src/Column.cc
+++ b/livestatus/src/Column.cc
@@ -42,16 +42,17 @@ Column::Column(string name, string description, int indirect_offset,
, _extra_extra_offset(extra_extra_offset) {}
namespace {
-void *shift(void *data, int offset) {
- return (data == nullptr || offset < 0) ? data
- : *offset_cast<void *>(data, offset);
+const void *shift(const void *data, int offset) {
+ return (data == nullptr || offset < 0)
+ ? data
+ : *offset_cast<const void *>(data, offset);
}
} // namespace
-void *Column::shiftPointer(Row row) const {
- return shift(
- shift(shift(row.rawData<void>(), _indirect_offset), _extra_offset),
- _extra_extra_offset);
+const void *Column::shiftPointer(Row row) const {
+ return shift(shift(shift(row.rawData<const void>(), _indirect_offset),
+ _extra_offset),
+ _extra_extra_offset);
}
unique_ptr<Filter> Column::createFilter(RelationalOperator /*unused*/,
diff --git a/livestatus/src/Column.h b/livestatus/src/Column.h
index a1494dc..749f9d2 100644
--- a/livestatus/src/Column.h
+++ b/livestatus/src/Column.h
@@ -81,7 +81,7 @@ private:
int _extra_offset;
int _extra_extra_offset;
- void *shiftPointer(Row row) const;
+ const void *shiftPointer(Row row) const;
};
#endif // Column_h
diff --git a/livestatus/src/Row.h b/livestatus/src/Row.h
index 507aee3..59401c2 100644
--- a/livestatus/src/Row.h
+++ b/livestatus/src/Row.h
@@ -30,18 +30,18 @@
class Row {
public:
// Here we basically forget the actual type of the row...
- explicit Row(void *ptr) : _ptr(ptr) {}
+ explicit Row(const void *ptr) : _ptr(ptr) {}
// ... and here we reconstruct it, hopefully in a correct way. :-/
template <typename T>
- T *rawData() const {
- return static_cast<T *>(_ptr);
+ const T *rawData() const {
+ return static_cast<const T *>(_ptr);
}
bool isNull() const { return _ptr == nullptr; }
private:
- void *_ptr;
+ const void *_ptr;
};
#endif // Row_h
diff --git a/livestatus/src/Table.h b/livestatus/src/Table.h
index 56529fa..5ae2b60 100644
--- a/livestatus/src/Table.h
+++ b/livestatus/src/Table.h
@@ -94,7 +94,7 @@ public:
virtual Row findObject(const std::string &objectspec);
template <typename T>
- T *rowData(Row row) const {
+ const T *rowData(Row row) const {
return row.rawData<T>();
}
diff --git a/livestatus/src/TableColumns.h b/livestatus/src/TableColumns.h
index ef4b5f0..2ac5ce8 100644
--- a/livestatus/src/TableColumns.h
+++ b/livestatus/src/TableColumns.h
@@ -45,7 +45,8 @@ public:
void answerQuery(Query *query) override;
void addTable(Table *);
- std::string getValue(const Column *column, ColumnsColumn::Type colcol) const;
+ std::string getValue(const Column *column,
+ ColumnsColumn::Type colcol) const;
std::string tableNameOf(const Column *column) const;
};
diff --git a/livestatus/src/TableEventConsole.cc b/livestatus/src/TableEventConsole.cc
index 6b495a8..bded8c6 100644
--- a/livestatus/src/TableEventConsole.cc
+++ b/livestatus/src/TableEventConsole.cc
@@ -132,7 +132,7 @@ bool TableEventConsole::isAuthorizedForEvent(Row row, contact *ctc) {
}
bool TableEventConsole::isAuthorizedForEventViaContactGroups(
- MonitoringCore::Contact *ctc, ECRow *row, bool &result) {
+ MonitoringCore::Contact *ctc, const ECRow *row, bool &result) {
auto col = static_pointer_cast<ListEventConsoleColumn>(
column("event_contact_groups"));
if (col->isNone(row)) {
@@ -148,7 +148,7 @@ bool TableEventConsole::isAuthorizedForEventViaContactGroups(
}
bool TableEventConsole::isAuthorizedForEventViaHost(
- MonitoringCore::Contact *ctc, ECRow *row, bool &result) {
+ MonitoringCore::Contact *ctc, const ECRow *row, bool &result) {
if (MonitoringCore::Host *hst = row->_host) {
return (result = core()->host_has_contact(hst, ctc), true);
}
diff --git a/livestatus/src/TableEventConsole.h b/livestatus/src/TableEventConsole.h
index 79acae7..1cc66b6 100644
--- a/livestatus/src/TableEventConsole.h
+++ b/livestatus/src/TableEventConsole.h
@@ -72,7 +72,7 @@ protected:
const std::function<T(std::string)> &f)
: _column(column), _default_value(default_value), _f(f) {}
- std::string getRaw(ECRow *row) const {
+ std::string getRaw(const ECRow *row) const {
return row == nullptr ? "" : row->_map.at(_column.name());
}
@@ -157,9 +157,11 @@ protected:
: mk::split(x.substr(1), '\001');
}) {}
- bool isNone(ECRow *row) { return _ecc.getRaw(row) == "\002"; }
+ bool isNone(const ECRow *row) const {
+ return _ecc.getRaw(row) == "\002";
+ }
- _column_t getValue(Row row) { return _ecc.getValue(row); }
+ _column_t getValue(Row row) const { return _ecc.getValue(row); }
void output(Row row, RowRenderer &r,
const contact * /* auth_user */) const override {
@@ -198,9 +200,9 @@ protected:
private:
bool isAuthorizedForEventViaContactGroups(MonitoringCore::Contact *ctc,
- ECRow *row, bool &result);
- bool isAuthorizedForEventViaHost(MonitoringCore::Contact *ctc, ECRow *row,
- bool &result);
+ const ECRow *row, bool &result);
+ bool isAuthorizedForEventViaHost(MonitoringCore::Contact *ctc,
+ const ECRow *row, bool &result);
};
#endif // TableEventConsole_h
diff --git a/livestatus/src/auth.cc b/livestatus/src/auth.cc
index 22a6410..a9b36c1 100644
--- a/livestatus/src/auth.cc
+++ b/livestatus/src/auth.cc
@@ -36,19 +36,20 @@ bool host_has_contact(const host *hst, const contact *ctc) {
const_cast<contact *>(ctc)) != 0;
}
-bool service_has_contact(MonitoringCore *mc, const host *hst, service *svc,
- const contact *ctc) {
+bool service_has_contact(MonitoringCore *mc, const host *hst,
+ const service *svc, const contact *ctc) {
// Older Nagios headers are not const-correct... :-P
- return is_contact_for_service(svc, const_cast<contact *>(ctc)) != 0 ||
- is_escalated_contact_for_service(svc, const_cast<contact *>(ctc)) !=
- 0 ||
+ return is_contact_for_service(const_cast<service *>(svc),
+ const_cast<contact *>(ctc)) != 0 ||
+ is_escalated_contact_for_service(const_cast<service *>(svc),
+ const_cast<contact *>(ctc)) != 0 ||
(mc->serviceAuthorization() == AuthorizationKind::loose &&
host_has_contact(hst, ctc));
}
} // namespace
bool is_authorized_for(MonitoringCore *mc, const contact *ctc, const host *hst,
- service *svc) {
+ const service *svc) {
return ctc != unknown_auth_user() &&
(svc == nullptr ? host_has_contact(hst, ctc)
: service_has_contact(mc, hst, svc, ctc));
diff --git a/livestatus/src/auth.h b/livestatus/src/auth.h
index 15a25f3..1a10c66 100644
--- a/livestatus/src/auth.h
+++ b/livestatus/src/auth.h
@@ -43,7 +43,7 @@ inline contact *unknown_auth_user() {
contact *unknown_auth_user();
class MonitoringCore;
bool is_authorized_for(MonitoringCore *mc, const contact *ctc, const host *hst,
- service *svc);
+ const service *svc);
#endif
#endif // auth_h