Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Lua wrappers for C++ code no longer compile under Visual Studio 2010 #152

amgaera opened this Issue · 3 comments

3 participants


After updating from SWIG 2.0.12 to 3.0.0 Lua wrappers that are generated for C++ code no longer compile under Visual Studio 2010.

Steps to reproduce:

  • open Visual Studio Command Prompt (2010)
  • navigate to SWIG's Examples\lua\class directory
  • execute the following commands:
swig.exe -c++ -lua example.i
cl.exe /c example_wrap.cxx /I path\to\lua\include\directory

Expected results:

  • compilation succeeds

Actual results:

  • compilation fails with the following error:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

example_wrap.cxx(1722) : error C3861: 'snprintf': identifier not found

The above description assumes that swigwin-3.0.0 (executable and source code) is used.

As far as I can tell, other versions of Visual Studio are affected by this issue as well: the snprintf function is still missing in VS 2013.


The line in question is this one (luarun.swg:1002):

snprintf(output, 255, "<%s userdata: %lX>", className, userData);

className is basicaly user-defined value. There couldn't be any guarantee that it will be less than 255. That's why usage of sprintf is incorrect - buffer overflow + memory corruption and all other well known consequences.
Microsoft has no intention to support C99
But it has _snprintf and _snprintf_s functions. I think code snippet like one below placed somewhere in the beginning of luarun.swg is our best choice:

#ifdef _MSC_VER
#define snprintf _snprintf

Another option is to use dynamically allocated array instead of static one:

output = new char[strlen(className)+2];
snprintf(output, 255, "<%s userdata: %lX>", className, userData);
lua_pushstring(L, (const char*)output);
delete output;

(Of course it should be malloc instead of new)

I prefer first one. Any objections ?


I'd prefer portable code and although the stack is better than the heap, portability is more important, so I'd choose the 2nd option. However, I suggest you use a 3rd option: lua_pushfstring.

@wsfulton wsfulton closed this in #153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.