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 Segmentation fault if HOME not set #162

Merged
merged 1 commit into from
Sep 1, 2024
Merged

Conversation

EyitopeIO
Copy link
Contributor

@EyitopeIO EyitopeIO commented Aug 30, 2024

Fixes #154

@EyitopeIO
Copy link
Contributor Author

EyitopeIO commented Aug 30, 2024

If the user-specified cache directory had other content the user cared about, then clearing the cache would wipe that directory along with the non-httpdirfs content.

Perhaps the help should be updated with this comment?

@fangfufu
Copy link
Owner

Yeh, your previous pull request successfully address the stated GitHub issue, so I merged it. If you cherry-pick this on top of the current master, update the pull request saying that it fixes #154, I will merge it if it is up to standard like the previous one.

src/cache.c Outdated Show resolved Hide resolved
@fangfufu
Copy link
Owner

You need to rebase your changes on top the head of the master branch. There are merge conflicts. The quickest way to resolve it is to rebase. I was happy with your previous commit, that's why I merged it. :)

@EyitopeIOn
Copy link

EyitopeIOn commented Aug 30, 2024

Commenting as @EyitopeIO

Good to know. Thanks.

I'm testing with $HOME non-existent, but got a crash. I'd do my best to keep the standard up and request a re-review as soon as it's ready.

@EyitopeIOn
Copy link

EyitopeIOn commented Aug 30, 2024

I think I don't have permissions to edit the PR title because I don't see the edit button.
I see you may be able to change the PR title at the point of merging.

@fangfufu fangfufu changed the title Add option to clear cache Fix Segmentation fault if HOME not set Aug 30, 2024
src/cache.c Outdated Show resolved Hide resolved
src/cache.h Outdated Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
src/cache.c Outdated Show resolved Hide resolved
src/cache.c Outdated Show resolved Hide resolved
src/cache.c Outdated Show resolved Hide resolved
src/cache.c Outdated Show resolved Hide resolved
src/cache.c Outdated Show resolved Hide resolved
src/cache.c Outdated
home = path_append(user_home, xdg_default);
}
} else {
home = path_append(xdg_home, xdg_default);
Copy link
Owner

Choose a reason for hiding this comment

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

Is this what we really want to do? When xdg_home == "XDG_CACHE_HOME"

src/main.c Show resolved Hide resolved
@fangfufu
Copy link
Owner

In general you need to take more care about your variable names, I think.

src/main.c Outdated
lprintf(warning, "$HOME is unset\n");
char *cur_dir = realpath("./", NULL);
if (!cur_dir) {
lprintf(fatal, "Could not get config directory\n");
Copy link
Owner

Choose a reason for hiding this comment

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

This should not be fatal, if the software cannot get the config directory, it should keep running. This should just be a warning.

src/cache.c Outdated
/*
* "XDG_CACHE_HOME/HOME/.cache" > "HOME/.cache" > "./.cache"
*/
char *xdg = getenv("XDG_CACHE_HOME");
Copy link
Owner

Choose a reason for hiding this comment

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

rename xdg to xdg_cache_home.

src/main.c Outdated
{
const char *default_config_subdir = "/.config";
char *config_dir;
char *xdg = getenv("XDG_CONFIG_HOME");
Copy link
Owner

Choose a reason for hiding this comment

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

rename this xdg to xdg_config_home.

src/cache.c Outdated
Comment on lines 47 to 49
/*
* "XDG_CACHE_HOME/HOME/.cache" > "HOME/.cache" > "./.cache"
*/
Copy link
Owner

Choose a reason for hiding this comment

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

I am not sure if the comment reflects what's actually happening. The code is clear enough describing what's happening. Perhaps delete this?

src/cache.c Outdated
* "XDG_CACHE_HOME/HOME/.cache" > "HOME/.cache" > "./.cache"
*/
char *xdg = getenv("XDG_CACHE_HOME");
if (!xdg) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would invert the this condition to make the whole thing clearer.

src/main.c Outdated
const char *default_config_subdir = "/.config";
char *config_dir;
char *xdg = getenv("XDG_CONFIG_HOME");
if (!xdg) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would invert the this condition to make the whole thing clearer.

src/cache.c Outdated
char *xdg = getenv("XDG_CACHE_HOME");
if (!xdg) {
char *user_home = getenv("HOME");
if (!user_home) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would invert this condition too.

src/main.c Outdated
char *xdg = getenv("XDG_CONFIG_HOME");
if (!xdg) {
char *user_home = getenv("HOME");
if (!user_home) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would invert this condition.

@fangfufu
Copy link
Owner

fangfufu commented Aug 31, 2024

Please address all the code quality issues introduced by your change flagged up by the static analysers.

Also, in general, when you have a if statement, you should condition them in such a way that the most common condition occurs at the first branch, so the code is easier to read. This is what I want in this repository for new contributions.

For example, I prefer:

if (common_condition) {
// handle common condition
} else {
// handle unexpected condition
}

I don't like:

if (!common_condition) {
// handle unexpected condition
} else {
//deal with common condition
}

Anyways, this pull request is heading towards a good direction. I like it so far.

@EyitopeIO
Copy link
Contributor Author

EyitopeIO commented Sep 1, 2024

Comments on static analyser

  • Issues about realpath function are false-positives because the function is already called with a destination buffer set to NULL.
  • Issue about the $HOME variable being modified by an attacker is not applicable. We assume the user's system is already secure, so whatever is in$HOME is legit.

Copy link

sonarcloud bot commented Sep 1, 2024

@fangfufu fangfufu merged commit 09ebc82 into fangfufu:master Sep 1, 2024
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segmentation fault if HOME not set
3 participants