Skip to content
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
20 changes: 20 additions & 0 deletions regress/expected/expr.out
Original file line number Diff line number Diff line change
Expand Up @@ -3548,6 +3548,26 @@ $$) AS (toFloatList agtype);
[1.20002]
(1 row)

-- large magnitudes must not overflow the conversion (regression: unbounded
-- sprintf into a fixed stack buffer overflowed for values like 1.0e308)
SELECT * FROM cypher('expr', $$
RETURN toFloatList([1.0e308, -1.0e308])
$$) AS (toFloatList agtype);
tofloatlist
-------------------
[1e+308, -1e+308]
(1 row)

-- precision must be preserved (regression: "%f" format truncated to 6 digits,
-- so 0.123456789 came back as 0.123457)
SELECT * FROM cypher('expr', $$
RETURN toFloatList([0.123456789])
$$) AS (toFloatList agtype);
tofloatlist
---------------
[0.123456789]
(1 row)

-- should return null
SELECT * FROM cypher('expr', $$
RETURN toFloatList(['true'])
Expand Down
10 changes: 10 additions & 0 deletions regress/sql/expr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,16 @@ $$) AS (toFloatList agtype);
SELECT * FROM cypher('expr', $$
RETURN toFloatList([1.20002])
$$) AS (toFloatList agtype);
-- large magnitudes must not overflow the conversion (regression: unbounded
-- sprintf into a fixed stack buffer overflowed for values like 1.0e308)
SELECT * FROM cypher('expr', $$
RETURN toFloatList([1.0e308, -1.0e308])
$$) AS (toFloatList agtype);
-- precision must be preserved (regression: "%f" format truncated to 6 digits,
-- so 0.123456789 came back as 0.123457)
SELECT * FROM cypher('expr', $$
RETURN toFloatList([0.123456789])
$$) AS (toFloatList agtype);
-- should return null
SELECT * FROM cypher('expr', $$
RETURN toFloatList(['true'])
Expand Down
19 changes: 11 additions & 8 deletions src/backend/utils/adt/agtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -7099,8 +7099,6 @@ Datum age_tofloatlist(PG_FUNCTION_ARGS)
int count;
int i;
bool is_valid = false;
float8 float_num;
char buffer[64];

/* check for null */
if (PG_ARGISNULL(0))
Expand Down Expand Up @@ -7160,11 +7158,16 @@ Datum age_tofloatlist(PG_FUNCTION_ARGS)

case AGTV_FLOAT:

/*
* The element is already a float8, so assign it directly. The
* previous approach formatted it to a string with sprintf() and
* re-parsed it: that both overflowed a fixed 64-byte stack buffer
* for large magnitudes (e.g. 1.0e308 needs ~317 chars) and lost
* precision, since "%f" truncates to 6 fractional digits. Direct
* assignment avoids both problems.
*/
float_elem.type = AGTV_FLOAT;
float_num = elem->val.float_value;
sprintf(buffer, "%f", float_num);
string = buffer;
float_elem.val.float_value = float8in_internal_null(string, NULL, "double precision", string, &is_valid);
float_elem.val.float_value = elem->val.float_value;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &float_elem);

break;
Expand Down Expand Up @@ -8146,7 +8149,7 @@ Datum age_tostringlist(PG_FUNCTION_ARGS)

case AGTV_FLOAT:

sprintf(buffer, "%.*g", DBL_DIG, elem->val.float_value);
snprintf(buffer, sizeof(buffer), "%.*g", DBL_DIG, elem->val.float_value);
string_elem.val.string.val = pstrdup(buffer);
string_elem.val.string.len = strlen(buffer);

Expand All @@ -8157,7 +8160,7 @@ Datum age_tostringlist(PG_FUNCTION_ARGS)

case AGTV_INTEGER:

sprintf(buffer, "%ld", elem->val.int_value);
snprintf(buffer, sizeof(buffer), "%ld", elem->val.int_value);
string_elem.val.string.val = pstrdup(buffer);
string_elem.val.string.len = strlen(buffer);

Expand Down
Loading