1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
Copyright (c) 2016, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sql/options_parser.h"
#include <assert.h>
#include <stddef.h>
#include <algorithm>
#include <cctype>
#include <stdexcept>
#include <utility>
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysqld_error.h"
#include "sql_string.h"
namespace options_parser {
enum class parser_status {
OK,
MISSING_SEPARATOR,
MORE_THAN_ONE_SEPARATOR,
SEQUENTIAL_SEPARATORS,
SEQUENTIAL_DELIMITERS,
STARTS_WITH_INVALID_CHARACTER,
ENDS_WITH_INVALID_CHARACTER,
DUPLICATE_KEY,
};
/**
Struct for storing the parsing result. Gives the user
specific feedback if an invalid string is entered.
*/
struct result {
/**
Whether parsing was successful or not, and what type of error
occurred, if any.
*/
parser_status status;
/**
Information about the specific error that occurred.
*/
std::string msg;
/**
Character that caused the parsing to fail.
*/
char c;
};
/**
Parses the options string into keys and values
Parses an options string into a set of keys and values, and stores the
result in an an std::map pointer. The string is split based on a delimiter
and a key-value separator, and might look something like:
"key1=value1, key2=value2, key3=value3...".
@param str unparsed options string
@param delimiter character to split the options string into several substrings
@param key_value_separator value which separates string into key and value
@param options pointer to an std::map
@return result
*/
result parse_options_string(String *str, char delimiter,
char key_value_separator,
std::map<std::string, std::string> *options) {
std::string str_copy(str->ptr(), str->length());
size_t str_start_pos = 0;
size_t str_end_pos = str_copy.length() - 1;
result parser_result;
if (str_copy.size() == 0) {
parser_result.status = parser_status::OK;
parser_result.msg = "";
return parser_result;
}
// Remove whitespaces at beginning of string.
for (size_t i = 0; i <= str_copy.size() - 1; i++) {
if (!std::isspace(str_copy.at(i))) {
str_start_pos = i;
break;
}
// In case of string containing only whitespaces.
if (i == str_copy.size() - 1) {
parser_result.status = parser_status::OK;
parser_result.msg = "";
return parser_result;
}
}
// Remove whitespaces at end of string.
for (int i = static_cast<int>(str_copy.size()) - 1; i >= 0; i--) {
if (!std::isspace(str_copy.at(i))) {
if (i < static_cast<int>(str_copy.size() - 1)) {
str_end_pos = i;
}
break;
}
}
std::transform(str_copy.begin(), str_copy.end(), str_copy.begin(), ::tolower);
// String cannot start or end with the delimiter value.
if (str_copy.at(str_start_pos) == delimiter) {
parser_result.status = parser_status::STARTS_WITH_INVALID_CHARACTER;
parser_result.c = delimiter;
return parser_result;
}
if (str_copy.at(str_start_pos) == key_value_separator) {
parser_result.status = parser_status::STARTS_WITH_INVALID_CHARACTER;
parser_result.c = key_value_separator;
return parser_result;
}
if (str_copy.at(str_end_pos) == delimiter) {
parser_result.status = parser_status::ENDS_WITH_INVALID_CHARACTER;
parser_result.c = delimiter;
return parser_result;
}
if (str_copy.at(str_end_pos) == key_value_separator) {
parser_result.status = parser_status::ENDS_WITH_INVALID_CHARACTER;
parser_result.c = key_value_separator;
return parser_result;
}
size_t substring_startpos = str_start_pos;
size_t delimiter_count =
std::count(str_copy.begin(), str_copy.end(), delimiter);
size_t substring_endpos = 0;
size_t key_value_separator_pos = 0;
for (size_t i = 0; i <= delimiter_count; i++) {
int prep_whitespace_counter = 0;
int trailing_whitespace_counter = 0;
if (i == delimiter_count) {
substring_endpos = str_end_pos;
} else {
assert(substring_endpos != std::string::npos);
substring_endpos = str_copy.find(delimiter, substring_startpos);
for (size_t j = substring_endpos - 1; j >= substring_startpos; j--) {
if (std::isspace(str_copy.at(j))) {
trailing_whitespace_counter++;
} else {
break;
}
}
substring_endpos -= trailing_whitespace_counter;
prep_whitespace_counter = 0;
for (size_t j = substring_endpos + trailing_whitespace_counter + 1;
j < str_copy.length(); j++) {
if (std::isspace(str_copy.at(j))) {
prep_whitespace_counter++;
} else {
break;
}
}
if (str_copy.at(substring_endpos + trailing_whitespace_counter +
prep_whitespace_counter + 1) == delimiter) {
parser_result.status = parser_status::SEQUENTIAL_DELIMITERS;
parser_result.c = delimiter;
return parser_result;
}
substring_endpos -= 1; // Decrement to set end position at position
// before delimiter.
}
size_t key_value_separator_count =
std::count(str_copy.begin() + substring_startpos,
str_copy.begin() + substring_endpos, key_value_separator);
if (key_value_separator_count == 0) {
parser_result.status = parser_status::MISSING_SEPARATOR;
parser_result.msg = str_copy.substr(
substring_startpos, substring_endpos - substring_startpos + 1);
parser_result.c = key_value_separator;
return parser_result;
}
key_value_separator_pos =
str_copy.find(key_value_separator, substring_startpos);
size_t key_startpos = substring_startpos;
size_t key_endpos = key_value_separator_pos - 1;
size_t value_startpos = key_value_separator_pos + 1;
size_t value_endpos = substring_endpos;
for (size_t j = key_value_separator_pos - 1; j >= substring_startpos; j--) {
if (std::isspace(str_copy.at(j))) {
key_endpos--;
} else {
break;
}
}
// Remove whitespaces after key-value separator.
for (size_t j = key_value_separator_pos + 1; j < str_copy.length(); j++) {
if (std::isspace(str_copy.at(j))) {
value_startpos++;
} else {
break;
}
}
if (str_copy.at(value_startpos) == key_value_separator) {
parser_result.status = parser_status::SEQUENTIAL_SEPARATORS;
parser_result.c = key_value_separator;
return parser_result;
}
std::string key =
str_copy.substr(key_startpos, key_endpos - key_startpos + 1);
std::string value =
str_copy.substr(value_startpos, value_endpos - value_startpos + 1);
if (key_value_separator_count > 1) {
parser_result.status = parser_status::MORE_THAN_ONE_SEPARATOR;
parser_result.msg = key + key_value_separator + value;
parser_result.c = key_value_separator;
return parser_result;
}
if (str_copy.at(substring_startpos) == key_value_separator) {
parser_result.status = parser_status::STARTS_WITH_INVALID_CHARACTER;
parser_result.c = key_value_separator;
return parser_result;
}
if (str_copy.at(substring_endpos) == key_value_separator) {
parser_result.status = parser_status::ENDS_WITH_INVALID_CHARACTER;
parser_result.c = key_value_separator;
return parser_result;
}
// Duplicate entry check.
if (!options->emplace(key, value).second) {
parser_result.status = parser_status::DUPLICATE_KEY;
parser_result.msg = key;
parser_result.c = key_value_separator;
return parser_result;
}
// Skip delimiter.
substring_startpos = substring_endpos + prep_whitespace_counter +
trailing_whitespace_counter + 2;
}
parser_result.status = parser_status::OK;
return parser_result;
}
/**
Determines result of parsing operation, raises error if parsing was
unsuccessful.
Determines the result of parsing operation by looking at the status
field of the result struct, and raises an appropriate error if the
parsing operation is unsuccessful.
@param result struct returned from parsing function
@param function_name name of function that uses parsing function
@retval false parsing was successful
@retval true parsing was ununsuccessful
*/
bool resolve_parser_result(result result, const char *function_name) {
switch (result.status) {
case parser_status::STARTS_WITH_INVALID_CHARACTER: {
my_error(ER_INVALID_OPTION_START_CHARACTER, MYF(0), function_name,
result.c);
return true;
}
case parser_status::ENDS_WITH_INVALID_CHARACTER: {
my_error(ER_INVALID_OPTION_END_CHARACTER, MYF(0), function_name,
result.c);
return true;
}
case parser_status::MISSING_SEPARATOR:
case parser_status::MORE_THAN_ONE_SEPARATOR: {
my_error(ER_INVALID_OPTION_KEY_VALUE_PAIR, MYF(0), result.msg.c_str(),
result.c, function_name);
return true;
}
case parser_status::SEQUENTIAL_SEPARATORS:
case parser_status::SEQUENTIAL_DELIMITERS: {
std::string invalid_character_sequence;
invalid_character_sequence += result.c;
invalid_character_sequence += result.c;
my_error(ER_INVALID_OPTION_CHARACTERS, MYF(0), function_name,
invalid_character_sequence.c_str());
return true;
}
case parser_status::DUPLICATE_KEY: {
my_error(ER_DUPLICATE_OPTION_KEY, MYF(0), result.msg.c_str(),
function_name);
return true;
}
case parser_status::OK: {
return false;
}
}
return true;
}
bool parse_string(String *str, std::map<std::string, std::string> *map,
const char *func_name) {
/*
Since we're using certain std::string functions (such as std::string.at()),
the code might give an out_of_range exception. Therefore, a try-catch with
a generic error message is necessary.
*/
try {
return resolve_parser_result(parse_options_string(str, ',', '=', map),
func_name);
} catch (const std::out_of_range &) {
assert(false);
my_error(ER_UNKNOWN_ERROR, MYF(0));
return true;
}
}
} // namespace options_parser
|