Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strncpy and disable MSVC unsafe warnings #7171

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ add_definitions(/DWINVER=0x0600 /D_WIN32_WINNT=0x0600)
endif()

if (NOT CMAKE_BUILD_TYPE AND CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug")
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()

option(DEVEL "DEVEL" OFF)
Expand All @@ -18,19 +18,29 @@ option(USE_DIRECTX "USE_DIRECTX" OFF)
option(USE_64BIT_BUILD "USE_64BIT_BUILD" OFF)
option(USE_STATIC_LINKING "USE_STATIC_LINKING" ON)

if(CMAKE_GENERATOR_PLATFORM MATCHES "x64")
set(USE_64BIT_BUILD ON)
endif()

if(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS "-std=c++11 -pedantic -pedantic-errors -march=i686 -m32 -O2 -s -fPIC -fpermissive")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_SHARED_LINKER_FLAGS "-static-libgcc -static-libstdc++")
SET(CMAKE_CXX_FLAGS "-std=c++11 -pedantic -pedantic-errors -march=i686 -m32 -O2 -s -fPIC -fpermissive")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_SHARED_LINKER_FLAGS "-static-libgcc -static-libstdc++")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(ERROR "SUPPORT NOT COMPLETE")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /arch:SSE2 /Qpar-report:2")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} /D _DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} /MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} /MT /Zi /O2 /Ob1 /D NDEBUG")
add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Disable MSVC *_s function warnings

if(USE_64BIT_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /Qpar-report:2") # Default SSE2
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /arch:SSE2 /Qpar-report:2")
endif()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} /D _DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} /MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /MT /O2 /Ob2 /D NDEBUG")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might aswell set /Ox for release?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always build RelWithDebInfo for releases.

Anyways, that's for separate PR should we do it.

set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} /MT /Zi /O2 /Ob1 /D NDEBUG")
endif()

include_directories("common")
Expand Down
30 changes: 15 additions & 15 deletions extensions/advanced_ballistics/AdvancedBallistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ double calculateAdvancedZero(double zeroRange, double muzzleVelocity, double bor
double v = 0.0f;

while (tof < 8.0f && px < zeroRange) {
lx = px;
lx = px;
ly = py;

v = std::sqrt(vx*vx + vy*vy);
Expand Down Expand Up @@ -295,15 +295,15 @@ extern "C"

void __stdcall RVExtensionVersion(char *output, int outputSize)
{
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
}

void __stdcall RVExtension(char *output, int outputSize, const char *function)
{
ZERO_OUTPUT();
std::stringstream outputStr;
if (!strcmp(function, "version")) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
EXTENSION_RETURN();
}

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

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

EXTENSION_RETURN();
} else if (!strcmp(mode, "atmosphericCorrection")) {
Expand All @@ -347,7 +347,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_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
} else if (!strcmp(mode, "new")) {
unsigned int index = 0;
Expand Down Expand Up @@ -453,7 +453,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].randGenerator.seed(bulletDatabase[index].randSeed);
}

strncpy_s(output, outputSize, "", _TRUNCATE);
strncpy(output, "", outputSize);
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 @@ -567,7 +567,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
ace::vector3<double> offset(distribution(bulletDatabase[index].randGenerator), distribution(bulletDatabase[index].randGenerator), distribution(bulletDatabase[index].randGenerator));
double coef = 1.0f - bulletDatabase[index].transonicStabilityCoef;

double trueSpeed = trueVelocity.magnitude();
double trueSpeed = trueVelocity.magnitude();
trueVelocity += offset * coef;
trueVelocity = trueVelocity.normalize() * trueSpeed;
};
Expand Down Expand Up @@ -622,7 +622,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].bulletVelocityPreviousFrame = bulletVelocityCurrentFrame + velocityOffset;

