Skip to content

Commit 25367b6

Browse files
authored
Merge pull request OSGeo#3834 from rouault/workaround_mapserver_6915
Avoid C++ exceptions to be thrown (and caught) when parsing strings like '+proj=longlat +datum=WGS84 +type=crs'
2 parents 6d10acf + c5b3d6b commit 25367b6

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

include/proj/internal/internal.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ PROJ_FOR_TEST std::string toString(double val, int precision = 15);
162162
PROJ_FOR_TEST double
163163
c_locale_stod(const std::string &s); // throw(std::invalid_argument)
164164

165+
// Variant of above that doesn't emit exceptions
166+
double c_locale_stod(const std::string &s, bool &success);
167+
165168
std::string concat(const std::string &, const std::string &) = delete;
166169
std::string concat(const char *, const char *) = delete;
167170
std::string concat(const char *a, const std::string &b);

src/iso19111/internal.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,17 @@ bool ends_with(const std::string &str, const std::string &suffix) noexcept {
240240
// ---------------------------------------------------------------------------
241241

242242
double c_locale_stod(const std::string &s) {
243+
bool success;
244+
double val = c_locale_stod(s, success);
245+
if (!success) {
246+
throw std::invalid_argument("non double value");
247+
}
248+
return val;
249+
}
243250

251+
double c_locale_stod(const std::string &s, bool &success) {
252+
253+
success = true;
244254
const auto s_size = s.size();
245255
// Fast path
246256
if (s_size > 0 && s_size < 15) {
@@ -277,7 +287,8 @@ double c_locale_stod(const std::string &s) {
277287
double d;
278288
iss >> d;
279289
if (!iss.eof() || iss.fail()) {
280-
throw std::invalid_argument("non double value");
290+
success = false;
291+
d = 0;
281292
}
282293
return d;
283294
}

src/iso19111/io.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10779,16 +10779,11 @@ SphericalCSNNPtr PROJStringParser::Private::buildSphericalCS(
1077910779

1078010780
static double getNumericValue(const std::string &paramValue,
1078110781
bool *pHasError = nullptr) {
10782-
try {
10783-
double value = c_locale_stod(paramValue);
10784-
if (pHasError)
10785-
*pHasError = false;
10786-
return value;
10787-
} catch (const std::invalid_argument &) {
10788-
if (pHasError)
10789-
*pHasError = true;
10790-
return 0.0;
10791-
}
10782+
bool success;
10783+
double value = c_locale_stod(paramValue, success);
10784+
if (pHasError)
10785+
*pHasError = !success;
10786+
return value;
1079210787
}
1079310788

1079410789
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)