Skip to content

Commit

Permalink
Merge pull request #3905 from acemod/fix-c4996
Browse files Browse the repository at this point in the history
Fix c4996
  • Loading branch information
thojkooi authored Jun 12, 2016
2 parents 1136a57 + b52c1c8 commit b16f444
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 87 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Kllrt <kllrtik@gmail.com>
legman <juicemelon@msn.com>
Legolasindar "Viper" <legolasindar@gmail.com>
licht-im-Norden87 <lichtimnorden87@gmail.com>
looter <looter222@gmail.com>
Macusercom <macusercom@gmail.com>
MarcBook
meat <p.humberdroz@gmail.com>
Expand Down
23 changes: 12 additions & 11 deletions extensions/advanced_ballistics/AdvancedBallistics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "shared.hpp"

#include <stdlib.h>
#include <string>
#include <vector>
#include <unordered_map>
Expand Down Expand Up @@ -238,7 +239,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
ZERO_OUTPUT();
std::stringstream outputStr;
if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
EXTENSION_RETURN();
}

Expand All @@ -261,8 +262,8 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
// int n = sprintf(output, "%f", retard);

outputStr << retard;
strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);

EXTENSION_RETURN();
} else if (!strcmp(mode, "atmosphericCorrection")) {
double ballisticCoefficient = 1.0;
Expand All @@ -280,7 +281,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
ballisticCoefficient = calculateAtmosphericCorrection(ballisticCoefficient, temperature, pressure, humidity, atmosphereModel);
//int n = sprintf(output, "%f", ballisticCoefficient);
outputStr << ballisticCoefficient;
strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
} else if (!strcmp(mode, "new")) {
unsigned int index = 0;
Expand Down Expand Up @@ -375,7 +376,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].frames = 0.0;
bulletDatabase[index].randSeed = 0;

strncpy(output, "", outputSize);
strncpy_s(output, outputSize, "", _TRUNCATE);
EXTENSION_RETURN();
} 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
Expand Down Expand Up @@ -592,9 +593,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
velocityOffset[1] += (distribution(bulletDatabase[index].randGenerator) * 0.8 - 0.4) * coef;
velocityOffset[2] += (distribution(bulletDatabase[index].randGenerator) * 0.8 - 0.4) * coef;
};

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();
} else if (!strcmp(mode, "set")) {
int height = 0;
Expand All @@ -609,7 +610,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map->gridBuildingNums.push_back(numObjects);
map->gridSurfaceIsWater.push_back(surfaceIsWater);

strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
} else if (!strcmp(mode, "init")) {
int mapSize = 0;
Expand All @@ -625,7 +626,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map = &mapDatabase[worldName];
if (map->gridHeights.size() == gridCells) {
outputStr << "Terrain already initialized";
strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
}

Expand All @@ -638,9 +639,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map->gridBuildingNums.reserve(gridCells);
map->gridSurfaceIsWater.reserve(gridCells);

strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
}
strncpy(output, outputStr.str().c_str(), outputSize);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
}
14 changes: 4 additions & 10 deletions extensions/break_line/ace_break_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "shared.hpp"

#include <stdlib.h>
#include <sstream>
#include <vector>
#include <string>
Expand Down Expand Up @@ -56,23 +57,16 @@ std::string addLineBreaks(const std::vector<std::string> &words) {
}
}
}

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) {
ZERO_OUTPUT();
if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else {
strncpy(output, addLineBreaks(splitString(function)).c_str(), outputSize);
output[outputSize - 1] = '\0';
strncpy_s(output, outputSize, addLineBreaks(splitString(function)).c_str(), _TRUNCATE);
}
EXTENSION_RETURN();
}

#pragma warning( pop )
65 changes: 28 additions & 37 deletions extensions/clipboard/ace_clipboard.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/*
* 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:
* Localized string as string
*
* Returns:
* None
*/

#include "shared.hpp"

#include <stdlib.h>
#include <vector>
#include <string>

Expand All @@ -30,61 +33,49 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
}

if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
EXTENSION_RETURN();
}

#ifdef _WIN32
#ifdef _WIN32

