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(dracut-util): avoid memory leak #2608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 32 additions & 16 deletions src/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

// Parts are copied from the linux kernel

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

// CODE FROM LINUX KERNEL START

Expand Down Expand Up @@ -56,6 +57,13 @@
return (char *)str;
}

#define _cleanup_(x) __attribute__((cleanup(x)))
static inline void freep(void *p)
Dismissed Show dismissed Hide dismissed
{
free(*(void **)p);
}
#define _cleanup_free_ _cleanup_(freep)

/*
* Parse a string to get a param value pair.
* You can use " around spaces, but can't escape ".
Expand Down Expand Up @@ -179,17 +187,20 @@
char *search_value;
char *end_value = NULL;
bool bool_value = false;
char *cmdline = NULL;
_cleanup_free_ char *cmdline = NULL;
char *args = NULL;

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

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");
}
if (!cmdline)
return -ENOMEM;

search_key = argv[1];

Expand All @@ -204,9 +215,10 @@
if (strlen(search_key) == 0)
usage(GETARG, EXIT_FAILURE, "search key undefined");

args = cmdline;
do {
char *key = NULL, *value = NULL;
cmdline = next_arg(cmdline, &key, &value);
args = next_arg(args, &key, &value);
if (strcmp(key, search_key) == 0) {
if (value) {
end_value = value;
Expand All @@ -216,7 +228,7 @@
bool_value = true;
}
}
} while (cmdline[0]);
} while (args[0]);

if (search_value) {
if (end_value && strcmp(end_value, search_value) == 0) {
Expand All @@ -243,17 +255,20 @@
char *search_key;
char *search_value;
bool found_value = false;
char *cmdline = NULL;
_cleanup_free_ char *cmdline = NULL;
char *args = NULL;

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

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

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

search_key = argv[1];

Expand All @@ -268,9 +283,10 @@
if (strlen(search_key) == 0)
usage(GETARGS, EXIT_FAILURE, "search key undefined");

args = cmdline;
do {
char *key = NULL, *value = NULL;
cmdline = next_arg(cmdline, &key, &value);
args = next_arg(args, &key, &value);
if (strcmp(key, search_key) == 0) {
if (search_value) {
if (strcmp(value, search_value) == 0) {
Expand All @@ -286,7 +302,7 @@
found_value = true;
}
}
} while (cmdline[0]);
} while (args[0]);
return found_value ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
Loading