[Spice-devel,vdagent-win,V2,3/6] Fix Visual Studio compiler warning (strncpy)

Submitted by Uri Lublin on March 24, 2014, 5:02 p.m.

Details

Message ID 1395680563-15526-4-git-send-email-uril@redhat.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Uri Lublin March 24, 2014, 5:02 p.m.
Visual Studio complains:
    vdagent\file_xfer.h(28) : warning C4996: 'strncpy': This function or variable may be unsafe.
    Consider using strncpy_s instead.
    To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
    See online help for details.

Replace strncpy with strcpy_s, which is implemented in both VS and mingw.
Also strcpy_s also guarantees an ending '\0'.
---
 vdagent/file_xfer.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index b506f59..5426b8c 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -25,7 +25,7 @@  typedef struct ALIGN_VC FileXferTask {
     FileXferTask(HANDLE _handle, uint64_t _size, char* _name):
     handle(_handle), size(_size), pos(0) {
         // FIXME: should raise an error if name is too long..
-        strncpy(name, _name, sizeof(name) - 1);
+        sprintf_s(name, sizeof(name), _name);
     }
     HANDLE handle;
     uint64_t size;

Comments

On Mon, Mar 24, 2014 at 07:02:40PM +0200, Uri Lublin wrote:
> Visual Studio complains:
>     vdagent\file_xfer.h(28) : warning C4996: 'strncpy': This function or variable may be unsafe.
>     Consider using strncpy_s instead.
>     To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
>     See online help for details.
> 
> Replace strncpy with strcpy_s, which is implemented in both VS and mingw.
> Also strcpy_s also guarantees an ending '\0'.
> ---
>  vdagent/file_xfer.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
> index b506f59..5426b8c 100644
> --- a/vdagent/file_xfer.h
> +++ b/vdagent/file_xfer.h
> @@ -25,7 +25,7 @@ typedef struct ALIGN_VC FileXferTask {
>      FileXferTask(HANDLE _handle, uint64_t _size, char* _name):
>      handle(_handle), size(_size), pos(0) {
>          // FIXME: should raise an error if name is too long..
> -        strncpy(name, _name, sizeof(name) - 1);
> +        sprintf_s(name, sizeof(name), _name);

Actually, this does not match what the commit log says, this uses sprintf_s
while the commit log mentions strcpy_s.

Christophe
On 03/26/2014 02:05 PM, Christophe Fergeau wrote:
> On Mon, Mar 24, 2014 at 07:02:40PM +0200, Uri Lublin wrote:
>> Visual Studio complains:
>>      vdagent\file_xfer.h(28) : warning C4996: 'strncpy': This function or variable may be unsafe.
>>      Consider using strncpy_s instead.
>>      To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
>>      See online help for details.
>>
>> Replace strncpy with strcpy_s, which is implemented in both VS and mingw.
>> Also strcpy_s also guarantees an ending '\0'.
>> ---
>>   vdagent/file_xfer.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
>> index b506f59..5426b8c 100644
>> --- a/vdagent/file_xfer.h
>> +++ b/vdagent/file_xfer.h
>> @@ -25,7 +25,7 @@ typedef struct ALIGN_VC FileXferTask {
>>       FileXferTask(HANDLE _handle, uint64_t _size, char* _name):
>>       handle(_handle), size(_size), pos(0) {
>>           // FIXME: should raise an error if name is too long..
>> -        strncpy(name, _name, sizeof(name) - 1);
>> +        sprintf_s(name, sizeof(name), _name);
> Actually, this does not match what the commit log says, this uses sprintf_s
> while the commit log mentions strcpy_s.

Yes, thanks.
I meant sprintf_s in the commit log.
I'll change the log.

Thanks,
     Uri.