if (!strcmp(function, "--COMPLETE--")) {
HGLOBAL hClipboardData = GlobalAlloc(GMEM_FIXED, gClipboardData.length() + 1);
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)) {
if (OpenClipboard(NULL) == 0) {
result = "OpenClipboard() failed, GetLastError=" + GetLastError();
}
else {
if (!EmptyClipboard()) {
result = "OpenClipboard() failed, GetLastError=" + GetLastError();
}
else {
if (!SetClipboardData(CF_TEXT, hClipboardData)) {
result = "SetClipboardData() failed, GetLastError=" + GetLastError();
} else {
if (EmptyClipboard() == 0) {
result = "EmptyClipboard() failed, GetLastError=" + GetLastError();
} else {
// GPTR = GMEM_FIXED + GMEM_ZEROINIT, returns a ptr, no need for GlobalLock/GlobalUnlock
char *pClipboardData = (char *)GlobalAlloc(GPTR, gClipboardData.length());

if (pClipboardData == NULL) {
result = "GlobalAlloc() failed, GetLastError=" + GetLastError();
EXTENSION_RETURN();
}
else {
if (!CloseClipboard()) {
strncpy_s(pClipboardData, gClipboardData.length(), gClipboardData.c_str(), _TRUNCATE);

// 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();
}
}
}
}

gClipboardData = "";
} else {
gClipboardData = gClipboardData + cur_input;
}

end:
if(result.length() > 1)
memcpy(output, result.c_str(), result.length()+1);
if (result.length() > 1) {
strncpy_s(output, outputSize, result.c_str(), _TRUNCATE);
}

#endif

EXTENSION_RETURN();
}

13 changes: 3 additions & 10 deletions extensions/fcs/ace_fcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#define _USE_MATH_DEFINES

#include <stdlib.h>
#include <math.h>
#include <sstream>
#include <vector>
Expand Down Expand Up @@ -96,15 +97,10 @@ double getSolution(double initSpeed, double airFriction, double angleTarget, dou
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) {
ZERO_OUTPUT();

if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else {
std::vector<std::string> argStrings = splitString(function);
double initSpeed = std::stod(argStrings[0]);
Expand All @@ -117,10 +113,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
std::stringstream sstream;
sstream << result;

strcpy(output, sstream.str().c_str());
output[outputSize - 1] = '\0';
strncpy_s(output, outputSize, sstream.str().c_str(), _TRUNCATE);
}
EXTENSION_RETURN();
}

#pragma warning( pop )
13 changes: 7 additions & 6 deletions extensions/medical/medical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

#include "shared.hpp"

#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
Expand All @@ -31,13 +33,13 @@ std::vector<std::string> parseExtensionInput(const std::string& input)

void __stdcall RVExtension(char *output, int outputSize, const char *function) {
if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
}
else
else
{
std::string returnValue = "";
std::vector<std::string> arguments = parseExtensionInput(function);
if (arguments.size() > 0)
if (arguments.size() > 0)
{
try {
std::string command = arguments.at(0);
Expand Down Expand Up @@ -82,8 +84,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
returnValue = debugreturn.str();
}
}
strncpy(output, returnValue.c_str(), outputSize);
output[outputSize - 1] = '\0';

strncpy_s(output, outputSize, returnValue.c_str(), _TRUNCATE);
}
}

21 changes: 8 additions & 13 deletions extensions/parse_imagepath/ace_parse_imagepath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "shared.hpp"

#include <stdlib.h>
#include <sstream>
#include <string>

Expand All @@ -36,18 +37,12 @@ std::string getImagePathFromStructuredText(const std::string & input) {
return returnValue;
}

#pragma warning( push )
#pragma warning( disable : 4996 )

void __stdcall RVExtension(char *output, int outputSize, const char *function) {
ZERO_OUTPUT();
if (!strcmp(function, "version")) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else {
strncpy_s(output, outputSize, getImagePathFromStructuredText(function).c_str(), _TRUNCATE);
output[outputSize - 1] = '\0';
}
EXTENSION_RETURN();
ZERO_OUTPUT();
if (!strcmp(function, "version")) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
} else {
strncpy_s(output, outputSize, getImagePathFromStructuredText(function).c_str(), _TRUNCATE);
}
EXTENSION_RETURN();
}

#pragma warning( pop )

0 comments on commit b16f444

Please sign in to comment.