Skip to content

Commit

Permalink
resolved overflowable value assignment in lexical_analyzer class test
Browse files Browse the repository at this point in the history
  • Loading branch information
fktn-k committed Oct 19, 2023
1 parent 8a963e1 commit fd99d0f
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions include/fkYAML/detail/input/lexical_analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class lexical_analyzer
m_input_handler.get_next();
break;
}
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
}
return m_last_token_type = lexical_token_t::ANCHOR_PREFIX;
}
Expand All @@ -171,7 +171,7 @@ class lexical_analyzer
m_input_handler.get_next();
break;
}
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
}
return m_last_token_type = lexical_token_t::ALIAS_PREFIX;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ class lexical_analyzer
case '\'':
return m_last_token_type = scan_string();
case '~':
m_value_buffer = current;
m_value_buffer = char_traits_type::to_char_type(current);
return m_last_token_type = lexical_token_t::NULL_VALUE;
case '+':
return m_last_token_type = scan_number();
Expand Down Expand Up @@ -495,19 +495,19 @@ class lexical_analyzer
{
throw fkyaml::exception("Invalid YAML major version found.");
}
m_value_buffer.push_back(m_input_handler.get_current());
m_value_buffer.push_back(char_traits_type::to_char_type(m_input_handler.get_current()));

if (m_input_handler.get_next() != '.')
{
throw fkyaml::exception("A period must be followed after the YAML major version.");
}
m_value_buffer.push_back(m_input_handler.get_current());
m_value_buffer.push_back(char_traits_type::to_char_type(m_input_handler.get_current()));

switch (m_input_handler.get_next())
{
case '1':
case '2':
m_value_buffer.push_back(m_input_handler.get_current());
m_value_buffer.push_back(char_traits_type::to_char_type(m_input_handler.get_current()));
break;
case '0':
case '3':
Expand Down Expand Up @@ -548,14 +548,14 @@ class lexical_analyzer
switch (current)
{
case '-':
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
ret = scan_negative_number();
break;
case '+':
ret = scan_decimal_number();
break;
case '0':
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
ret = scan_number_after_zero_at_first();
break;
case '1':
Expand All @@ -567,7 +567,7 @@ class lexical_analyzer
case '7':
case '8':
case '9':
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
ret = scan_decimal_number();
break;
default: // LCOV_EXCL_LINE
Expand Down Expand Up @@ -601,7 +601,7 @@ class lexical_analyzer

if (std::isdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number();
}

Expand Down Expand Up @@ -634,15 +634,15 @@ class lexical_analyzer
switch (next)
{
case '.':
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number_after_decimal_point();
case 'o':
// Do not store 'o' since std::strtoull does not support "0o" but "0" as the prefix for octal numbers.
// YAML specifies octal values start with the prefix "0o".
// See "10.3.2. Tag Resolution" section in https://yaml.org/spec/1.2.2/
return scan_octal_number();
case 'x':
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_hexadecimal_number();
default:
return lexical_token_t::INTEGER_VALUE;
Expand All @@ -660,7 +660,7 @@ class lexical_analyzer

if (std::isdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
scan_decimal_number();
return lexical_token_t::FLOAT_NUMBER_VALUE;
}
Expand All @@ -678,12 +678,12 @@ class lexical_analyzer
char_int_type next = m_input_handler.get_next();
if (next == '+' || next == '-')
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
scan_decimal_number_after_sign();
}
else if (std::isdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
scan_decimal_number();
}
else
Expand All @@ -704,7 +704,7 @@ class lexical_analyzer

if (std::isdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number();
}

Expand All @@ -722,24 +722,25 @@ class lexical_analyzer

if (std::isdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number();
}

if (next == '.')
{
if (m_value_buffer.find(next) != string_type::npos) // NOLINT(abseil-string-find-str-contains)
// NOLINTNEXTLINE(abseil-string-find-str-contains)
if (m_value_buffer.find(char_traits_type::to_char_type(next)) != string_type::npos)
{
// TODO: support this use case (e.g. version info like 1.0.0)
throw fkyaml::exception("Multiple decimal points found in a token.");
}
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number_after_decimal_point();
}

if (next == 'e' || next == 'E')
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
return scan_decimal_number_after_exponent();
}

Expand All @@ -756,7 +757,7 @@ class lexical_analyzer
char_int_type next = m_input_handler.get_next();
if ('0' <= next && next <= '7')
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
scan_octal_number();
}
return lexical_token_t::INTEGER_VALUE;
Expand All @@ -772,7 +773,7 @@ class lexical_analyzer
char_int_type next = m_input_handler.get_next();
if (std::isxdigit(next))
{
m_value_buffer.push_back(next);
m_value_buffer.push_back(char_traits_type::to_char_type(next));
scan_hexadecimal_number();
}
return lexical_token_t::INTEGER_VALUE;
Expand Down Expand Up @@ -818,7 +819,7 @@ class lexical_analyzer
{
return lexical_token_t::STRING_VALUE;
}
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -836,7 +837,7 @@ class lexical_analyzer
}

// if the target is a single-quoted string token.
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -852,7 +853,7 @@ class lexical_analyzer
// an escaped single quotation mark.
if (m_input_handler.test_next_char('\''))
{
m_value_buffer.push_back(m_input_handler.get_next());
m_value_buffer.push_back(char_traits_type::to_char_type(m_input_handler.get_next()));
continue;
}

Expand All @@ -865,7 +866,7 @@ class lexical_analyzer
// Just regard a colon as a character if surrounded by quotation marks.
if (needs_last_double_quote || needs_last_single_quote)
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -875,7 +876,7 @@ class lexical_analyzer
// A colon as a key separator must be followed by a space or a newline code.
if (next != ' ' && next != '\r' && next != '\n')
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -895,7 +896,7 @@ class lexical_analyzer
// Just regard a comma as a character if surrounded by quotation marks.
if (needs_last_double_quote || needs_last_single_quote)
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -908,7 +909,7 @@ class lexical_analyzer
// just regard a right square bracket as a character if surrounded by quotation marks.
if (needs_last_double_quote || needs_last_single_quote)
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand All @@ -921,7 +922,7 @@ class lexical_analyzer
// just regard a right curly brace as a character if surrounded by quotation marks.
if (needs_last_double_quote || needs_last_single_quote)
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand Down Expand Up @@ -1018,7 +1019,7 @@ class lexical_analyzer
// Handle ASCII characters except control characters.
if (current <= 0x7E)
{
m_value_buffer.push_back(current);
m_value_buffer.push_back(char_traits_type::to_char_type(current));
continue;
}

Expand Down Expand Up @@ -1056,7 +1057,7 @@ class lexical_analyzer
case 0x08:
throw fkyaml::exception("Control character U+0008 (BS) must be escaped to \\b or \\u0008.");
case 0x09: // HT
m_value_buffer.push_back(c);
m_value_buffer.push_back(char_traits_type::to_char_type(c));
break;
// 0x0A(LF) has already been handled above.
case 0x0B:
Expand Down

0 comments on commit fd99d0f

Please sign in to comment.