Skip to content
Open
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
8 changes: 3 additions & 5 deletions src/parser/seclang-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3363,7 +3363,7 @@
3972, 3972, 3972, 3972, 3972, 3972, 3972, 3972, 3972, 3972
} ;

static const flex_int16_t yy_chk[13611] =

Check warning on line 3366 in src/parser/seclang-scanner.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use "std::array" or "std::vector" instead of a C-style array.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AZ919VKNGPmZJSHO2FyE&open=AZ919VKNGPmZJSHO2FyE&pullRequest=3582
{ 0,
0, 1, 1, 1, 1, 5, 1, 1, 5, 6,
0, 71, 6, 9, 9, 1, 9, 9, 72, 21,
Expand Down Expand Up @@ -4953,6 +4953,7 @@
#include "src/parser/seclang-parser.hh"
#include "src/utils/https_client.h"
#include "src/utils/string.h"
#include "src/utils/system.h"

using modsecurity::Parser::Driver;
using modsecurity::Utils::HttpsClient;
Expand Down Expand Up @@ -8400,8 +8401,7 @@
driver.loc.push_back(new yy::location());
driver.m_filenames.push_back(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
yyin = fopen(f.c_str(), "r" );
if (!yyin) {
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) { // NOSONAR
BEGIN(INITIAL);
driver.loc.pop_back();
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
Expand Down Expand Up @@ -8432,9 +8432,7 @@
driver.loc.push_back(new yy::location());
driver.m_filenames.push_back(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());

yyin = fopen(f.c_str(), "r" );
if (!yyin) {
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) { // NOSONAR
BEGIN(INITIAL);
driver.loc.pop_back();
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
Expand Down
7 changes: 3 additions & 4 deletions src/parser/seclang-scanner.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "src/parser/seclang-parser.hh"
#include "src/utils/https_client.h"
#include "src/utils/string.h"
#include "src/utils/system.h"

using modsecurity::Parser::Driver;
using modsecurity::Utils::HttpsClient;
Expand Down Expand Up @@ -1273,8 +1274,7 @@ EQUALS_MINUS (?i:=\-)
driver.loc.push_back(new yy::location());
driver.m_filenames.push_back(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());
yyin = fopen(f.c_str(), "r" );
if (!yyin) {
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) {
BEGIN(INITIAL);
driver.loc.pop_back();
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
Expand Down Expand Up @@ -1303,8 +1303,7 @@ EQUALS_MINUS (?i:=\-)
driver.m_filenames.push_back(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = &(driver.m_filenames.back());

yyin = fopen(f.c_str(), "r" );
if (!yyin) {
if (!modsecurity::utils::fopen_modsec(&yyin, f.c_str(), "r")) {
BEGIN(INITIAL);
driver.loc.pop_back();
driver.error (*driver.loc.back(), "", s + std::string(": Not able to open file. ") + err);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/shared_files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include "src/utils/shared_files.h"
#include "src/utils/system.h"

#include <fcntl.h>
#ifdef WIN32
Expand All @@ -27,8 +28,8 @@ namespace utils {

SharedFiles::handlers_map::iterator SharedFiles::add_new_handler(
const std::string &fileName, std::string *error) {
FILE *fp = fopen(fileName.c_str(), "a");
if (fp == 0) {
FILE *fp;
if (!fopen_modsec(&fp, fileName.c_str(), "a")) {
error->assign("Failed to open file: " + fileName);
return m_handlers.end();
}
Expand Down
23 changes: 20 additions & 3 deletions src/utils/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ bool createDir(const std::string& dir, int mode, std::string *error) {

bool isFile(const std::string& f) {
struct stat fileInfo;
FILE *fp = fopen(f.c_str(), "r");
if (fp == NULL) {
FILE *fp;
if (!fopen_modsec(&fp, f.c_str(), "r")) {
return false;
}
fstat(fileno(fp), &fileInfo);
Expand All @@ -219,6 +219,23 @@ bool isFile(const std::string& f) {
return true;
}


#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode) {
if (v_fp == nullptr || filename == nullptr || mode == nullptr) {
return false;
}
#if defined(_MSC_VER)
return fopen_s(v_fp, filename, mode) == 0 && *v_fp != nullptr;
#else
*v_fp = fopen(filename, mode);
return *v_fp != nullptr;
#endif
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
} // namespace utils
} // namespace modsecurity
3 changes: 3 additions & 0 deletions src/utils/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*
*/

#include <stdio.h>

#include <string>
#include <list>

Expand All @@ -33,6 +35,7 @@ std::string get_path(const std::string& file);
std::list<std::string> expandEnv(const std::string& var, int flags);
bool createDir(const std::string& dir, int mode, std::string *error);
bool isFile(const std::string& f);
bool fopen_modsec(FILE **v_fp, const char *filename, const char *mode);

} // namespace utils
} // namespace modsecurity
Expand Down