MengRao / fmtlog

fmtlog is a performant fmtlib-style logging library with latency in nanoseconds.
MIT License
807 stars 124 forks source link

Implicit long/int truncation in fmtlog.h #30

Closed snej closed 2 years ago

snej commented 2 years ago

Compiling on macOS or iOS with Clang produces this warning, due to these 64-bit platforms using 32-bit ints:

vendor/fmtlog/fmtlog.h:546:21: error: implicit conversion loses integer precision: 'std::vector<fmt::basic_format_arg<fmt::basic_format_context<fmt::appender, char>>>::size_type' (aka 'unsigned long') to 'int' [-Werror,-Wshorten-64-to-32]
      argIdx = args.size();
             ~ ~~~~~^~~~~~

Fix is simply to manually coerce the size_t to an int:

diff --git a/fmtlog.h b/fmtlog.h
index 9bf6501..77ebf2b 100644
--- a/fmtlog.h
+++ b/fmtlog.h
@@ -543,7 +543,7 @@ public:
     const char* dtor_args[std::max(num_dtors, (size_t)1)];
     const char* ret;
     if (argIdx < 0) {
-      argIdx = args.size();
+      argIdx = int(args.size());
       args.resize(argIdx + num_args);
       ret = decodeArgs<false, 0, 0, Args...>(data, args.data() + argIdx, dtor_args);
     }
@@ -630,13 +630,13 @@ public:
     size_t alloc_size = 8 + getArgSizes<0>(cstringSizes, args...);
     if (threadBuffer == nullptr) preallocate();
     do {
-      if (auto header = threadBuffer->varq.allocMsg(alloc_size)) {
+      if (auto header = threadBuffer->varq.allocMsg(uint32_t(alloc_size))) {
         header->logId = logId;
         char* out = (char*)(header + 1);
         *(int64_t*)out = tsc;
         out += 8;
         encodeArgs<0>(cstringSizes, out, std::forward<Args>(args)...);
-        header->push(alloc_size);
+        header->push(uint32_t(alloc_size));
         break;
       }
     } while (FMTLOG_BLOCK);

Edit: I found two more of these conversions as I started using the API, and added them to the patch.

Apologies for lack of a PR, but I didn't feel it was worth forking the repo for such a tiny fix.

MengRao commented 2 years ago

Fixed