Discussion:
[PATCH 0/9] use cleanup attribute + misc patches
Lucas De Marchi
2013-11-18 14:04:18 UTC
Permalink
With this patch set libkmod is now using attribute cleanup. It's working here
for me, but please give it a look.

There are also some minor improvements, such as adding OOM checks.

Lucas De Marchi (9):
testsuite: Move test-alias to test-util
util: Add cleanup attribute
config: Use _cleanup_free_
util: use _cleanup_free_ on path_make_absolute_cwd()
testsuite: add basic test for getline_wrapped
util: Be OOM-safe and use _cleanup_free_
array: avoid duplicate code to reallocate
file: use _cleanup_free_
module: use _cleanup_free and remove useless call to free()

Makefile.am | 6 +-
libkmod/libkmod-array.c | 38 ++++----
libkmod/libkmod-config.c | 62 +++++-------
libkmod/libkmod-file.c | 8 +-
libkmod/libkmod-module.c | 14 +--
libkmod/libkmod-util.c | 52 ++++++----
libkmod/libkmod-util.h | 7 ++
libkmod/macro.h | 1 +
testsuite/.gitignore | 6 +-
testsuite/rootfs-pristine/test-alias/correct.txt | 25 -----
.../rootfs-pristine/test-util/alias-correct.txt | 25 +++++
.../test-util/getline_wrapped-correct.txt | 6 ++
.../test-util/getline_wrapped-input.txt | 6 ++
testsuite/test-alias.c | 75 ---------------
testsuite/test-util.c | 106 +++++++++++++++++++++
15 files changed, 238 insertions(+), 199 deletions(-)
delete mode 100644 testsuite/rootfs-pristine/test-alias/correct.txt
create mode 100644 testsuite/rootfs-pristine/test-util/alias-correct.txt
create mode 100644 testsuite/rootfs-pristine/test-util/getline_wrapped-correct.txt
create mode 100644 testsuite/rootfs-pristine/test-util/getline_wrapped-input.txt
delete mode 100644 testsuite/test-alias.c
create mode 100644 testsuite/test-util.c
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:22 UTC
Permalink
---
libkmod/libkmod-util.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c
index d686250..837f417 100644
--- a/libkmod/libkmod-util.c
+++ b/libkmod/libkmod-util.c
@@ -283,15 +283,15 @@ bool path_is_absolute(const char *p)

char *path_make_absolute_cwd(const char *p)
{
- char *cwd, *r;
- size_t plen;
- size_t cwdlen;
+ _cleanup_free_ char *cwd = NULL;
+ size_t plen, cwdlen;
+ char *r;

if (path_is_absolute(p))
return strdup(p);

cwd = get_current_dir_name();
- if (cwd == NULL)
+ if (!cwd)
return NULL;

plen = strlen(p);
@@ -299,11 +299,10 @@ char *path_make_absolute_cwd(const char *p)

/* cwd + '/' + p + '\0' */
r = realloc(cwd, cwdlen + 1 + plen + 1);
- if (r == NULL) {
- free(cwd);
+ if (r == NULL)
return NULL;
- }

+ cwd = NULL;
r[cwdlen] = '/';
memcpy(&r[cwdlen + 1], p, plen + 1);
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:23 UTC
Permalink
---
.../test-util/getline_wrapped-correct.txt | 6 +++++
.../test-util/getline_wrapped-input.txt | 6 +++++
testsuite/test-util.c | 31 ++++++++++++++++++++++
3 files changed, 43 insertions(+)
create mode 100644 testsuite/rootfs-pristine/test-util/getline_wrapped-correct.txt
create mode 100644 testsuite/rootfs-pristine/test-util/getline_wrapped-input.txt

diff --git a/testsuite/rootfs-pristine/test-util/getline_wrapped-correct.txt b/testsuite/rootfs-pristine/test-util/getline_wrapped-correct.txt
new file mode 100644
index 0000000..87344ab
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-util/getline_wrapped-correct.txt
@@ -0,0 +1,6 @@
+this is the first line wrapped by one \
+2
+this is a single line
+1
+three line lines in a row
+3
diff --git a/testsuite/rootfs-pristine/test-util/getline_wrapped-input.txt b/testsuite/rootfs-pristine/test-util/getline_wrapped-input.txt
new file mode 100644
index 0000000..f84a852
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-util/getline_wrapped-input.txt
@@ -0,0 +1,6 @@
+this is the first line \
+wrapped by one \\
+this is a single line
+three line \
+lines \
+in a row
diff --git a/testsuite/test-util.c b/testsuite/test-util.c
index db9f134..4fedb24 100644
--- a/testsuite/test-util.c
+++ b/testsuite/test-util.c
@@ -67,8 +67,39 @@ static DEFINE_TEST(alias_1,
.out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
});

