Skip to content

Commit

Permalink
cli: fix cli_options.target leaking memory
Browse files Browse the repository at this point in the history
  • Loading branch information
7Ji committed Jun 6, 2024
1 parent c998dd0 commit c0d4c3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/* Local */
#include "ept.h"
#include <linux/limits.h>

/* Enumerable */

Expand Down Expand Up @@ -54,7 +55,7 @@ struct
uint64_t gap_partition;
uint64_t gap_reserved;
size_t size;
char * target;
char target[PATH_MAX];
};

/* Variable */
Expand Down
24 changes: 16 additions & 8 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/* System */

#include <bits/getopt_core.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
Expand Down Expand Up @@ -77,7 +78,7 @@ struct cli_options cli_options = {
.gap_partition = EPT_PARTITION_GAP_GENERIC,
.gap_reserved = EPT_PARTITION_GAP_RESERVED,
.size = 0,
.target = NULL
.target = ""
};

/* Function */
Expand Down Expand Up @@ -302,6 +303,17 @@ cli_parse_options(
return 0;
}

static inline
void
cli_option_replace_target(
// struct cli_options *const restrict cli_options,
char const *const restrict target
){
size_t len_target = strnlen(target, (sizeof cli_options.target) - 1);
memcpy(cli_options.target, target, len_target);
cli_options.target[len_target] = '\0';
}

static inline
int
cli_find_disk(){
Expand All @@ -311,8 +323,8 @@ cli_find_disk(){
return 1;
}
pr_error("CLI interface: Operating on '%s' instead, content type is now disk\n", path_disk);
free(cli_options.target);
cli_options.target = path_disk;
cli_option_replace_target(path_disk);
free(path_disk);
cli_options.content = CLI_CONTENT_TYPE_DISK;
return 0;
}
Expand Down Expand Up @@ -384,11 +396,7 @@ cli_complete_options(
cli_options.write = CLI_WRITE_NOTHING;
}
if (optind < argc) {
cli_options.target = strdup(argv[optind++]);
if (!cli_options.target) {
pr_error("CLI interface: Failed to duplicate target string '%s'\n", argv[optind-1]);
return 2;
}
cli_option_replace_target(argv[optind++]);
pr_error("CLI interface: Operating on target file/block device '%s'\n", cli_options.target);
int const r = cli_options_complete_target_info();
if (r) {
Expand Down

0 comments on commit c0d4c3c

Please sign in to comment.