Skip to content

Parsing -+nan[(n-char-seq-opt)] and -+inf[inity] succeeds with FASTFLOAT_ALLOWS_LEADING_PLUS #276

@dalle

Description

@dalle

The following strings succeeds to parse when FASTFLOAT_ALLOWS_LEADING_PLUS is defined:

"-+nan" // parsed as nan
"-+nan(abc)" // parsed as nan
"-+inf" // parsed as -inf
"-+infinity" // parsed as -inf

Mixing both - and + should result in an error.

To resolve this, replace:

  bool minusSign = false;
  if (*first ==
      UC('-')) { // assume first < last, so dereference without checks;
                 // C++17 20.19.3.(7.1) explicitly forbids '+' here
    minusSign = true;
    ++first;
  }
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
  if (*first == UC('+')) {
    ++first;
  }
#endif

with:

  bool minusSign = (*first == UC('-'));
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
  if ((*first == UC('-')) || (*first == UC('+'))) {
#else
  if (*first == UC('-')) {
#endif
    ++first;
  }

I'll create a PR with fix and additional tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions