On 22/04/2025 12:51, Alex Peshkoff wrote:
> On 4/22/25 10:56, 'Mark Rotteveel' via firebird-devel wrote:
>> On 19/04/2025 08:54, 'Mark Rotteveel' via firebird-devel wrote:
>>> In the macOS build, I noticed warnings about sprintf:
>>>
>>> ```
>>> src/cloop/Lexer.cpp:157:5: warning: 'sprintf' is deprecated: This
>>> function is provided for compatibility reasons only. Due to security
>>> concerns inherent in the design of sprintf(3), it is highly
>>> recommended that you use snprintf(3) instead. [-Wdeprecated-
>>> declarations]
>>> sprintf(buffer, "%s:%i:%i: error:
>>> Invalid hexadecimal prefix.",
>>> ^
>>> ```
>>>
>>> Any objections if I try and replace those with snprintf?
>>
>> I see a macro SNPRINTF defined as a compatibility shim, while - as far
>> as I know - all compilers we use support it.
>
> Long ago snprintf behaved in different manner on different platforms -
> it was return code related (do not remember details - something like -1
> or -required_size on overflow). SNPRINTF was targeted to address this.
> May be it was solved since that days - did not pay much attention.
>
>> Additionally, there is fb_utils::snprintf to assert the returned length.
>>
>> Should I call fb_utils::snprintf instead of bare snprintf, or should I
>> use SNPRINTF?
>>
>
> Do not know. BTW, SNPRINTF & fb_utils::snprintf are not accessible in
> all places, for example in cloop one can't use them.
OK, but should I use fb_utils::snprintf if it is available, and
otherwise bare snprintf? Or should I use, in order of availability,
fb_utils::snprintf, otherwise SNPRINTF, or otherwise bare snprintf?
>> I also notice that this isn't done consistently right now (sometimes
>> snprintf is called directly, instead of using fb_utils::snprintf or
>> SNPRINTF). Should that be addressed as well?
>
> I'm not sure we have single way to solve that everywhere. For example I
> remember that in fb_string I've added a code that takes care about
> different (per platform) return values itself. Doubt that replacing it
> with some generic macro/function will make it work better.
The return value should be the same on each platform, assuming C99 or
newer compliance, though Microsoft's _snprintf (not to be confused with
snprintf) and *old* versions of snprintf (MSVC/MSVC++ before 14.0
(2015)) do not correctly account for null-termination:
"""
Beginning with the UCRT in Visual Studio 2015 and Windows 10, snprintf
is no longer identical to _snprintf. The snprintf behavior is now C99
standard conformant. The difference is that if you run out of buffer,
snprintf null-terminates the end of the buffer and returns the number of
characters that would have been required whereas _snprintf doesn't
null-terminate the buffer and returns -1.
"""
(from
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170)
Mark
--
Mark Rotteveel