Merge pull request #3905 from acemod/fix-c4996

Fix c4996
This commit is contained in:
Glowbal 2016-06-12 17:42:38 +02:00 committed by GitHub
commit b16f444795
7 changed files with 63 additions and 87 deletions

View File

@ -88,6 +88,7 @@ Kllrt <kllrtik@gmail.com>
legman <juicemelon@msn.com> legman <juicemelon@msn.com>
Legolasindar "Viper" <legolasindar@gmail.com> Legolasindar "Viper" <legolasindar@gmail.com>
licht-im-Norden87 <lichtimnorden87@gmail.com> licht-im-Norden87 <lichtimnorden87@gmail.com>
looter <looter222@gmail.com>
Macusercom <macusercom@gmail.com> Macusercom <macusercom@gmail.com>
MarcBook MarcBook
meat <p.humberdroz@gmail.com> meat <p.humberdroz@gmail.com>

View File

@ -1,5 +1,6 @@
#include "shared.hpp" #include "shared.hpp"
#include <stdlib.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
@ -238,7 +239,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
ZERO_OUTPUT(); ZERO_OUTPUT();
std::stringstream outputStr; std::stringstream outputStr;
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
@ -261,7 +262,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
// int n = sprintf(output, "%f", retard); // int n = sprintf(output, "%f", retard);
outputStr << retard; outputStr << retard;
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} else if (!strcmp(mode, "atmosphericCorrection")) { } else if (!strcmp(mode, "atmosphericCorrection")) {
@ -280,7 +281,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
ballisticCoefficient = calculateAtmosphericCorrection(ballisticCoefficient, temperature, pressure, humidity, atmosphereModel); ballisticCoefficient = calculateAtmosphericCorrection(ballisticCoefficient, temperature, pressure, humidity, atmosphereModel);
//int n = sprintf(output, "%f", ballisticCoefficient); //int n = sprintf(output, "%f", ballisticCoefficient);
outputStr << ballisticCoefficient; outputStr << ballisticCoefficient;
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} else if (!strcmp(mode, "new")) { } else if (!strcmp(mode, "new")) {
unsigned int index = 0; unsigned int index = 0;
@ -375,7 +376,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].frames = 0.0; bulletDatabase[index].frames = 0.0;
bulletDatabase[index].randSeed = 0; bulletDatabase[index].randSeed = 0;
strncpy(output, "", outputSize); strncpy_s(output, outputSize, "", _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} else if (!strcmp(mode, "simulate")) { } else if (!strcmp(mode, "simulate")) {
// simulate:0:[-0.109985,542.529,-3.98301]:[3751.57,5332.23,214.252]:[0.598153,2.38829,0]:28.6:0:0.481542:0:215.16 // simulate:0:[-0.109985,542.529,-3.98301]:[3751.57,5332.23,214.252]:[0.598153,2.38829,0]:28.6:0:0.481542:0:215.16
@ -594,7 +595,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
}; };
outputStr << "_bullet setVelocity (_bulletVelocity vectorAdd [" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]); _bullet setPosASL (_bulletPosition vectorAdd [" << positionOffset[0] << "," << positionOffset[1] << "," << positionOffset[2] << "]);"; outputStr << "_bullet setVelocity (_bulletVelocity vectorAdd [" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]); _bullet setPosASL (_bulletPosition vectorAdd [" << positionOffset[0] << "," << positionOffset[1] << "," << positionOffset[2] << "]);";
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} else if (!strcmp(mode, "set")) { } else if (!strcmp(mode, "set")) {
int height = 0; int height = 0;
@ -609,7 +610,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map->gridBuildingNums.push_back(numObjects); map->gridBuildingNums.push_back(numObjects);
map->gridSurfaceIsWater.push_back(surfaceIsWater); map->gridSurfaceIsWater.push_back(surfaceIsWater);
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} else if (!strcmp(mode, "init")) { } else if (!strcmp(mode, "init")) {
int mapSize = 0; int mapSize = 0;
@ -625,7 +626,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map = &mapDatabase[worldName]; map = &mapDatabase[worldName];
if (map->gridHeights.size() == gridCells) { if (map->gridHeights.size() == gridCells) {
outputStr << "Terrain already initialized"; outputStr << "Terrain already initialized";
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
@ -638,9 +639,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map->gridBuildingNums.reserve(gridCells); map->gridBuildingNums.reserve(gridCells);
map->gridSurfaceIsWater.reserve(gridCells); map->gridSurfaceIsWater.reserve(gridCells);
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
strncpy(output, outputStr.str().c_str(), outputSize); strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} }

View File

@ -13,6 +13,7 @@
#include "shared.hpp" #include "shared.hpp"
#include <stdlib.h>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <string> #include <string>
@ -60,19 +61,12 @@ std::string addLineBreaks(const std::vector<std::string> &words) {
return sstream.str(); return sstream.str();
} }
// i like to live dangerously. jk, fix strncpy sometime pls.
#pragma warning( push )
#pragma warning( disable : 4996 )
void __stdcall RVExtension(char *output, int outputSize, const char *function) { void __stdcall RVExtension(char *output, int outputSize, const char *function) {
ZERO_OUTPUT(); ZERO_OUTPUT();
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else { } else {
strncpy(output, addLineBreaks(splitString(function)).c_str(), outputSize); strncpy_s(output, outputSize, addLineBreaks(splitString(function)).c_str(), _TRUNCATE);
output[outputSize - 1] = '\0';
} }
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
#pragma warning( pop )

View File

@ -1,7 +1,7 @@
/* /*
* ace_clipboard.cpp * ace_clipboard.cpp
* *
* Takes a string and copies it to the clipboard; bypasses arma 8k clippy limit. * Takes a string and copies it to the clipboard; bypasses arma 8k clippy limit. Windows only.
* *
* Takes: * Takes:
* Localized string as string * Localized string as string
@ -9,7 +9,10 @@
* Returns: * Returns:
* None * None
*/ */
#include "shared.hpp" #include "shared.hpp"
#include <stdlib.h>
#include <vector> #include <vector>
#include <string> #include <string>
@ -30,61 +33,49 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
} }
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
#ifdef _WIN32 #ifdef _WIN32
if (!strcmp(function, "--COMPLETE--")) { if (!strcmp(function, "--COMPLETE--")) {
HGLOBAL hClipboardData = GlobalAlloc(GMEM_FIXED, gClipboardData.length() + 1); if (OpenClipboard(NULL) == 0) {
if (!hClipboardData) {
result = "GlobalAlloc() failed, GetLastError=" + GetLastError();
gClipboardData = "";
EXTENSION_RETURN();
}
char *pClipboardData = (char *)GlobalLock(hClipboardData);
if (!pClipboardData) {
result = "GlobalLock() failed, GetLastError=" + GetLastError();
gClipboardData = "";
EXTENSION_RETURN();
}
memcpy(pClipboardData, gClipboardData.c_str(), gClipboardData.length());
pClipboardData[gClipboardData.length() + 1] = 0x00;
GlobalUnlock(hClipboardData);
if (!OpenClipboard(NULL)) {
result = "OpenClipboard() failed, GetLastError=" + GetLastError(); result = "OpenClipboard() failed, GetLastError=" + GetLastError();
} } else {
else { if (EmptyClipboard() == 0) {
if (!EmptyClipboard()) { result = "EmptyClipboard() failed, GetLastError=" + GetLastError();
result = "OpenClipboard() failed, GetLastError=" + GetLastError(); } else {
} // GPTR = GMEM_FIXED + GMEM_ZEROINIT, returns a ptr, no need for GlobalLock/GlobalUnlock
else { char *pClipboardData = (char *)GlobalAlloc(GPTR, gClipboardData.length());
if (!SetClipboardData(CF_TEXT, hClipboardData)) {
result = "SetClipboardData() failed, GetLastError=" + GetLastError(); if (pClipboardData == NULL) {
result = "GlobalAlloc() failed, GetLastError=" + GetLastError();
EXTENSION_RETURN();
} }
else { strncpy_s(pClipboardData, gClipboardData.length(), gClipboardData.c_str(), _TRUNCATE);
if (!CloseClipboard()) {
// if success, system owns the memory, if fail, free it from the heap
if (SetClipboardData(CF_TEXT, pClipboardData) == NULL) {
result = "SetClipboardData() failed, GetLastError=" + GetLastError();
GlobalFree(pClipboardData);
} else {
if (CloseClipboard() == 0) {
result = "CloseClipboard() failed, GetLastError=" + GetLastError(); result = "CloseClipboard() failed, GetLastError=" + GetLastError();
} }
} }
} }
} }
gClipboardData = ""; gClipboardData = "";
} else { } else {
gClipboardData = gClipboardData + cur_input; gClipboardData = gClipboardData + cur_input;
} }
end: if (result.length() > 1) {
if(result.length() > 1) strncpy_s(output, outputSize, result.c_str(), _TRUNCATE);
memcpy(output, result.c_str(), result.length()+1); }
#endif #endif
EXTENSION_RETURN(); EXTENSION_RETURN();
} }

