Skip to content

Commit

Permalink
asterisms: fix reading long integers
Browse files Browse the repository at this point in the history
  • Loading branch information
10110111 committed Feb 3, 2025
1 parent 9c4e61c commit 072df96
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/core/StelUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include <QRegularExpression>
#include <QProcess>
#include <QSysInfo>
#include <QJsonValue>
#include <cmath> // std::fmod
#include <limits>
#include <zlib.h>

#ifdef CYGWIN
Expand Down Expand Up @@ -2788,6 +2790,16 @@ QByteArray uncompress(QIODevice& device, qint64 maxBytes)
return out;
}

qint64 getLongLong(const QJsonValue& v)
{
if(v.isString()) return v.toString().toLongLong();
const auto value = v.toDouble();
constexpr qint64 max = (1LL<<std::numeric_limits<double>::digits) - 1;
if(std::abs(value) > max) return 0;
const auto integer = static_cast<qint64>(value);
if(value != integer) return 0; // fractional part must be zero
return integer;
}

} // end of the StelUtils namespace

12 changes: 12 additions & 0 deletions src/core/StelUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ namespace StelUtils
//! Return the user agent name, i.e. "Stellarium/0.15.0 (Linux)"
QString getUserAgentString();

/*! \brief Get a long integer from a JSON value
*
* QJsonValue stores JSON numbers as double-precision floating-point values. This means
* that numbers greater than 2^53-1 will be rounded, which is unacceptable for e.g. ids
* like those of the Gaia catalog.
* This function supports, in addition to normal JSON numbers, also representations of
* numbers by JSON strings. If the value passed is a JSON string, it will be parsed to a
* number. If it's a JSON number less than 2^53, it will be returned as is. Otherwise it
* will return 0, as QJsonValue's getters would in case of a type mismatch.
*/
qint64 getLongLong(const class QJsonValue& v);

inline const QString getEndLineChar() {
#ifdef Q_OS_WIN
const QString stelEndl="\r\n";
Expand Down
6 changes: 1 addition & 5 deletions src/core/modules/Asterism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ bool Asterism::read(const QJsonObject& data, StarMgr *starMgr)
<< lineIndex << ": isn't a number";
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const StarId HIP = point.toInteger();
#else
const StarId HIP = point.toInt(); // Won't support Gaia id unless it fits into 32 bits
#endif
const StarId HIP = StelUtils::getLongLong(point);
if (HIP <= NR_OF_HIP)
asterism.push_back(starMgr->searchHP(HIP));
else
Expand Down

0 comments on commit 072df96

Please sign in to comment.