-
Notifications
You must be signed in to change notification settings - Fork 489
Critical: fix stack overflow from unbounded sprintf() #2410
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6556,7 +6556,7 @@ Datum age_tofloatlist(PG_FUNCTION_ARGS) | |||||
| int i; | ||||||
| bool is_valid = false; | ||||||
| float8 float_num; | ||||||
| char buffer[64]; | ||||||
| char buffer[400]; | ||||||
|
|
||||||
| /* check for null */ | ||||||
| if (PG_ARGISNULL(0)) | ||||||
|
|
@@ -6618,7 +6618,7 @@ Datum age_tofloatlist(PG_FUNCTION_ARGS) | |||||
|
|
||||||
| float_elem.type = AGTV_FLOAT; | ||||||
| float_num = elem->val.float_value; | ||||||
| sprintf(buffer, "%f", float_num); | ||||||
| snprintf(buffer, sizeof(buffer), "%f", float_num); | ||||||
| string = buffer; | ||||||
|
Comment on lines
+6621
to
6622
|
||||||
| float_elem.val.float_value = float8in_internal_null(string, NULL, "double precision", string, &is_valid); | ||||||
|
Comment on lines
6620
to
6623
|
||||||
| agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &float_elem); | ||||||
|
|
@@ -7574,7 +7574,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); | ||||||
|
|
||||||
|
|
@@ -7585,7 +7585,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); | ||||||
|
||||||
| snprintf(buffer, sizeof(buffer), "%ld", elem->val.int_value); | |
| snprintf(buffer, sizeof(buffer), INT64_FORMAT, elem->val.int_value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
char buffer[400];is a new magic number on the stack. Since the required size is derived fromDBL_MAX_10_EXPand the chosen formatting/precision, consider defining a named constant (or computing the bound) so it's clear why 400 is sufficient and future format changes don't accidentally reintroduce truncation/overflow risks.