-
Notifications
You must be signed in to change notification settings - Fork 388
libcrun: check setenv failure #2007
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: main
Are you sure you want to change the base?
Conversation
Closes: containers#1998 Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
Summary of ChangesHello @eriksjolund, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request adds necessary error checking for setenv calls, which improves the robustness of the code. The changes in src/libcrun/container.c are correct. However, the modifications in src/libcrun/utils.c to the set_home_env function introduce a bug in the error handling logic. My review includes a comment detailing this issue.
| if (UNLIKELY(ret < 0)) | ||
| goto error; | ||
| else | ||
| return 0; |
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.
The else statement is redundant and can be removed for clarity.
More critically, the reuse of the ret variable introduces a bug. The set_home_env function now incorrectly returns 0 (success) when a user is not found in /etc/passwd. This happens because fgetpwent_r sets ret to 0 on success, and if the user isn't found, the function proceeds to the error label where ret ? -errno : 0 evaluates to 0.
The original logic of returning -1 from the error label was correct for all failure cases, including 'user not found'. The new logic return ret ? -errno : 0; is flawed and should be reconsidered.
if (UNLIKELY(ret < 0))
goto error;
return 0;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.
this should be addressed, ret is used after the error label.
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.
Do you know why there is a ternary operator on line 1486 in the current main branch?
It seems ret could never be 0
Here is code from the current main branch.
Lines 1450 to 1487 in 0bc3050
| int ret = -1; | |
| buf_size = sysconf (_SC_GETPW_R_SIZE_MAX); | |
| if (buf_size < 0) | |
| buf_size = 1024; | |
| buf = xmalloc (buf_size); | |
| stream = fopen ("/etc/passwd", "re"); | |
| if (stream == NULL) | |
| goto error; | |
| for (;;) | |
| { | |
| struct passwd *ret_pw = NULL; | |
| ret = fgetpwent_r (stream, &pwd, buf, buf_size, &ret_pw); | |
| if (UNLIKELY (ret != 0)) | |
| { | |
| if (errno != ERANGE) | |
| goto error; | |
| buf_size *= 2; | |
| buf = xrealloc (buf, buf_size); | |
| continue; | |
| } | |
| if (ret_pw && ret_pw->pw_uid == id) | |
| { | |
| setenv ("HOME", ret_pw->pw_dir, 1); | |
| return 0; | |
| } | |
| } | |
| error: | |
| /* Let callers handle the error if the user was not found. */ | |
| return ret ? -errno : 0; | |
| } |
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.
I tried another approach in
3d23abe
If you like it, I could switch to that.
(Untested code. I haven't run clang-format yet)
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.
@eriksjolund I like the approach of not reusing ret for setenv checks.
Yet in 3d23abe you also refactor the set_home_env, which seems kind of out of scope for this PR. If you want to refactor it, please do so in a separate commit (so you can explain what's going on). Currently, your commit description says "check setenv failure" but you also do some other things, which is confusing.
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.
@kolyshkin yes, I will need to create separate commits and PR.
To keep better order I created
giuseppe
left a comment
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.
nit: could you wrap the checks into UNLIKELY?
You'll need to run make clang-format before committing
| if (UNLIKELY(ret < 0)) | ||
| goto error; | ||
| else | ||
| return 0; |
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.
this should be addressed, ret is used after the error label.
Closes: #1998