View File

@ -15,6 +15,7 @@
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <stdlib.h>
#include <math.h> #include <math.h>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -96,15 +97,10 @@ double getSolution(double initSpeed, double airFriction, double angleTarget, dou
return a2 - angleTarget; return a2 - angleTarget;
} }
// i like to live dangerously. jk, fix strncpy sometime pls.
#pragma warning( push )
#pragma warning( disable : 4996 )
void __stdcall RVExtension(char *output, int outputSize, const char *function) { void __stdcall RVExtension(char *output, int outputSize, const char *function) {
ZERO_OUTPUT(); ZERO_OUTPUT();
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else { } else {
std::vector<std::string> argStrings = splitString(function); std::vector<std::string> argStrings = splitString(function);
double initSpeed = std::stod(argStrings[0]); double initSpeed = std::stod(argStrings[0]);
@ -117,10 +113,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
std::stringstream sstream; std::stringstream sstream;
sstream << result; sstream << result;
strcpy(output, sstream.str().c_str()); strncpy_s(output, outputSize, sstream.str().c_str(), _TRUNCATE);
output[outputSize - 1] = '\0';
} }
EXTENSION_RETURN(); EXTENSION_RETURN();
} }
#pragma warning( pop )

View File

@ -6,6 +6,8 @@
*/ */
#include "shared.hpp" #include "shared.hpp"
#include <stdlib.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -31,7 +33,7 @@ std::vector<std::string> parseExtensionInput(const std::string& input)
void __stdcall RVExtension(char *output, int outputSize, const char *function) { void __stdcall RVExtension(char *output, int outputSize, const char *function) {
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} }
else else
{ {
@ -82,8 +84,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
returnValue = debugreturn.str(); returnValue = debugreturn.str();
} }
} }
strncpy(output, returnValue.c_str(), outputSize);
output[outputSize - 1] = '\0'; strncpy_s(output, outputSize, returnValue.c_str(), _TRUNCATE);
} }
} }

View File

@ -13,6 +13,7 @@
#include "shared.hpp" #include "shared.hpp"
#include <stdlib.h>
#include <sstream> #include <sstream>
#include <string> #include <string>
@ -36,18 +37,12 @@ std::string getImagePathFromStructuredText(const std::string & input) {
return returnValue; return returnValue;
} }
#pragma warning( push )
#pragma warning( disable : 4996 )
void __stdcall RVExtension(char *output, int outputSize, const char *function) { void __stdcall RVExtension(char *output, int outputSize, const char *function) {
ZERO_OUTPUT(); ZERO_OUTPUT();
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE); strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else { } else {
strncpy_s(output, outputSize, getImagePathFromStructuredText(function).c_str(), _TRUNCATE); strncpy_s(output, outputSize, getImagePathFromStructuredText(function).c_str(), _TRUNCATE);
output[outputSize - 1] = '\0'; }
} EXTENSION_RETURN();
EXTENSION_RETURN();
} }
#pragma warning( pop )