diff --git a/apps/opencs/model/world/refcollection.cpp b/apps/opencs/model/world/refcollection.cpp index 25c934c7f0..5d128a3576 100644 --- a/apps/opencs/model/world/refcollection.cpp +++ b/apps/opencs/model/world/refcollection.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include "ref.hpp" #include "cell.hpp" #include "universalid.hpp" @@ -60,7 +62,12 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool // ignoring moved references sub-record; instead calculate cell from coordinates std::pair index = ref.getCellIndex(); - ref.mCell = "#" + std::to_string(index.first) + " " + std::to_string(index.second); + char buf[100]; + int res = snprintf(buf, 100, "#%d %d", index.first, index.second); + if (res > 0 && res < 100) + ref.mCell = std::string(buf); + else + throw std::runtime_error("getNewId possible buffer overflow"); if (!base && // don't try to update base records mref.mRefNum.mIndex != 0) // MVRF tag found @@ -181,7 +188,12 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool std::string CSMWorld::RefCollection::getNewId() { - return "ref#" + std::to_string(mNextId++); + char buf[100]; + int res = snprintf(buf, 100, "ref#%d", mNextId++); + if (res > 0 && res < 100) + return std::string(buf); + else + throw std::runtime_error("getNewId possible buffer overflow"); } unsigned int CSMWorld::RefCollection::extractIdNum (const std::string& id) const diff --git a/libs/platform/strings.h b/libs/platform/strings.h index 305705044e..027f903330 100644 --- a/libs/platform/strings.h +++ b/libs/platform/strings.h @@ -10,11 +10,38 @@ # pragma warning(disable: 4996) # define strcasecmp stricmp # if (_MSC_VER < 1900) -# define snprintf _snprintf +# include +# include +# define snprintf c99_snprintf +# define vsnprintf c99_vsnprintf +/* see http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 */ +inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) +{ + int count = -1; + + if (size != 0) + count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap); + if (count == -1) + count = _vscprintf(format, ap); + + return count; +} + +inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...) +{ + int count; + va_list ap; + + va_start(ap, format); + count = c99_vsnprintf(outBuf, size, format, ap); + va_end(ap); + + return count; +} # endif #else # warning "Unable to determine your compiler, you should probably take a look here." # include // Just take a guess -#endif +#endif #endif /* _STRINGS_WRAPPER_H */