Skip to content

Commit

Permalink
Protect macros from user code to the left of them
Browse files Browse the repository at this point in the history
  • Loading branch information
romkatv committed Feb 16, 2019
1 parent 41f4bf9 commit 4db0631
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/glog/logging.h.in
Expand Up @@ -568,8 +568,10 @@ class LogSink; // defined below
LOG_TO_STRING_##severity(static_cast<std::vector<std::string>*>(outvec)).stream()

#define LOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
#define SYSLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity)

#define LOG_ASSERT(condition) \
Expand Down Expand Up @@ -859,6 +861,7 @@ DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
&@ac_google_namespace@::LogMessage::SendToLog)

#define PLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity)

// A CHECK() macro that postpends errno if the condition is false. E.g.
Expand Down Expand Up @@ -1003,23 +1006,29 @@ const LogSeverity GLOG_0 = GLOG_ERROR;

#else // !DCHECK_IS_ON()

#define DLOG(severity) \
#define DLOG(severity) \
static_cast<void>(0), \
true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)

#define DVLOG(verboselevel) \
(true || !VLOG_IS_ON(verboselevel)) ?\
(void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO)
#define DVLOG(verboselevel) \
static_cast<void>(0), \
(true || !VLOG_IS_ON(verboselevel)) ? \
(void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO)

#define DLOG_IF(severity, condition) \
static_cast<void>(0), \
(true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)

#define DLOG_EVERY_N(severity, n) \
static_cast<void>(0), \
true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)

#define DLOG_IF_EVERY_N(severity, condition, n) \
static_cast<void>(0), \
(true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)

#define DLOG_ASSERT(condition) \
static_cast<void>(0), \
true ? (void) 0 : LOG_ASSERT(condition)

// MSVC warning C4127: conditional expression is constant
Expand Down
15 changes: 12 additions & 3 deletions src/windows/glog/logging.h
Expand Up @@ -572,8 +572,10 @@ class LogSink; // defined below
LOG_TO_STRING_##severity(static_cast<std::vector<std::string>*>(outvec)).stream()

#define LOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : google::LogMessageVoidify() & LOG(severity)
#define SYSLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : google::LogMessageVoidify() & SYSLOG(severity)

#define LOG_ASSERT(condition) \
Expand Down Expand Up @@ -863,6 +865,7 @@ DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
&google::LogMessage::SendToLog)

#define PLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void) 0 : google::LogMessageVoidify() & PLOG(severity)

// A CHECK() macro that postpends errno if the condition is false. E.g.
Expand Down Expand Up @@ -1006,23 +1009,29 @@ const LogSeverity GLOG_0 = GLOG_ERROR;

#else // !DCHECK_IS_ON()

#define DLOG(severity) \
#define DLOG(severity) \
static_cast<void>(0), \
true ? (void) 0 : google::LogMessageVoidify() & LOG(severity)

#define DVLOG(verboselevel) \
(true || !VLOG_IS_ON(verboselevel)) ?\
#define DVLOG(verboselevel) \
static_cast<void>(0), \
(true || !VLOG_IS_ON(verboselevel)) ? \
(void) 0 : google::LogMessageVoidify() & LOG(INFO)

#define DLOG_IF(severity, condition) \
static_cast<void>(0), \
(true || !(condition)) ? (void) 0 : google::LogMessageVoidify() & LOG(severity)

#define DLOG_EVERY_N(severity, n) \
static_cast<void>(0), \
true ? (void) 0 : google::LogMessageVoidify() & LOG(severity)

#define DLOG_IF_EVERY_N(severity, condition, n) \
static_cast<void>(0), \
(true || !(condition))? (void) 0 : google::LogMessageVoidify() & LOG(severity)

#define DLOG_ASSERT(condition) \
static_cast<void>(0), \
true ? (void) 0 : LOG_ASSERT(condition)

// MSVC warning C4127: conditional expression is constant
Expand Down

0 comments on commit 4db0631

Please sign in to comment.