[Spice-devel] [vdagent-win V2 4/6] Fix Visual Studio compiler warning (sscanf/strcat)

Uri Lublin uril at redhat.com
Mon Oct 13 07:26:17 PDT 2014


On 03/26/2014 04:01 PM, Christophe Fergeau wrote:
> Hey,
>
> Finally received 4/6 ;)
>
> On Mon, Mar 24, 2014 at 07:02:41PM +0200, Uri Lublin wrote:
>> This commit partially reverts 4b9e9b1d28ea7eaec44ff73e2f91c4064986b12a.
>>
>> Visual Studio complains about non-safe sscanf and strcat:
>>      vdagent\file_xfer.cpp(89) : warning C4996: 'strcat': This function
>>      or variable may be unsafe.  Consider using strcat_s instead.
>>      To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
>>      See online help for details.
>>
>> Replace them with sscanf_s and strcat_s.
>> Add #define macros when building with mingw.
>> Note that for strcat_s, size parameter (NumberOfElements) represents the
>> size of destination-buffer, while for strncat, size parameter (n)
>> represents the number of characters to copy.
>> ---
>>   vdagent/file_xfer.cpp | 8 +++++---
>>   1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
>> index 01072ba..3bed1b1 100644
>> --- a/vdagent/file_xfer.cpp
>> +++ b/vdagent/file_xfer.cpp
>> @@ -24,6 +24,8 @@
>>   #define PRIu64 "I64u"
>>   #else // compiling with mingw
>>   #include <inttypes.h>
>> +#define sscanf_s sscanf
>> +#define strcat_s(d, n, s) strncat(d, s, n - strlen(d))
> It turns out the mingw version on my system (fedora 20) has strcat_s, and
> should have sscanf_s, so I suggest to turn this into configure.ac checks:
>
> AC_CHECK_FUNCS([strcat_s strncpy_s sscanf_s])
>
> combined with
>
> diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
> index e402eb2..a71000f 100644
> --- a/vdagent/file_xfer.cpp
> +++ b/vdagent/file_xfer.cpp
> @@ -15,6 +15,9 @@
>      along with this program.  If not, see <http://www.gnu.org/licenses/>.
>   */
>
> +#ifdef HAVE_CONFIG_H
> +#include <config.h>
> +#endif
>   #include <shlobj.h>
>   #define __STDC_FORMAT_MACROS
>   #define __USE_MINGW_ANSI_STDIO 1
> @@ -23,6 +26,14 @@
>   #include "file_xfer.h"
>   #include "as_user.h"
>
> +#ifndef HAVE_SSCANF_S
> +// sscanf_s fallback
> +#endif
> +
> +#ifndef HAVE_STRCAT_S
> +// strcat_s fallback
> +#endif
> +
>
> One bit I did not get right yet is that a standalone test program and the
> AC_CHECK_FUNCS() call can successfully use sscanf_s, but trying to use it
> in file-xfer.c results in a compilation failure, not quite sure about the
> difference :(
>
> Christophe

Hi Christophe,

Finally got to give your suggestion a try.
My system is Fedora 20, with mingw32-headers-3.2.0-1.fc20.noarch installed.

At first I was able to build with strcat_s, but the program failed to run on
Windows XP, claiming it was missing that function.
As you've mention sscanf_s needs to be declared in the src file, but the 
program
failed to run on Windows XP, claiming it was missing that function. 
(This should
probably be added to mingw's sec_api/string.h)
After playing with it, I was able to make it work, by linking with 
-lmsvcr100
and installing "Microsoft Visual C++ 2010 redistributable (x86)" on the 
Windows XP.

It also works for me with -lmsvcr110 and installing 2012 redistributable,
but not with -lmsvcr80 or -lmsvcr90 and installing 2005/2008 
redistributable.

On Windows 7 it works for me without the extra -lmsvcr100, but if I do 
build with
that flag than 2010 redistributable has to be installed too.


test program (a.c), compiled with "i686-w64-mingw32-gcc -o a.exe a.c 
-lmsvcr100":

#include <stdio.h>
#include <string.h>
int sscanf_s (const char *, const char *, ...);
//char* strcat_s (char *, int, const char *);

int main ()
{
     char buff[128] = {0,};
     unsigned long  value=0;
     int l = sscanf_s("55555", "%Iu64", &value);
     strcat_s(buff, sizeof(buff), "hello");
     strcat_s(buff, sizeof(buff), " world");
     printf("l=%d v=%lu s=%s\n", l, value, buff);
     return 0;
}



I prefer to commit the patch as is and not add the extra step of installing
Microsoft Visual C++ redistributable package on the guest.

What do you think ?

Thanks,
     Uri.


More information about the Spice-devel mailing list