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

Refactor: handle in-output errors for string functions (Part 1) #11854

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anandamideShakyan
Copy link

@anandamideShakyan anandamideShakyan commented Dec 13, 2024

PR Description:

Detect and address input/output errors that can result in undefined behavior. Ensure proper error handling for I/O functions that may fail and leave variables uninitialized. Neglecting to check the status of these functions before using their outputs (e.g., memory buffers, file descriptors, etc.) can lead to undefined program behavior. This update enforces checks on commonly used I/O functions to validate their return values and prevent improper usage.

This is first of the several changes for this refactoring.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 13, 2024
Copy link

netlify bot commented Dec 13, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 557f739
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/67611ac200ac520008096554

Copy link
Contributor

@Yuhta Yuhta left a comment

Choose a reason for hiding this comment

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

This is for debugging purpose, but it is perf sensitive, so should not throw or print. And in the case it is truncated, snprintf already put 0 at end so no need to do it ourselves.

int result = snprintf(entry.label, entry.kLabelCapacity, "%s", label_.c_str());

if (result < 0) {
throw std::runtime_error("Encoding error in snprintf");
Copy link
Collaborator

Choose a reason for hiding this comment

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

On error I suggest to set the first byte to null byte. That's all that is needed to ensure the buffer doesn't contains anything bad. I think the buffer itself is not initialized prior so could contain random data which is overwritten or on error we set the null byte at the beginning. This should take care of errors and use of an uninitialized buffer.

Copy link
Contributor

@Yuhta Yuhta Dec 13, 2024

Choose a reason for hiding this comment

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

Yes we can do that. This virtually never happens though so we should wrap the condition inside FOLLY_UNLIKELY so CPU don't waste time decoding the unused instructions.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants