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

Christophe Fergeau cfergeau at redhat.com
Wed Mar 26 07:01:25 PDT 2014


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20140326/adcd3151/attachment.sig>


More information about the Spice-devel mailing list