Skip to content

Commit

Permalink
Merge pull request #72 from SafeCoding233/check-null
Browse files Browse the repository at this point in the history
Check null pointer for strdup
  • Loading branch information
hmgle authored Jul 28, 2024
2 parents 2ab0aff + d1c025b commit 75169e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static int config_local_addr(const char *key, const char *value, struct graftcp_
if (strlen(value) <= 0)
return -1;
conf->local_addr = strdup(value);
if (!conf->local_addr)
return -1;
return 0;
}

Expand All @@ -59,18 +61,24 @@ static int config_local_port(const char *key, const char *value, struct graftcp_
static int config_pipe_path(const char *key, const char *value, struct graftcp_conf *conf)
{
conf->pipe_path = strdup(value);
if (!conf->pipe_path)
return -1;
return 0;
}

static int config_blackip_file_path(const char *key, const char *value, struct graftcp_conf *conf)
{
conf->blackip_file_path = strdup(value);
if (!conf->blackip_file_path)
return -1;
return 0;
}

static int config_whiteip_file_path(const char *key, const char *value, struct graftcp_conf *conf)
{
conf->whiteip_file_path = strdup(value);
if (!conf->whiteip_file_path)
return -1;
return 0;
}

Expand Down
28 changes: 28 additions & 0 deletions graftcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,29 +581,53 @@ int client_main(int argc, char **argv)
switch (opt) {
case 'a':
cmd_conf.local_addr = strdup(optarg);
if (cmd_conf.local_addr == NULL) {
perror("strdup");
exit(1);
}
break;
case 'p':
cmd_conf.local_port = malloc(sizeof(*cmd_conf.local_port));
*cmd_conf.local_port = atoi(optarg);
break;
case 'f':
cmd_conf.pipe_path = strdup(optarg);
if (cmd_conf.pipe_path == NULL) {
perror("strdup");
exit(1);
}
break;
case 'b':
cmd_conf.blackip_file_path = strdup(optarg);
if (cmd_conf.blackip_file_path == NULL) {
perror("strdup");
exit(1);
}
break;
case 'w':
cmd_conf.whiteip_file_path = strdup(optarg);
if (cmd_conf.whiteip_file_path == NULL) {
perror("strdup");
exit(1);
}
break;
case 'n':
cmd_conf.ignore_local = malloc(sizeof(*cmd_conf.ignore_local));
*cmd_conf.ignore_local = false;
break;
case 'c':
conf_file_path = strdup(optarg);
if (conf_file_path == NULL) {
perror("strdup");
exit(1);
}
break;
case 'u':
cmd_conf.username = strdup(optarg);
if (cmd_conf.username == NULL) {
perror("strdup");
exit(1);
}
break;
case 'V':
fprintf(stderr, "graftcp %s\n", VERSION);
Expand Down Expand Up @@ -670,6 +694,10 @@ int client_main(int argc, char **argv)
run_gid = pent->pw_gid;
run_uid = pent->pw_uid;
run_home = strdup(pent->pw_dir);
if (run_home == NULL) {
perror("strdup");
exit(1);
}
}

init(&conf, argc - optind, argv + optind);
Expand Down

0 comments on commit 75169e7

Please sign in to comment.