Skip to content

Commit

Permalink
alternatives: properly handle chars with const in normalize_path
Browse files Browse the repository at this point in the history
  • Loading branch information
lnykryn authored and jamacku committed May 14, 2024
1 parent fa155a9 commit 400e11f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static int usage(int rc) {
* Function to clean path form unnecessary backslashes
* It will make from //abcd///efgh/ -> /abcd/efgh/
*/
const char *normalize_path(const char *s) {
char *normalize_path(char *s) {
if (s) {
const char *src = s;
char *dst = (char *)s;
Expand All @@ -121,7 +121,12 @@ const char *normalize_path(const char *s) {
dst++;
}
}
return (const char *)s;
return s;
}

char *normalize_path_alloc(const char *s) {
char *ret = strdup(s);
return normalize_path(ret);
}

int streq(const char *a, const char *b) {
Expand Down Expand Up @@ -197,7 +202,7 @@ static void setupDoubleArg(enum programModes *mode, const char ***nextArgPtr,

if (!*nextArg)
usage(2);
*target = strdup(normalize_path(*nextArg));
*target = normalize_path_alloc(*nextArg);
*nextArgPtr = nextArg + 1;
}

Expand All @@ -218,7 +223,7 @@ static void setupTripleArg(enum programModes *mode, const char ***nextArgPtr,

if (!*nextArg)
usage(2);
*target = strdup(normalize_path(*nextArg));
*target = normalize_path_alloc(*nextArg);
nextArg++;

if (!*nextArg)
Expand All @@ -232,7 +237,7 @@ static void setupLinkSet(struct linkSet *set, const char ***nextArgPtr) {

if (!*nextArg || **nextArg != '/')
usage(2);
set->facility = strdup(normalize_path(*nextArg));
set->facility = normalize_path_alloc(*nextArg);
nextArg++;

if (!*nextArg || **nextArg == '/')
Expand All @@ -242,7 +247,7 @@ static void setupLinkSet(struct linkSet *set, const char ***nextArgPtr) {

if (!*nextArg || **nextArg != '/')
usage(2);
set->target = strdup(normalize_path(*nextArg));
set->target = normalize_path_alloc(*nextArg);
*nextArgPtr = nextArg + 1;
}

Expand Down Expand Up @@ -391,7 +396,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
goto finish;
}

set->alts[set->numAlts].leader.facility = strdup(normalize_path(groups[0].facility));
set->alts[set->numAlts].leader.facility = normalize_path_alloc(groups[0].facility);
set->alts[set->numAlts].leader.title = strdup(groups[0].title);
set->alts[set->numAlts].leader.target = strsteal(&line);
set->alts[set->numAlts].numFollowers = numGroups - 1;
Expand Down Expand Up @@ -455,7 +460,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
set->alts[set->numAlts].followers[i - 1].title =
strdup(groups[i].title);
set->alts[set->numAlts].followers[i - 1].facility =
strdup(normalize_path(groups[i].facility));
normalize_path_alloc(groups[i].facility);
set->alts[set->numAlts].followers[i - 1].target =
(line && strlen(line)) ? strsteal(&line) : NULL;
}
Expand Down Expand Up @@ -505,7 +510,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
set->current = i;
}

set->currentLink = strdup(normalize_path(linkBuf));
set->currentLink = normalize_path_alloc(linkBuf);
finish:
for (i = 1; i < numGroups; i++) {
free(groups[i].title);
Expand Down Expand Up @@ -1400,13 +1405,13 @@ int main(int argc, const char **argv) {
nextArg++;
if (!*nextArg)
usage(2);
altDir = strdup(normalize_path(*nextArg));
altDir = normalize_path_alloc(*nextArg);
nextArg++;
} else if (!strcmp(*nextArg, "--admindir")) {
nextArg++;
if (!*nextArg)
usage(2);
stateDir = strdup(normalize_path(*nextArg));
stateDir = normalize_path_alloc(*nextArg);
nextArg++;
} else if (!strcmp(*nextArg, "--list")) {
if (mode != MODE_UNKNOWN)
Expand Down

0 comments on commit 400e11f

Please sign in to comment.