Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dumping large floating numbers (fix #2367) #2661

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,11 @@ enum {
#define JVP_FLAGS_NUMBER_NATIVE_STR JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_NATIVE, 1))
#define JVP_FLAGS_NUMBER_LITERAL JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_DECIMAL, 1))

#define STR(x) #x
#define XSTR(x) STR(x)
#define DBL_MAX_STR XSTR(DBL_MAX)
#define DBL_MIN_STR "-" XSTR(DBL_MAX)

// the decimal precision of binary double
#define BIN64_DEC_PRECISION (17)
#define DEC_NUMBER_STRING_GUARD (14)

#include <jv_thread.h>
#include "jv_thread.h"
#ifdef WIN32
/* Copied from Heimdal: thread-specific keys; see lib/base/dll.c in Heimdal */

Expand Down Expand Up @@ -658,12 +653,12 @@ static const char* jvp_literal_number_literal(jv n) {
}

if (decNumberIsInfinite(pdec)) {
// For backward compatibility.
if (decNumberIsNegative(pdec)) {
return DBL_MIN_STR;
} else {
return DBL_MAX_STR;
}
// We cannot preserve the literal data of numbers outside the limited
// range of exponent. Since `decNumberToString` returns "Infinity"
// (or "-Infinity"), and to reduce stack allocations as possible, we
// normalize infinities in the callers instead of printing the maximum
// (or minimum) double here.
return NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By returning NULL here will we end up here https://github.com/jqlang/jq/blob/master/src/jv_print.c#L243-L252 and inf gets clamped to DBL_MAX?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

}

if (plit->literal_data == NULL) {
Expand Down
6 changes: 6 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ elif [ $? -ne 5 ]; then
exit 1
fi

# Regression test for #2367; make sure to call jq twice
if ! echo '{"a": 1E9999999999}' | $JQ . | $JQ -e .a; then
printf 'Issue #2367 is back?\n' 1>&2
exit 1
fi

# Regression test for #1534
echo "[1,2,3,4]" > $d/expected
printf "[1,2][3,4]" | $JQ -cs add > $d/out 2>&1
Expand Down