outputStr << "[" << velocityOffset.x() << "," << velocityOffset.y() << "," << velocityOffset.z() << "]";
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
} else if (!strcmp(mode, "set")) {
int height = 0;
Expand All @@ -637,7 +637,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
map->gridBuildingNums.push_back(numObjects);
map->gridSurfaceIsWater.push_back(surfaceIsWater);

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

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

strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
} else if (!strcmp(mode, "replicateVanillaZero")) {
float zeroRange = strtof(strtok_s(NULL, ":", &next_token), NULL);
Expand All @@ -676,7 +676,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
float zeroAngle = replicateVanillaZero(zeroRange, initSpeed, airFriction);

outputStr << DEGREES(zeroAngle);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
} else if (!strcmp(mode, "calcZero")) {
double zeroRange = strtod(strtok_s(NULL, ":", &next_token), NULL);
Expand All @@ -687,7 +687,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double zeroAngle = calculateVanillaZero(zeroRange, initSpeed, airFriction, boreHeight);

outputStr << DEGREES(zeroAngle);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
} else if (!strcmp(mode, "calcZeroAB")) {
double zeroRange = strtod(strtok_s(NULL, ":", &next_token), NULL);
Expand All @@ -703,9 +703,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double zeroAngle = calculateAdvancedZero(zeroRange, muzzleVelocity, boreHeight, temperature, pressure, humidity, ballisticCoefficient, dragModel, atmosphereModel);

outputStr << DEGREES(zeroAngle);
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
}
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
strncpy(output, outputStr.str().c_str(), outputSize);
EXTENSION_RETURN();
}
4 changes: 2 additions & 2 deletions extensions/break_line/ace_break_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ std::string addLineBreaks(const std::vector<std::string> &words) {
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);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
} else {
strncpy_s(output, outputSize, addLineBreaks(splitString(function)).c_str(), _TRUNCATE);
strncpy(output, addLineBreaks(splitString(function)).c_str(), outputSize);
}
EXTENSION_RETURN();
}
6 changes: 3 additions & 3 deletions extensions/clipboard/ace_clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
}

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

Expand All @@ -53,7 +53,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
result = "GlobalAlloc() failed, GetLastError=" + GetLastError();
EXTENSION_RETURN();
}
strncpy_s(pClipboardData, gClipboardData.length(), gClipboardData.c_str(), _TRUNCATE);
strncpy(pClipboardData, gClipboardData.c_str(), gClipboardData.length());

// if success, system owns the memory, if fail, free it from the heap
if (SetClipboardData(CF_TEXT, pClipboardData) == NULL) {
Expand All @@ -72,7 +72,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
}

if (result.length() > 1) {
strncpy_s(output, outputSize, result.c_str(), _TRUNCATE);
strncpy(output, result.c_str(), outputSize);
}

#endif
Expand Down
4 changes: 2 additions & 2 deletions extensions/fcs/ace_fcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ double getSolution(double initSpeed, double airFriction, double angleTarget, dou
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);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
} else {
std::vector<std::string> argStrings = splitString(function);
double initSpeed = std::stod(argStrings[0]);
Expand All @@ -109,7 +109,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
std::stringstream sstream;
sstream << result;

strncpy_s(output, outputSize, sstream.str().c_str(), _TRUNCATE);
strncpy(output, sstream.str().c_str(), outputSize);
}
EXTENSION_RETURN();
}
4 changes: 2 additions & 2 deletions extensions/medical/medical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::vector<std::string> parseExtensionInput(const std::string& input)

void __stdcall RVExtension(char *output, int outputSize, const char *function) {
if (!strcmp(function, "version")) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
}
else
{
Expand Down Expand Up @@ -85,6 +85,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) {
}
}

strncpy_s(output, outputSize, returnValue.c_str(), _TRUNCATE);
strncpy(output, returnValue.c_str(), outputSize);
}
}
4 changes: 2 additions & 2 deletions extensions/parse_imagepath/ace_parse_imagepath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ std::string getImagePathFromStructuredText(const std::string & input) {
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);
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
} else {
strncpy_s(output, outputSize, getImagePathFromStructuredText(function).c_str(), _TRUNCATE);
strncpy(output, getImagePathFromStructuredText(function).c_str(), outputSize);
}
EXTENSION_RETURN();
}