Skip to content

Commit

Permalink
Avoid C++ exceptions to be thrown (and caught) when parsing strings l…
Browse files Browse the repository at this point in the history
…ike '+proj=longlat +datum=WGS84 +type=crs'

This makes it more friendly with debuggers under Windows, and should
help workarounding MapServer/MapServer#6915
(note however that the issue in that ticket is more a 'system level'
issue than a PROJ bug)
  • Loading branch information
rouault committed Jul 19, 2023
1 parent 6d10acf commit c5b3d6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/proj/internal/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ PROJ_FOR_TEST std::string toString(double val, int precision = 15);
PROJ_FOR_TEST double
c_locale_stod(const std::string &s); // throw(std::invalid_argument)

// Variant of above that doesn't emit exceptions
double c_locale_stod(const std::string &s, bool &success);

std::string concat(const std::string &, const std::string &) = delete;
std::string concat(const char *, const char *) = delete;
std::string concat(const char *a, const std::string &b);
Expand Down
13 changes: 12 additions & 1 deletion src/iso19111/internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,17 @@ bool ends_with(const std::string &str, const std::string &suffix) noexcept {
// ---------------------------------------------------------------------------

double c_locale_stod(const std::string &s) {
bool success;
double val = c_locale_stod(s, success);
if (!success) {
throw std::invalid_argument("non double value");
}
return val;
}

double c_locale_stod(const std::string &s, bool &success) {

success = true;
const auto s_size = s.size();
// Fast path
if (s_size > 0 && s_size < 15) {
Expand Down Expand Up @@ -277,7 +287,8 @@ double c_locale_stod(const std::string &s) {
double d;
iss >> d;
if (!iss.eof() || iss.fail()) {
throw std::invalid_argument("non double value");
success = false;
d = 0;
}
return d;
}
Expand Down
15 changes: 5 additions & 10 deletions src/iso19111/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10779,16 +10779,11 @@ SphericalCSNNPtr PROJStringParser::Private::buildSphericalCS(

static double getNumericValue(const std::string &paramValue,
bool *pHasError = nullptr) {
try {
double value = c_locale_stod(paramValue);
if (pHasError)
*pHasError = false;
return value;
} catch (const std::invalid_argument &) {
if (pHasError)
*pHasError = true;
return 0.0;
}
bool success;
double value = c_locale_stod(paramValue, success);
if (pHasError)
*pHasError = !success;
return value;
}

// ---------------------------------------------------------------------------
Expand Down

0 comments on commit c5b3d6b

Please sign in to comment.