[Spice-devel] [vdagent-win PATCH] file-xfer: handle_start: use snprintf instead of sprintf_s

Uri Lublin uril at redhat.com
Thu Feb 28 09:11:04 UTC 2019


On 2/25/19 1:15 PM, Frediano Ziglio wrote:
>>
>> When building with older mingw, sprintf_s does not
>> always work as expected, but snprintf does.
>>
>> Also it's more consistent in the file.
>>
>> Note that when building with VS, snprintf becomes sprintf_s
>>
>> Related: rhbz#1410181
>>
>> Signed-off-by: Uri Lublin <uril at redhat.com>
> 
> Yes, you are right.
> I think the change SPICE_N_ELEMENTS -> sizeof is not needed.
> In this case does not make much difference but if tomorrow
> we move to wide character is better to keep SPICE_N_ELEMENTS
> as we will need to pass the "character" size, not the byte size.

I can change it back, but

Both Linux and Windows documentation mention that snprintf's
second argument is in bytes (although in the Windows one
it's a bit confusing).

If we change the type we better change the snprintf
as well (either the format string or the function itself).


Uri.

> 
> Frediano
> 
>> ---
>>   vdagent/file_xfer.cpp | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
>> index ada3b47..c456bbe 100644
>> --- a/vdagent/file_xfer.cpp
>> +++ b/vdagent/file_xfer.cpp
>> @@ -113,7 +113,7 @@ void FileXfer::handle_start(VDAgentFileXferStartMessage*
>> start,
>>           if (attempt == 0) {
>>               strcpy(dest_filename, file_name);
>>           } else {
>> -            sprintf_s(dest_filename, SPICE_N_ELEMENTS(dest_filename),
>> +            snprintf(dest_filename, sizeof(dest_filename),
>>                         "%.*s (%d)%s", int(extension - file_name), file_name,
>>                         attempt, extension);
>>           }
>>           if ((MultiByteToWideChar(CP_UTF8, 0, dest_filename, -1, file_path +
>>           wlen, MAX_PATH - wlen)) == 0) {



More information about the Spice-devel mailing list