replace variables and ~ in script paths#351
Conversation
|
Well, it might be good here, to add tests, too. |
|
Yeah, tests are a good idea. |
Here they are. |
|
|
||
| wordexp_t replaced_path; | ||
| if (path && 0 == wordexp(path, &replaced_path, WRDE_NOCMD)){ | ||
| path = g_strdup(replaced_path.we_wordv[0]); |
There was a problem hiding this comment.
@tsipinakis I'm missing here a free(path);, right?
There was a problem hiding this comment.
Yeah you are missing a free, path from init_get_string is never freed.
| free(ptr); | ||
|
|
||
| path = ini_get_string(section, "expand_home", ""); | ||
| wordexp(path, &replaced_path, WRDE_NOCMD); |
There was a problem hiding this comment.
Since we know the contents of the expansion we should use the simplest method (read: getenv) to get the data we need to compile the test case. Using the exact same code the function you're testing is using is error prone and can miss quite a few bugs.
(hint: You can use the string_replace function from utils.c to do the actual replacement)
|
|
||
| path = ini_get_string(section, "expand_tilde", ""); | ||
| wordexp(path, &replaced_path, WRDE_NOCMD); | ||
| ASSERT_STR_EQ(replaced_path.we_wordv[0], (ptr = ini_get_path(section, "expand_tilde", ""))); |
There was a problem hiding this comment.
Same with this test, we should use the simplest method to setup the test case.
|
|
||
| wordexp_t replaced_path; | ||
| if (path && 0 == wordexp(path, &replaced_path, WRDE_NOCMD)){ | ||
| path = g_strdup(replaced_path.we_wordv[0]); |
There was a problem hiding this comment.
Yeah you are missing a free, path from init_get_string is never freed.
| char *path = ini_get_string(section, key, def); | ||
|
|
||
| wordexp_t replaced_path; | ||
| if (path && 0 == wordexp(path, &replaced_path, WRDE_NOCMD)){ |
There was a problem hiding this comment.
Some more error handling here would be nice, especially for WRDE_BADVAL and WRDE_CMDSUB.
|
|
||
| wordexp_t replaced_path; | ||
| if (path && 0 == wordexp(path, &replaced_path, WRDE_NOCMD)){ | ||
| path = g_strdup(replaced_path.we_wordv[0]); |
There was a problem hiding this comment.
You're also only getting the first word here, we_wordv is a string array (char **), not a char *. It's a common mistake so please also add a regression test for this.
|
Disclaimer: I'm currently not at my laptop for the next few days and can't test it. I read the wordexp manpage over and over again until I fully understood the Problems. I have to conclude, that this is the shittiest approach possible. Here is why:
Yes this is correct. So if there are multiple words, we have to strcat all Well, if someone sets `IFS=/', the returned path is broken. Even worse: IFS is unset, the path contains spaces. So how should we deterministically chain the words again together? Wordexp also does more stuff, I personally dislike. It also replaces other variables. IMO in our case only
What about using this in the actual |
246a5c2 to
4e873fe
Compare
Well, it turns out to be true in all cases.
So, I went for that. Here are the commits. Sorry for the spam on travis. In my previous commits, I failed using |
No worries, you're not a C programmer until you've wasted a few hours debugging weird memory issues :p It might be beneficial to also apply this to the |
Thanks for the hint. |
I moved the logics now to a function in The tests from |
|
LGTM 👍 Thank you for implementing this. |
wordexp replaces all variables and
~in the string. TBH I don't quite like the thing, that it replaces all variables, but there is no other way.Fixes #350