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 printf funcs on memory pressure with floats #1275

Merged
merged 1 commit into from
Sep 1, 2024

Conversation

GabrielRavier
Copy link
Collaborator

@GabrielRavier GabrielRavier commented Sep 1, 2024

Cosmopolitan's printf-family functions currently crash if one tries to
format a floating point number with a large precision (large enough
that gdtoa tries to allocate memory to format the number) when under
memory pressure (i.e. when malloc fails), because gdtoa fails to check
if malloc fails.

The added tests (which would previously crash under cosmopolitan
without this patch) show how to reproduce the issue.

This patch fixes this, and adds the aforementioned tests.

Cosmopolitan's printf-family functions currently crash if one tries to
format a floating point number with a large precision (large enough
that gdtoa tries to allocate memory to format the number) when under
memory pressure (i.e. when malloc fails), because gdtoa fails to check
if malloc fails.

The added tests (which would previously crash under cosmopolitan
without this patch) show how to reproduce the issue.

This patch fixes this, and adds the aforementioned tests.
Copy link
Owner

@jart jart left a comment

Choose a reason for hiding this comment

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

Nice! Thank you!

if (limit.rlim_max > 1000000 || limit.rlim_max == RLIM_INFINITY) {
limit.rlim_max = 1000000;
limit.rlim_cur = limit.rlim_max;
ASSERT_GE(setrlimit(RLIMIT_AS, &limit), 0);
Copy link
Owner

Choose a reason for hiding this comment

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

You truly understand Cosmopolitan Libc if you're using this function.

@jart jart merged commit a089c07 into jart:master Sep 1, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants