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

rs-fw-logger - enforce user input validation #5801

Merged
merged 3 commits into from
Feb 6, 2020
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
2 changes: 1 addition & 1 deletion tools/fw-logger/fw-logs-formating-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace fw_logger
}
}

std::unordered_map<std::string, std::vector<std::string>> fw_logs_formating_options::get_enums() const
std::unordered_map<string, std::vector<kvp>> fw_logs_formating_options::get_enums() const
{
return _fw_logs_enum_names_list;
}
Expand Down
8 changes: 5 additions & 3 deletions tools/fw-logger/fw-logs-formating-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ namespace fw_logger
{
struct fw_log_event
{
int num_of_params;
size_t num_of_params;
std::string line;

fw_log_event();
fw_log_event(int input_num_of_params, const std::string& input_line);
};

typedef std::pair<int,std::string> kvp; // XML key/value pair

class fw_logs_xml_helper;

class fw_logs_formating_options
Expand All @@ -33,15 +35,15 @@ namespace fw_logger
bool get_event_data(int id, fw_log_event* log_event_data) const;
bool get_file_name(int id, std::string* file_name) const;
bool get_thread_name(uint32_t thread_id, std::string* thread_name) const;
std::unordered_map<std::string, std::vector<std::string>> get_enums() const;
std::unordered_map<std::string, std::vector<kvp>> get_enums() const;
bool initialize_from_xml();

private:
friend fw_logs_xml_helper;
std::unordered_map<int, fw_log_event> _fw_logs_event_list;
std::unordered_map<int, std::string> _fw_logs_file_names_list;
std::unordered_map<int, std::string> _fw_logs_thread_names_list;
std::unordered_map<std::string, std::vector<std::string>> _fw_logs_enum_names_list;
std::unordered_map<std::string, std::vector<std::pair<int,std::string>>> _fw_logs_enum_names_list;

std::string _xml_full_file_path;
};
Expand Down
27 changes: 19 additions & 8 deletions tools/fw-logger/fw-logs-xml-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ namespace fw_logger
bool fw_logs_xml_helper::build_meta_data_structure(xml_node<> *xml_node_list_of_events, fw_logs_formating_options* logs_formating_options)
{
node_type res = none;
int id;
int num_of_params;
int id{};
int num_of_params{};
string line;

// loop through all elements in the Format.
Expand All @@ -111,11 +111,11 @@ namespace fw_logger
}
else if (res == file)
{
logs_formating_options->_fw_logs_file_names_list.insert(pair<int, string>(id, line));
logs_formating_options->_fw_logs_file_names_list.insert(kvp(id, line));
}
else if (res == thread)
{
logs_formating_options->_fw_logs_thread_names_list.insert(pair<int, string>(id, line));
logs_formating_options->_fw_logs_thread_names_list.insert(kvp(id, line));
}
else if (res == enums)
{
Expand All @@ -127,21 +127,32 @@ namespace fw_logger
if (attr.compare("Name") == 0)
{
string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
vector<string> values;
vector<kvp> xml_kvp;

for (xml_node<>* enum_value_node = enum_node->first_node(); enum_value_node; enum_value_node = enum_value_node->next_sibling())
{
int key = 0;
string value_str;
for (xml_attribute<>* attribute = enum_value_node->first_attribute(); attribute; attribute = attribute->next_attribute())
{
string attr(attribute->name(), attribute->name() + attribute->name_size());
if (attr.compare("Value") == 0)
{
string value_str(attribute->value(), attribute->value() + attribute->value_size());
values.push_back(value_str);
value_str = std::string(attribute->value(), attribute->value() + attribute->value_size());
}
if (attr.compare("Key") == 0)
{
try
{
auto key_str = std::string(attribute->value());
key = std::stoi(key_str, nullptr);
}
catch (...) {}
}
}
xml_kvp.push_back(std::make_pair(key, value_str));
}
logs_formating_options->_fw_logs_enum_names_list.insert(pair<string, vector<string>>(name_attr_str, values));
logs_formating_options->_fw_logs_enum_names_list.insert(pair<string, vector<kvp>>(name_attr_str, xml_kvp));
}
}
}
Expand Down
28 changes: 22 additions & 6 deletions tools/fw-logger/string-formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include "string-formatter.h"
#include "fw-logs-formating-options.h"
#include <regex>
#include <sstream>
#include <iomanip>
#include <iostream>

using namespace std;

namespace fw_logger
{
string_formatter::string_formatter(std::unordered_map<std::string, std::vector<std::string>> enums)
string_formatter::string_formatter(std::unordered_map<std::string, std::vector<kvp>> enums)
:_enums(enums)
{
}
Expand All @@ -19,14 +21,14 @@ namespace fw_logger
{
}

bool string_formatter::generate_message(const string& source, int num_of_params, const uint32_t* params, string* dest)
bool string_formatter::generate_message(const string& source, size_t num_of_params, const uint32_t* params, string* dest)
{
map<string, string> exp_replace_map;
map<string, int> enum_replace_map;

if (params == nullptr && num_of_params > 0) return false;

for (int i = 0; i < num_of_params; i++)
for (size_t i = 0; i < num_of_params; i++)
{
string regular_exp[3];
string replacement[3];
Expand Down Expand Up @@ -83,7 +85,7 @@ namespace fw_logger
string st_regular_exp = "[a-zA-Z]+";
regex e1(st_regular_exp);

for(auto exp = 0; exp<m.size(); exp++)
for(size_t exp = 0; exp<m.size(); exp++)
{
string str = m[exp];

Expand All @@ -92,14 +94,28 @@ namespace fw_logger
regex e2 = e1;
std::regex_search(str, m1, std::regex(e2));

for (auto exp = 0; exp < m1.size(); exp++)
for (size_t exp = 0; exp < m1.size(); exp++)
{
enum_name = m1[exp];
if (_enums.size()>0 && _enums.find(enum_name) != _enums.end())
{
auto vec = _enums[enum_name];
regex e3 = e;
auto res1 = regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, vec[exp_replace_it->second]);
// Verify user's input is within the enumerated range
int val = exp_replace_it->second;
auto it = std::find_if(vec.begin(), vec.end(), [val](kvp& entry){ return entry.first == val; });
if (it != vec.end())
{
regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, it->second);
}
else
{
stringstream s;
s << "Protocol Error recognized!\nImproper log message received: " << source_temp
<< ", invalid parameter: " << exp_replace_it->second << ".\n The range of supported values is \n";
for_each(vec.begin(), vec.end(), [&s](kvp& entry) { s << entry.first << ":" << entry.second << " ,"; });
std::cout << s.str().c_str() << std::endl;;
}
source_temp = destTemp;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tools/fw-logger/string-formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace fw_logger
class string_formatter
{
public:
string_formatter(std::unordered_map<std::string, std::vector<std::string>> enums);
string_formatter(std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> enums);
~string_formatter(void);

bool generate_message(const std::string& source, int num_of_params, const uint32_t* params, std::string* dest);
bool generate_message(const std::string& source, size_t num_of_params, const uint32_t* params, std::string* dest);

private:
bool replace_params(const std::string& source, const std::map<std::string, std::string>& exp_replace_map, const std::map<std::string, int>& enum_replace_map, std::string* dest);

std::unordered_map<std::string, std::vector<std::string>> _enums;
std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> _enums;
};
}