Skip to content

Commit 323d0eb

Browse files
authored
Fix clang on windows warnings (#180)
* Fix warning suppression on Windows clang When building loguru under Windows clang, _MSC_VER is defined, but it uses clang-style warning suppression flags. Fix that by checking for __GNUC__ and __clang__ first, then checking for _MSC_VER. Also fix the lack of a `#pragma GCC diagnostic pop` for people who include loguru.cpp. * Fix unsigned/signed comparison `snprintf` can return a negative value to signify that the buffer is not large enough to contain the bytes you attempted to write, so check for a positive value before advancing `pos` -- otherwise a later `snprintf` might overwrite bytes we've already written to the buffer.
1 parent 048711b commit 323d0eb

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

loguru.cpp

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef _WIN32
1+
#if defined(__GNUC__) || defined(__clang__)
22
// Disable all warnings from gcc/clang:
33
#pragma GCC diagnostic push
44
#pragma GCC diagnostic ignored "-Wpragmas"
@@ -11,16 +11,13 @@
1111
#pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
1212
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
1313
#pragma GCC diagnostic ignored "-Wpadded"
14-
#pragma GCC diagnostic ignored "-Wsign-compare"
1514
#pragma GCC diagnostic ignored "-Wsign-conversion"
1615
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
1716
#pragma GCC diagnostic ignored "-Wunused-macros"
1817
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
19-
#else
20-
#ifdef _MSC_VER
18+
#elif defined(_MSC_VER)
2119
#pragma warning(push)
22-
#pragma warning(disable:4018)
23-
#endif // _MSC_VER
20+
#pragma warning(disable:4365) // conversion from 'X' to 'Y', signed/unsigned mismatch
2421
#endif
2522

2623
#include "loguru.hpp"
@@ -1244,27 +1241,48 @@ namespace loguru
12441241
{
12451242
if (out_buff_size == 0) { return; }
12461243
out_buff[0] = '\0';
1247-
long pos = 0;
1244+
size_t pos = 0;
12481245
if (g_preamble_date && pos < out_buff_size) {
1249-
pos += snprintf(out_buff + pos, out_buff_size - pos, "date ");
1246+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date ");
1247+
if (bytes > 0) {
1248+
pos += bytes;
1249+
}
12501250
}
12511251
if (g_preamble_time && pos < out_buff_size) {
1252-
pos += snprintf(out_buff + pos, out_buff_size - pos, "time ");
1252+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time ");
1253+
if (bytes > 0) {
1254+
pos += bytes;
1255+
}
12531256
}
12541257
if (g_preamble_uptime && pos < out_buff_size) {
1255-
pos += snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1258+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1259+
if (bytes > 0) {
1260+
pos += bytes;
1261+
}
12561262
}
12571263
if (g_preamble_thread && pos < out_buff_size) {
1258-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1264+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1265+
if (bytes > 0) {
1266+
pos += bytes;
1267+
}
12591268
}
12601269
if (g_preamble_file && pos < out_buff_size) {
1261-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1270+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1271+
if (bytes > 0) {
1272+
pos += bytes;
1273+
}
12621274
}
12631275
if (g_preamble_verbose && pos < out_buff_size) {
1264-
pos += snprintf(out_buff + pos, out_buff_size - pos, " v");
1276+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v");
1277+
if (bytes > 0) {
1278+
pos += bytes;
1279+
}
12651280
}
12661281
if (g_preamble_pipe && pos < out_buff_size) {
1267-
pos += snprintf(out_buff + pos, out_buff_size - pos, "| ");
1282+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "| ");
1283+
if (bytes > 0) {
1284+
pos += bytes;
1285+
}
12681286
}
12691287
}
12701288

@@ -1296,36 +1314,57 @@ namespace loguru
12961314
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
12971315
}
12981316

1299-
long pos = 0;
1317+
size_t pos = 0;
13001318

13011319
if (g_preamble_date && pos < out_buff_size) {
1302-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1303-
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1320+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1321+
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1322+
if (bytes > 0) {
1323+
pos += bytes;
1324+
}
13041325
}
13051326
if (g_preamble_time && pos < out_buff_size) {
1306-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1307-
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1327+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1328+
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1329+
if (bytes > 0) {
1330+
pos += bytes;
1331+
}
13081332
}
13091333
if (g_preamble_uptime && pos < out_buff_size) {
1310-
pos += snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1311-
uptime_sec);
1334+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1335+
uptime_sec);
1336+
if (bytes > 0) {
1337+
pos += bytes;
1338+
}
13121339
}
13131340
if (g_preamble_thread && pos < out_buff_size) {
1314-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1315-
LOGURU_THREADNAME_WIDTH, thread_name);
1341+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1342+
LOGURU_THREADNAME_WIDTH, thread_name);
1343+
if (bytes > 0) {
1344+
pos += bytes;
1345+
}
13161346
}
13171347
if (g_preamble_file && pos < out_buff_size) {
13181348
char shortened_filename[LOGURU_FILENAME_WIDTH + 1];
13191349
snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file);
1320-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1321-
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1350+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1351+
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1352+
if (bytes > 0) {
1353+
pos += bytes;
1354+
}
13221355
}
13231356
if (g_preamble_verbose && pos < out_buff_size) {
1324-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1325-
level_buff);
1357+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1358+
level_buff);
1359+
if (bytes > 0) {
1360+
pos += bytes;
1361+
}
13261362
}
13271363
if (g_preamble_pipe && pos < out_buff_size) {
1328-
pos += snprintf(out_buff + pos, out_buff_size - pos, "| ");
1364+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "| ");
1365+
if (bytes > 0) {
1366+
pos += bytes;
1367+
}
13291368
}
13301369
}
13311370

@@ -1956,10 +1995,11 @@ namespace loguru
19561995

19571996
#endif // _WIN32
19581997

1959-
#ifdef _WIN32
1960-
#ifdef _MSC_VER
1998+
1999+
#if defined(__GNUC__) || defined(__clang__)
2000+
#pragma GCC diagnostic pop
2001+
#elif defined(_MSC_VER)
19612002
#pragma warning(pop)
1962-
#endif // _MSC_VER
1963-
#endif // _WIN32
2003+
#endif
19642004

19652005
#endif // LOGURU_IMPLEMENTATION

0 commit comments

Comments
 (0)