Unverified Commit b2da19b8 authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

check feature names for special JSON chars (#2557)

parent 83ecb386
...@@ -561,7 +561,11 @@ class Dataset { ...@@ -561,7 +561,11 @@ class Dataset {
for (auto& feature_name : feature_names_) { for (auto& feature_name : feature_names_) {
// check ascii // check ascii
if (!Common::CheckASCII(feature_name)) { if (!Common::CheckASCII(feature_name)) {
Log::Fatal("Do not support non-ascii characters in feature name."); Log::Fatal("Do not support non-ASCII characters in feature name.");
}
// check json
if (!Common::CheckAllowedJSON(feature_name)) {
Log::Fatal("Do not support special JSON characters in feature name.");
} }
if (feature_name.find(' ') != std::string::npos) { if (feature_name.find(' ') != std::string::npos) {
spaceInFeatureName = true; spaceInFeatureName = true;
......
...@@ -927,6 +927,24 @@ inline bool CheckASCII(const std::string& s) { ...@@ -927,6 +927,24 @@ inline bool CheckASCII(const std::string& s) {
return true; return true;
} }
inline bool CheckAllowedJSON(const std::string& s) {
unsigned char char_code;
for (auto c : s) {
char_code = static_cast<unsigned char>(c);
if (char_code == 34 // "
|| char_code == 44 // ,
|| char_code == 58 // :
|| char_code == 91 // [
|| char_code == 93 // ]
|| char_code == 123 // {
|| char_code == 125 // }
) {
return false;
}
}
return true;
}
} // namespace Common } // namespace Common
} // namespace LightGBM } // namespace LightGBM
......
...@@ -21,7 +21,7 @@ void Config::KV2Map(std::unordered_map<std::string, std::string>* params, const ...@@ -21,7 +21,7 @@ void Config::KV2Map(std::unordered_map<std::string, std::string>* params, const
value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1])); value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1]));
} }
if (!Common::CheckASCII(key) || !Common::CheckASCII(value)) { if (!Common::CheckASCII(key) || !Common::CheckASCII(value)) {
Log::Fatal("Do not support non-ascii characters in config."); Log::Fatal("Do not support non-ASCII characters in config.");
} }
if (key.size() > 0) { if (key.size() > 0) {
auto value_search = params->find(key); auto value_search = params->find(key);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment