Skip to content

Commit 86bbfd9

Browse files
jorgenptkcgen
authored andcommitted
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 aca302d commit 86bbfd9

File tree

1 file changed

+68
-34
lines changed

1 file changed

+68
-34
lines changed

loguru.cpp

Lines changed: 68 additions & 34 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"
@@ -1249,27 +1246,45 @@ namespace loguru
12491246
{
12501247
if (out_buff_size == 0) { return; }
12511248
out_buff[0] = '\0';
1252-
long pos = 0;
1253-
if (g_preamble_date) {
1254-
pos += snprintf(out_buff + pos, out_buff_size - pos, "date ");
1249+
size_t pos = 0;
1250+
if (g_preamble_date && pos < out_buff_size) {
1251+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date ");
1252+
if (bytes > 0) {
1253+
pos += bytes;
1254+
}
12551255
}
12561256
if (g_preamble_time && pos < out_buff_size) {
1257-
pos += snprintf(out_buff + pos, out_buff_size - pos, "time ");
1257+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time ");
1258+
if (bytes > 0) {
1259+
pos += bytes;
1260+
}
12581261
}
12591262
if (g_preamble_uptime && pos < out_buff_size) {
1260-
pos += snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1263+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1264+
if (bytes > 0) {
1265+
pos += bytes;
1266+
}
12611267
}
12621268
if (g_preamble_thread && pos < out_buff_size) {
1263-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1269+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1270+
if (bytes > 0) {
1271+
pos += bytes;
1272+
}
12641273
}
12651274
if (g_preamble_file && pos < out_buff_size) {
1266-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1275+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1276+
if (bytes > 0) {
1277+
pos += bytes;
1278+
}
12671279
}
12681280
if (g_preamble_verbose && pos < out_buff_size) {
1269-
pos += snprintf(out_buff + pos, out_buff_size - pos, " v");
1281+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v");
1282+
if (bytes > 0) {
1283+
pos += bytes;
1284+
}
12701285
}
12711286
if (g_preamble_pipe && pos < out_buff_size) {
1272-
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
1287+
/* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
12731288
}
12741289
}
12751290

@@ -1301,36 +1316,54 @@ namespace loguru
13011316
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
13021317
}
13031318

1304-
long pos = 0;
1319+
size_t pos = 0;
13051320

1306-
if (g_preamble_date) {
1307-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1308-
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1321+
if (g_preamble_date && pos < out_buff_size) {
1322+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1323+
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1324+
if (bytes > 0) {
1325+
pos += bytes;
1326+
}
13091327
}
13101328
if (g_preamble_time && pos < out_buff_size) {
1311-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1312-
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1329+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1330+
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1331+
if (bytes > 0) {
1332+
pos += bytes;
1333+
}
13131334
}
13141335
if (g_preamble_uptime && pos < out_buff_size) {
1315-
pos += snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1316-
uptime_sec);
1336+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1337+
uptime_sec);
1338+
if (bytes > 0) {
1339+
pos += bytes;
1340+
}
13171341
}
13181342
if (g_preamble_thread && pos < out_buff_size) {
1319-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1320-
LOGURU_THREADNAME_WIDTH, thread_name);
1343+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1344+
LOGURU_THREADNAME_WIDTH, thread_name);
1345+
if (bytes > 0) {
1346+
pos += bytes;
1347+
}
13211348
}
13221349
if (g_preamble_file && pos < out_buff_size) {
13231350
char shortened_filename[LOGURU_FILENAME_WIDTH + 1];
13241351
snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file);
1325-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1326-
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1352+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1353+
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1354+
if (bytes > 0) {
1355+
pos += bytes;
1356+
}
13271357
}
13281358
if (g_preamble_verbose && pos < out_buff_size) {
1329-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1330-
level_buff);
1359+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1360+
level_buff);
1361+
if (bytes > 0) {
1362+
pos += bytes;
1363+
}
13311364
}
13321365
if (g_preamble_pipe && pos < out_buff_size) {
1333-
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
1366+
/* pos += */ (void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
13341367
}
13351368
}
13361369

@@ -1969,10 +2002,11 @@ namespace loguru
19692002

19702003
#endif // _WIN32
19712004

1972-
#ifdef _WIN32
1973-
#ifdef _MSC_VER
2005+
2006+
#if defined(__GNUC__) || defined(__clang__)
2007+
#pragma GCC diagnostic pop
2008+
#elif defined(_MSC_VER)
19742009
#pragma warning(pop)
1975-
#endif // _MSC_VER
1976-
#endif // _WIN32
2010+
#endif
19772011

19782012
#endif // LOGURU_IMPLEMENTATION

0 commit comments

Comments
 (0)