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

Add TAKE_PTR() #264

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 8 additions & 4 deletions libkmod/libkmod-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ static int kmod_config_add_command(struct kmod_config *config, const char *modna
if (!l)
return -ENOMEM;

TAKE_PTR(cmd);
*list = l;
cmd = NULL;
lucasdemarchi marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}

Expand Down Expand Up @@ -175,8 +176,9 @@ static int kmod_config_add_options(struct kmod_config *config, const char *modna
if (!list)
return -ENOMEM;

opt = NULL;
TAKE_PTR(opt);
config->options = list;

return 0;
}

Expand All @@ -202,8 +204,9 @@ static int kmod_config_add_alias(struct kmod_config *config, const char *name,
if (!list)
return -ENOMEM;

alias = NULL;
TAKE_PTR(alias);
config->aliases = list;

return 0;
}

Expand All @@ -222,8 +225,9 @@ static int kmod_config_add_blacklist(struct kmod_config *config, const char *mod
if (!list)
return -ENOMEM;

p = NULL;
TAKE_PTR(p);
config->blacklists = list;

return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions libkmod/libkmod-file-zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

int kmod_file_load_zlib(struct kmod_file *file)
{
_cleanup_free_ unsigned char *p = NULL;
int err = 0;
off_t did = 0, total = 0;
_cleanup_free_ unsigned char *p = NULL;
gzFile gzf;
int gzfd;

Expand Down Expand Up @@ -68,10 +68,10 @@ int kmod_file_load_zlib(struct kmod_file *file)
did += r;
}

file->memory = p;
file->memory = TAKE_PTR(p);
file->size = did;
p = NULL;
gzclose(gzf);

return 0;

error:
Expand Down
2 changes: 1 addition & 1 deletion libkmod/libkmod-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,9 @@ static int do_finit_module(struct kmod_module *mod, unsigned int flags, const ch

static int do_init_module(struct kmod_module *mod, unsigned int flags, const char *args)
{
_cleanup_free_ const void *stripped = NULL;
struct kmod_elf *elf;
const void *mem;
_cleanup_free_ const void *stripped = NULL;
off_t size;
int err;

Expand Down
8 changes: 5 additions & 3 deletions shared/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ char *freadline_wrapped(FILE *fp, unsigned int *linenum)
n++;

{
char *ret = buf;
char *ret = TAKE_PTR(buf);

ret[i] = '\0';
buf = NULL;
if (linenum)
*linenum += n;

return ret;
}

Expand Down Expand Up @@ -390,7 +391,8 @@ char *path_make_absolute_cwd(const char *p)
if (r == NULL)
return NULL;

cwd = NULL;
TAKE_PTR(cwd);

r[cwdlen] = '/';
memcpy(&r[cwdlen + 1], p, plen + 1);

Expand Down
7 changes: 7 additions & 0 deletions shared/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ static inline bool umulsz_overflow(size_t a, size_t b, size_t *res)
#error "Unknown sizeof(size_t)"
#endif
}

#define TAKE_PTR(x) \
({ \
typeof(x) x__ = x; \
(x) = NULL; \
x__; \
})