Open chfast opened 5 years ago
That format ending with %03dZ
is trying to format a 3-digit integer, followed by a literal Z
. I don't know why it would be interpreted as a Z format string - and if you run the actual code it produces a Z literal as expected. So possibly the warning is just wrong?
Maybe it's a false positive. Let me check the compiler version and what flag Go is using and I will try to reproduce this with a smaller example.
The warning is a bit misleading, it's not the Z
that it has a problem with, rather the entire formatting string. The formatting string specifies the minimum sizes of the fields (e.g. %04d
for year), but the data type the method puts into that slot might marshal into more than 4 characters (same for the other fields).
The values are range checked before the call, but the compiler probably doesn't carry that range information far enough. It would need to carry that information through the public API, as ECMAScript datetime components have known ranges.
I don't see how the compiler could be (reliably) made aware of the actual ranges of the values, so the solution would be either to suppress the warning (the buffer is large enough so it is bogus) or to switch to snprintf().
The annoying thing about snprintf()
is that it's not portable below C99 (which Duktape also targets) so it's going to need an ifdef at least for older MSVC (as in https://github.com/svaarala/duktape/blob/master/extras/module-duktape/duk_module_duktape.c#L8-L14). Inside Duktape itself there are platform/compiler workarounds for this already, but the logging module is external so it doesn't have access to these. Another non-MSVC-specific alternative is to check for C99 and use snprintf() if C99 is available, and fall back to sprintf() otherwise (and allow the warning).
Hi there,
I get the following compiler warning:
Obviously you can fix it by increasing the
date_buf
size to 85 in https://github.com/svaarala/duktape/blob/master/extras/logging/duk_logging.c#L138.I wander if you have any opinions on that.