+static int test_getline_wrapped(const struct test *t)
+{
+ FILE *fp = fopen("/getline_wrapped-input.txt", "re");
+
+ if (!fp)
+ return EXIT_FAILURE;
+
+ while (!feof(fp) && !ferror(fp)) {
+ unsigned int num = 0;
+ char *s = getline_wrapped(fp, &num);
+ if (!s)
+ break;
+ puts(s);
+ free(s);
+ printf("%u\n", num);
+ }
+
+ fclose(fp);
+ return EXIT_SUCCESS;
+}
+static DEFINE_TEST(test_getline_wrapped,
+ .description = "check if getline_wrapped() does the right thing",
+ .config = {
+ [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
+ },
+ .need_spawn = true,
+ .output = {
+ .out = TESTSUITE_ROOTFS "test-util/getline_wrapped-correct.txt",
+ });
+
static const struct test *tests[] = {
&salias_1,
+ &stest_getline_wrapped,
NULL,
};
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:25 UTC
Permalink
---
libkmod/libkmod-array.c | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/libkmod/libkmod-array.c b/libkmod/libkmod-array.c
index 8417f9a..1082deb 100644
--- a/libkmod/libkmod-array.c
+++ b/libkmod/libkmod-array.c
@@ -28,6 +28,17 @@

/* basic pointer array growing in steps */

+
+static int array_realloc(struct array *array, size_t new_total)
+{
+ void *tmp = realloc(array->array, sizeof(void *) * new_total);
+ if (tmp == NULL)
+ return -ENOMEM;
+ array->array = tmp;
+ array->total = new_total;
+ return 0;
+}
+
void array_init(struct array *array, size_t step)
{
assert(step > 0);
@@ -42,13 +53,9 @@ int array_append(struct array *array, const void *element)
size_t idx;

if (array->count + 1 >= array->total) {
- size_t new_total = array->total + array->step;
- void *tmp = realloc(array->array, sizeof(void *) * new_total);
- assert(array->step > 0);
- if (tmp == NULL)
- return -ENOMEM;
- array->array = tmp;
- array->total = new_total;
+ int r = array_realloc(array, array->total + array->step);
+ if (r < 0)
+ return r;
}
idx = array->count;
array->array[idx] = (void *)element;
@@ -69,13 +76,9 @@ int array_append_unique(struct array *array, const void *element)
void array_pop(struct array *array) {
array->count--;
if (array->count + array->step < array->total) {
- size_t new_total = array->total - array->step;
- void *tmp = realloc(array->array, sizeof(void *) * new_total);
- assert(array->step > 0);
- if (tmp == NULL)
+ int r = array_realloc(array, array->total - array->step);
+ if (r < 0)
return;
- array->array = tmp;
- array->total = new_total;
}
}

@@ -102,13 +105,10 @@ int array_remove_at(struct array *array, unsigned int pos)
sizeof(void *) * (array->count - pos));

if (array->count + array->step < array->total) {
- size_t new_total = array->total - array->step;
- void *tmp = realloc(array->array, sizeof(void *) * new_total);
- assert(array->step > 0);
- if (tmp == NULL)
+ int r = array_realloc(array, array->total - array->step);
+ /* ignore error */
+ if (r < 0)
return 0;
- array->array = tmp;
- array->total = new_total;
}

return 0;
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:26 UTC
Permalink
---
libkmod/libkmod-file.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
index 390f018..feb4a15 100644
--- a/libkmod/libkmod-file.c
+++ b/libkmod/libkmod-file.c
@@ -175,13 +175,12 @@ static int load_zlib(struct kmod_file *file)
{
int err = 0;
off_t did = 0, total = 0;
- unsigned char *p = NULL;
+ _cleanup_free_ unsigned char *p = NULL;

errno = 0;
file->gzf = gzdopen(file->fd, "rb");
- if (file->gzf == NULL) {
+ if (file->gzf == NULL)
return -errno;
- }
file->fd = -1; /* now owned by gzf due gzdopen() */

for (;;) {
@@ -215,9 +214,10 @@ static int load_zlib(struct kmod_file *file)

file->memory = p;
file->size = did;
+ p = NULL;
return 0;
+
error:
- free(p);
gzclose(file->gzf);
return err;
}
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:24 UTC
Permalink
---
libkmod/libkmod-util.c | 39 +++++++++++++++++++++++++--------------
1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c
index 837f417..df12433 100644
--- a/libkmod/libkmod-util.c
+++ b/libkmod/libkmod-util.c
@@ -46,8 +46,8 @@
char *getline_wrapped(FILE *fp, unsigned int *linenum)
{
int size = 256;
- int i = 0;
- char *buf = malloc(size);
+ int i = 0, n = 0;
+ _cleanup_free_ char *buf = malloc(size);

if (buf == NULL)
return NULL;
@@ -57,26 +57,33 @@ char *getline_wrapped(FILE *fp, unsigned int *linenum)

switch(ch) {
case EOF:
- if (i == 0) {
- free(buf);
+ if (i == 0)
return NULL;
- }
/* else fall through */

case '\n':
- if (linenum)
- (*linenum)++;
- if (i == size)
- buf = realloc(buf, size + 1);
- buf[i] = '\0';
- return buf;
+ n++;
+
+ {
+ char *ret;
+ if (i == size) {
+ ret = realloc(buf, size + 1);
+ if (!ret)
+ return NULL;
+ } else
+ ret = buf;
+ ret[i] = '\0';
+ buf = NULL;
+ if (linenum)
+ *linenum += n;
+ return ret;
+ }

case '\\':
ch = getc_unlocked(fp);

if (ch == '\n') {
- if (linenum)
- (*linenum)++;
+ n++;
continue;
}
/* else fall through */
@@ -85,8 +92,12 @@ char *getline_wrapped(FILE *fp, unsigned int *linenum)
buf[i++] = ch;

if (i == size) {
+ char *tmp;
size *= 2;
- buf = realloc(buf, size);
+ tmp = realloc(buf, size);
+ if (!tmp)
+ return NULL;
+ buf = tmp;
}
}
}
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:19 UTC
Permalink
Move file so we can use the same file to test other functions from
libkmod-util.c
---
Makefile.am | 6 +-
testsuite/.gitignore | 6 +-
testsuite/rootfs-pristine/test-alias/correct.txt | 25 --------
.../rootfs-pristine/test-util/alias-correct.txt | 25 ++++++++
testsuite/test-alias.c | 75 ----------------------
testsuite/test-util.c | 75 ++++++++++++++++++++++
6 files changed, 106 insertions(+), 106 deletions(-)
delete mode 100644 testsuite/rootfs-pristine/test-alias/correct.txt
create mode 100644 testsuite/rootfs-pristine/test-util/alias-correct.txt
delete mode 100644 testsuite/test-alias.c
create mode 100644 testsuite/test-util.c

diff --git a/Makefile.am b/Makefile.am
index 1c1e5f7..bdf758e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -176,7 +176,7 @@ testsuite_libtestsuite_la_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_libtestsuite_la_LIBADD = -lrt

TESTSUITE = testsuite/test-init testsuite/test-testsuite testsuite/test-loaded \
- testsuite/test-modinfo testsuite/test-alias testsuite/test-new-module \
+ testsuite/test-modinfo testsuite/test-util testsuite/test-new-module \
testsuite/test-modprobe testsuite/test-blacklist \
testsuite/test-dependencies testsuite/test-depmod

@@ -191,8 +191,8 @@ testsuite_test_loaded_LDADD = $(TESTSUITE_LDADD)
testsuite_test_loaded_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_test_modinfo_LDADD = $(TESTSUITE_LDADD)
testsuite_test_modinfo_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
-testsuite_test_alias_LDADD = $(TESTSUITE_LDADD)
-testsuite_test_alias_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
+testsuite_test_util_LDADD = $(TESTSUITE_LDADD)
+testsuite_test_util_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_test_new_module_LDADD = $(TESTSUITE_LDADD)
testsuite_test_new_module_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
testsuite_test_modprobe_LDADD = $(TESTSUITE_LDADD)
diff --git a/testsuite/.gitignore b/testsuite/.gitignore
index 71f34cc..bd26a40 100644
--- a/testsuite/.gitignore
+++ b/testsuite/.gitignore
@@ -2,7 +2,7 @@
*.la
*.so
/.dirstamp
-/test-alias
+/test-util
/test-blacklist
/test-dependencies
/test-depmod
@@ -14,8 +14,8 @@
/test-modprobe
/rootfs
/stamp-rootfs
-/test-alias.log
-/test-alias.trs
+/test-util.log
+/test-util.trs
/test-blacklist.log
/test-blacklist.trs
/test-dependencies.log
diff --git a/testsuite/rootfs-pristine/test-alias/correct.txt b/testsuite/rootfs-pristine/test-alias/correct.txt
deleted file mode 100644
index 86d0304..0000000
--- a/testsuite/rootfs-pristine/test-alias/correct.txt
+++ /dev/null
@@ -1,25 +0,0 @@
-input test1234
-return 0
-len 8
-output test1234
-
-input test[abcfoobar]2211
-return 0
-len 19
-output test[abcfoobar]2211
-
-input bar[aaa][bbbb]sss
-return 0
-len 17
-output bar[aaa][bbbb]sss
-
-input kmod[p.b]lib
-return 0
-len 12
-output kmod[p.b]lib
-
-input [az]1234[AZ]
-return 0
-len 12
-output [az]1234[AZ]
-
diff --git a/testsuite/rootfs-pristine/test-util/alias-correct.txt b/testsuite/rootfs-pristine/test-util/alias-correct.txt
new file mode 100644
index 0000000..86d0304
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-util/alias-correct.txt
@@ -0,0 +1,25 @@
+input test1234
+return 0
+len 8
+output test1234
+
+input test[abcfoobar]2211
+return 0
+len 19
+output test[abcfoobar]2211
+
+input bar[aaa][bbbb]sss
+return 0
+len 17
+output bar[aaa][bbbb]sss
+
+input kmod[p.b]lib
+return 0
+len 12
+output kmod[p.b]lib
+
+input [az]1234[AZ]
+return 0
+len 12
+output [az]1234[AZ]
+
diff --git a/testsuite/test-alias.c b/testsuite/test-alias.c
deleted file mode 100644
index 4d1d8d3..0000000
--- a/testsuite/test-alias.c
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2012-2013 ProFUSION embedded systems
- * Copyright (C) 2012 Pedro Pedruzzi
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <libkmod.h>
-
-#include "libkmod-util.h"
-#include "testsuite.h"
-
-static int alias_1(const struct test *t)
-{
- static const char *input[] = {
- "test1234",
- "test[abcfoobar]2211",
- "bar[aaa][bbbb]sss",
- "kmod[p.b]lib",
- "[az]1234[AZ]",
- NULL,
- };
-
- char buf[PATH_MAX];
- size_t len;
- const char **alias;
-
- for (alias = input; *alias != NULL; alias++) {
- int ret;
-
- ret = alias_normalize(*alias, buf, &len);
- printf("input %s\n", *alias);
- printf("return %d\n", ret);
-
- if (ret == 0) {
- printf("len %zu\n", len);
- printf("output %s\n", buf);
- }
-
- printf("\n");
- }
-
- return EXIT_SUCCESS;
-}
-static DEFINE_TEST(alias_1,
- .description = "check if alias_normalize does the right thing",
- .config = {
- [TC_ROOTFS] = TESTSUITE_ROOTFS "test-alias/",
- },
- .need_spawn = true,
- .output = {
- .out = TESTSUITE_ROOTFS "test-alias/correct.txt",
- });
-
-static const struct test *tests[] = {
- &salias_1,
- NULL,
-};
-
-TESTSUITE_MAIN(tests);
diff --git a/testsuite/test-util.c b/testsuite/test-util.c
new file mode 100644
index 0000000..db9f134
--- /dev/null
+++ b/testsuite/test-util.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2012-2013 ProFUSION embedded systems
+ * Copyright (C) 2012 Pedro Pedruzzi
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libkmod.h>
+
+#include "libkmod-util.h"
+#include "testsuite.h"
+
+static int alias_1(const struct test *t)
+{
+ static const char *input[] = {
+ "test1234",
+ "test[abcfoobar]2211",
+ "bar[aaa][bbbb]sss",
+ "kmod[p.b]lib",
+ "[az]1234[AZ]",
+ NULL,
+ };
+
+ char buf[PATH_MAX];
+ size_t len;
+ const char **alias;
+
+ for (alias = input; *alias != NULL; alias++) {
+ int ret;
+
+ ret = alias_normalize(*alias, buf, &len);
+ printf("input %s\n", *alias);
+ printf("return %d\n", ret);
+
+ if (ret == 0) {
+ printf("len %zu\n", len);
+ printf("output %s\n", buf);
+ }
+
+ printf("\n");
+ }
+
+ return EXIT_SUCCESS;
+}
+static DEFINE_TEST(alias_1,
+ .description = "check if alias_normalize does the right thing",
+ .config = {
+ [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
+ },
+ .need_spawn = true,
+ .output = {
+ .out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
+ });
+
+static const struct test *tests[] = {
+ &salias_1,
+ NULL,
+};
+
+TESTSUITE_MAIN(tests);
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:21 UTC
Permalink
---
libkmod/libkmod-config.c | 62 +++++++++++++++++-------------------------------
1 file changed, 22 insertions(+), 40 deletions(-)

diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c
index c5f4803..32adb8b 100644
--- a/libkmod/libkmod-config.c
+++ b/libkmod/libkmod-config.c
@@ -2,6 +2,7 @@
* libkmod - interface to kernel module operations
*
* Copyright (C) 2011-2013 ProFUSION embedded systems
+ * Copyright (C) 2013 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -145,7 +146,7 @@ static int kmod_config_add_command(struct kmod_config *config,
const char *command_name,
struct kmod_list **list)
{
- struct kmod_command *cmd;
+ _cleanup_free_ struct kmod_command *cmd;
struct kmod_list *l;
size_t modnamelen = strlen(modname) + 1;
size_t commandlen = strlen(command) + 1;
@@ -154,25 +155,20 @@ static int kmod_config_add_command(struct kmod_config *config,
command);

cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
- if (cmd == NULL)
- goto oom_error_init;
+ if (!cmd)
+ return -ENOMEM;

cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
memcpy(cmd->modname, modname, modnamelen);
memcpy(cmd->command, command, commandlen);

l = kmod_list_append(*list, cmd);
- if (l == NULL)
- goto oom_error;
+ if (!l)
+ return -ENOMEM;

*list = l;
+ cmd = NULL;
return 0;
-
-oom_error:
- free(cmd);
-oom_error_init:
- ERR(config->ctx, "out-of-memory\n");
- return -ENOMEM;
}

static void kmod_config_free_command(struct kmod_config *config,
@@ -188,7 +184,7 @@ static void kmod_config_free_command(struct kmod_config *config,
static int kmod_config_add_options(struct kmod_config *config,
const char *modname, const char *options)
{
- struct kmod_options *opt;
+ _cleanup_free_ struct kmod_options *opt;
struct kmod_list *list;
size_t modnamelen = strlen(modname) + 1;
size_t optionslen = strlen(options) + 1;
@@ -196,8 +192,8 @@ static int kmod_config_add_options(struct kmod_config *config,
DBG(config->ctx, "modname='%s' options='%s'\n", modname, options);

opt = malloc(sizeof(*opt) + modnamelen + optionslen);
- if (opt == NULL)
- goto oom_error_init;
+ if (!opt)
+ return -ENOMEM;

opt->options = sizeof(*opt) + modnamelen + (char *)opt;

@@ -206,17 +202,12 @@ static int kmod_config_add_options(struct kmod_config *config,
strchr_replace(opt->options, '\t', ' ');

list = kmod_list_append(config->options, opt);
- if (list == NULL)
- goto oom_error;
+ if (!list)
+ return -ENOMEM;

+ opt = NULL;
config->options = list;
return 0;
-
-oom_error:
- free(opt);
-oom_error_init:
- ERR(config->ctx, "out-of-memory\n");
- return -ENOMEM;
}

static void kmod_config_free_options(struct kmod_config *config,
@@ -232,7 +223,7 @@ static void kmod_config_free_options(struct kmod_config *config,
static int kmod_config_add_alias(struct kmod_config *config,
const char *name, const char *modname)
{
- struct kmod_alias *alias;
+ _cleanup_free_ struct kmod_alias *alias;
struct kmod_list *list;
size_t namelen = strlen(name) + 1, modnamelen = strlen(modname) + 1;

@@ -240,7 +231,7 @@ static int kmod_config_add_alias(struct kmod_config *config,

alias = malloc(sizeof(*alias) + namelen + modnamelen);
if (!alias)
- goto oom_error_init;
+ return -ENOMEM;

alias->name = sizeof(*alias) + modnamelen + (char *)alias;

@@ -249,16 +240,11 @@ static int kmod_config_add_alias(struct kmod_config *config,

list = kmod_list_append(config->aliases, alias);
if (!list)
- goto oom_error;
+ return -ENOMEM;

+ alias = NULL;
config->aliases = list;
return 0;
-
-oom_error:
- free(alias);
-oom_error_init:
- ERR(config->ctx, "out-of-memory name=%s modname=%s\n", name, modname);
- return -ENOMEM;
}

static void kmod_config_free_alias(struct kmod_config *config,
@@ -274,26 +260,22 @@ static void kmod_config_free_alias(struct kmod_config *config,
static int kmod_config_add_blacklist(struct kmod_config *config,
const char *modname)
{
- char *p;
+ _cleanup_free_ char *p;
struct kmod_list *list;

DBG(config->ctx, "modname=%s\n", modname);

p = strdup(modname);
if (!p)
- goto oom_error_init;
+ return -ENOMEM;

list = kmod_list_append(config->blacklists, p);
if (!list)
- goto oom_error;
+ return -ENOMEM;
+
+ p = NULL;
config->blacklists = list;
return 0;
-
-oom_error:
- free(p);
-oom_error_init:
- ERR(config->ctx, "out-of-memory modname=%s\n", modname);
- return -ENOMEM;
}

static void kmod_config_free_blacklist(struct kmod_config *config,
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:27 UTC
Permalink
---
libkmod/libkmod-module.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 3adbb69..2f92e16 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -252,10 +252,8 @@ static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
keylen = namelen + aliaslen + 1;

m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
- if (m == NULL) {
- free(m);
+ if (m == NULL)
return -ENOMEM;
- }

memset(m, 0, sizeof(*m));

@@ -971,7 +969,8 @@ static int module_do_install_commands(struct kmod_module *mod,
struct probe_insert_cb *cb)
{
const char *command = kmod_module_get_install_commands(mod);
- char *p, *cmd;
+ char *p;
+ _cleanup_free_ char *cmd;
int err;
size_t cmdlen, options_len, varlen;

@@ -994,10 +993,9 @@ static int module_do_install_commands(struct kmod_module *mod,
size_t slen = cmdlen - varlen + options_len;
char *suffix = p + varlen;
char *s = malloc(slen + 1);
- if (s == NULL) {
- free(cmd);
+ if (!s)
return -ENOMEM;
- }
+
memcpy(s, cmd, p - cmd);
memcpy(s + prefixlen, options, options_len);
memcpy(s + prefixlen + options_len, suffix, suffixlen);
@@ -1013,8 +1011,6 @@ static int module_do_install_commands(struct kmod_module *mod,
else
err = command_do(mod, "install", cmd);

- free(cmd);
-
return err;
}
--
1.8.4.2
Lucas De Marchi
2013-11-18 14:04:20 UTC
Permalink
---
libkmod/libkmod-util.h | 7 +++++++
libkmod/macro.h | 1 +
2 files changed, 8 insertions(+)

diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h
index 8a70aeb..bc1ed4d 100644
--- a/libkmod/libkmod-util.h
+++ b/libkmod/libkmod-util.h
@@ -3,6 +3,7 @@

#include <limits.h>
#include <stdbool.h>
+#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -56,3 +57,9 @@ static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
{
return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
}
+
+static inline void freep(void *p) {
+ free(*(void**) p);
+}
+
+#define _cleanup_free_ _cleanup_(freep)
diff --git a/libkmod/macro.h b/libkmod/macro.h
index 7969072..5396598 100644
--- a/libkmod/macro.h
+++ b/libkmod/macro.h
@@ -57,3 +57,4 @@
#define _printf_format_(a,b) __attribute__((format (printf, a, b)))
#define _unused_ __attribute__((unused))
#define _always_inline_ __inline__ __attribute__((always_inline))
+#define _cleanup_(x) __attribute__((cleanup(x)))
--
1.8.4.2
Loading...