From: bangerth Date: Sat, 19 Jul 2014 12:43:59 +0000 (+0000) Subject: Detect errors in Utilities::string_to_int. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65cea1f9ce7000c2f3092fa0f365bbb7db80545f;p=dealii-svn.git Detect errors in Utilities::string_to_int. git-svn-id: https://svn.dealii.org/trunk@33201 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 4969a34529..4d033f8846 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -190,6 +190,13 @@ inconvenience this causes.

Specific improvements

    +
  1. Fixed: Utilities::string_to_int() did not catch if the + string given started with an integer but contained additional + text. It now throws an exception if this happens. +
    + (Wolfgang Bangerth, 2014/07/14) +
  2. +
  3. New: The function GridOut::write, can now be used also in the codimension one case.
    @@ -203,7 +210,6 @@ inconvenience this causes. (Luca Heltai, 2014/07/18)
  4. -
  5. New: The GridReordering::reorder_cells() function used a numbering format for the vertices in a cell that was last used in deal.II version 5.2. This format is still used internally, but diff --git a/deal.II/source/base/utilities.cc b/deal.II/source/base/utilities.cc index 16c8d1ab31..b014ee6ff7 100644 --- a/deal.II/source/base/utilities.cc +++ b/deal.II/source/base/utilities.cc @@ -149,18 +149,10 @@ namespace Utilities int string_to_int (const std::string &s) { - std::istringstream ss(s); - - int i = std::numeric_limits::max(); - ss >> i; - // check for errors - AssertThrow (i != std::numeric_limits::max(), - ExcCantConvertString (s)); - -//TODO: The test for errors above doesn't work, as can easily be -//verified. furthermore, it doesn't catch cases like when calling -//string_to_int("1.23.4") since it just reads in however much it can, without -//realizing that there is more + char *p; + const int i = std::strtol(s.c_str(), &p, 10); + AssertThrow ( !((errno != 0) || (s.size() == 0) || ((s.size()>0) && (*p != '\0'))), + ExcMessage ("Can't convert <" + s + "> to an integer.")); return i; }