From a5539692ec947d34741398d0dba34032c88fa298 Mon Sep 17 00:00:00 2001 From: jonpas Date: Tue, 10 Sep 2019 15:48:06 +0200 Subject: [PATCH] Use strncpy and disable MSVC unsafe warnings (#7171) * Use strncpy and disable MSVC unsafe warning * Set 64-bit correctly from generator string, Disable SSE2 flag on 64-bit (non-existent) --- extensions/CMakeLists.txt | 30 ++++++++++++------- .../AdvancedBallistics.cpp | 30 +++++++++---------- extensions/break_line/ace_break_line.cpp | 4 +-- extensions/clipboard/ace_clipboard.cpp | 6 ++-- extensions/fcs/ace_fcs.cpp | 4 +-- extensions/medical/medical.cpp | 4 +-- .../parse_imagepath/ace_parse_imagepath.cpp | 4 +-- 7 files changed, 46 insertions(+), 36 deletions(-) diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index ef6d8f8ded4..0ac2fa50616 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -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) @@ -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") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} /MT /Zi /O2 /Ob1 /D NDEBUG") endif() include_directories("common") diff --git a/extensions/advanced_ballistics/AdvancedBallistics.cpp b/extensions/advanced_ballistics/AdvancedBallistics.cpp index b77c412b35f..6ea8d1fdea1 100644 --- a/extensions/advanced_ballistics/AdvancedBallistics.cpp +++ b/extensions/advanced_ballistics/AdvancedBallistics.cpp @@ -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); @@ -295,7 +295,7 @@ 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) @@ -303,7 +303,7 @@ 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(); } @@ -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")) { @@ -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; @@ -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 @@ -567,7 +567,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function) ace::vector3 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; }; @@ -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; @@ -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; @@ -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(); } @@ -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); @@ -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); @@ -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); @@ -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(); } diff --git a/extensions/break_line/ace_break_line.cpp b/extensions/break_line/ace_break_line.cpp index c408ceab5cb..1ac9b9009df 100644 --- a/extensions/break_line/ace_break_line.cpp +++ b/extensions/break_line/ace_break_line.cpp @@ -64,9 +64,9 @@ std::string addLineBreaks(const std::vector &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(); } diff --git a/extensions/clipboard/ace_clipboard.cpp b/extensions/clipboard/ace_clipboard.cpp index 0b3d965ad5d..9b5cf09d550 100644 --- a/extensions/clipboard/ace_clipboard.cpp +++ b/extensions/clipboard/ace_clipboard.cpp @@ -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(); } @@ -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) { @@ -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 diff --git a/extensions/fcs/ace_fcs.cpp b/extensions/fcs/ace_fcs.cpp index 8ab57a1c778..1c00212c27e 100644 --- a/extensions/fcs/ace_fcs.cpp +++ b/extensions/fcs/ace_fcs.cpp @@ -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 argStrings = splitString(function); double initSpeed = std::stod(argStrings[0]); @@ -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(); } diff --git a/extensions/medical/medical.cpp b/extensions/medical/medical.cpp index aca062e2050..ef6c00fde9a 100644 --- a/extensions/medical/medical.cpp +++ b/extensions/medical/medical.cpp @@ -33,7 +33,7 @@ std::vector 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 { @@ -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); } } diff --git a/extensions/parse_imagepath/ace_parse_imagepath.cpp b/extensions/parse_imagepath/ace_parse_imagepath.cpp index 110cb904cc3..8f52a1e4bcf 100644 --- a/extensions/parse_imagepath/ace_parse_imagepath.cpp +++ b/extensions/parse_imagepath/ace_parse_imagepath.cpp @@ -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(); }