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

Fixed some issues found by static analyzers #2634

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/install/dracut-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static char *get_real_file(const char *src, bool fullyresolve)
return NULL;
}

linksz = readlink(fullsrcpath, linktarget, sizeof(linktarget));
linksz = readlink(fullsrcpath, linktarget, sizeof(linktarget) - 1);
if (linksz < 0)
return NULL;
linktarget[linksz] = '\0';
Expand Down Expand Up @@ -1140,6 +1140,7 @@ static int parse_argv(int argc, char *argv[])
kerneldir = optarg;
break;
case ARG_FIRMWAREDIRS:
strv_free(firmwaredirs);
firmwaredirs = strv_split(optarg, ":");
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to the fix, but maybe add a check that strv_split() succeeded?

break;
case 'f':
Expand Down
34 changes: 26 additions & 8 deletions src/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ static int getarg(int argc, char **argv)
char *end_value = NULL;
bool bool_value = false;
char *cmdline = NULL;
char *cmdline_iter = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Merge this into the preceding line?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, the var doesn't have to be initialized.


char *p = getenv("CMDLINE");
if (p == NULL) {
usage(GETARG, EXIT_FAILURE, "CMDLINE env not set");
}
cmdline = strdup(p);

if (argc != 2) {
usage(GETARG, EXIT_FAILURE, "Number of arguments invalid");
Expand All @@ -204,9 +204,16 @@ static int getarg(int argc, char **argv)
if (strlen(search_key) == 0)
usage(GETARG, EXIT_FAILURE, "search key undefined");

cmdline = strdup(p);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I see no reason to copy p in the first place... The function could just use it directly, i.e., initialize cmdline (the iterator) simply as cmdline = p. Or have I overlooked something?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have... next_arg() modifies the string, so it must be copied. So scratch this.

if (cmdline == NULL) {
fprintf(stderr, "ERROR: out of memory.\n");
exit(EXIT_FAILURE);
}
cmdline_iter = cmdline;

do {
char *key = NULL, *value = NULL;
cmdline = next_arg(cmdline, &key, &value);
cmdline = next_arg(cmdline_iter, &key, &value);
Copy link
Contributor

Choose a reason for hiding this comment

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

cmdline_iter = ...

if (strcmp(key, search_key) == 0) {
if (value) {
end_value = value;
Expand All @@ -216,7 +223,9 @@ static int getarg(int argc, char **argv)
bool_value = true;
}
}
} while (cmdline[0]);
} while (cmdline_iter[0]);

free(cmdline);

if (search_value) {
if (end_value && strcmp(end_value, search_value) == 0) {
Expand Down Expand Up @@ -244,16 +253,15 @@ static int getargs(int argc, char **argv)
char *search_value;
bool found_value = false;
char *cmdline = NULL;
char *cmdline_iter = NULL;
Copy link
Contributor

@dtardon dtardon Mar 7, 2024

Choose a reason for hiding this comment

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

Merge this into the preceding line?


char *p = getenv("CMDLINE");
if (p == NULL) {
usage(GETARGS, EXIT_FAILURE, "CMDLINE env not set");
}
cmdline = strdup(p);

if (argc != 2) {
if (argc != 2)
usage(GETARGS, EXIT_FAILURE, "Number of arguments invalid");
}

search_key = argv[1];

Expand All @@ -268,9 +276,16 @@ static int getargs(int argc, char **argv)
if (strlen(search_key) == 0)
usage(GETARGS, EXIT_FAILURE, "search key undefined");

cmdline = strdup(p);
if (cmdline == NULL) {
fprintf(stderr, "ERROR: out of memory.\n");
exit(EXIT_FAILURE);
}
cmdline_iter = cmdline;

do {
char *key = NULL, *value = NULL;
cmdline = next_arg(cmdline, &key, &value);
cmdline = next_arg(cmdline_iter, &key, &value);
Copy link
Contributor

Choose a reason for hiding this comment

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

cmdline_iter = ...

if (strcmp(key, search_key) == 0) {
if (search_value) {
if (strcmp(value, search_value) == 0) {
Expand All @@ -286,7 +301,10 @@ static int getargs(int argc, char **argv)
found_value = true;
}
}
} while (cmdline[0]);
} while (cmdline_iter[0]);

free(cmdline_iter);
Copy link
Contributor

Choose a reason for hiding this comment

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

free(cmdline)


return found_value ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
Loading