From 679fac1f8be87ccb431d1ce981e98bdbfbd61dd8 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 25 Nov 2021 20:28:52 +0100 Subject: Fixed a bug in freeing memory after execution of certain commands (thanks to Tommaso for reporting) --- src/commands.c | 38 ++++++++++++++++++++++---------------- src/commands.h | 1 + 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/commands.c b/src/commands.c index 48f5eba..2442d81 100644 --- a/src/commands.c +++ b/src/commands.c @@ -105,13 +105,7 @@ solve_parse_args(int c, char **v) int i; long val; - CommandArgs *a = malloc(sizeof(CommandArgs)); - - a->success = false; - a->opts = malloc(sizeof(SolveOptions)); - a->step = steps[0]; - a->command = NULL; - a->scramble = NULL; + CommandArgs *a = new_args(); a->opts->min_moves = 0; a->opts->max_moves = 20; @@ -182,12 +176,7 @@ CommandArgs * help_parse_args(int c, char **v) { int i; - CommandArgs *a = malloc(sizeof(CommandArgs)); - - a->scramble = NULL; - a->opts = NULL; - a->step = NULL; - a->command = NULL; + CommandArgs *a = new_args(); if (c == 1) { for (i = 0; i < NCOMMANDS; i++) @@ -205,9 +194,9 @@ help_parse_args(int c, char **v) CommandArgs * parse_no_arg(int c, char **v) { - CommandArgs *a = malloc(sizeof(CommandArgs)); + CommandArgs *a = new_args(); - a->success = true; + a->success = true; return a; } @@ -215,9 +204,10 @@ parse_no_arg(int c, char **v) CommandArgs * print_parse_args(int c, char **v) { - CommandArgs *a = malloc(sizeof(CommandArgs)); + CommandArgs *a = new_args(); a->success = read_scramble(c, v, a); + return a; } @@ -364,3 +354,19 @@ free_args(CommandArgs *args) free(args); } + +CommandArgs * +new_args() +{ + CommandArgs *args = malloc(sizeof(CommandArgs)); + + args->success = false; + args->scramble = NULL; /* initialized in read_scramble */ + args->opts = malloc(sizeof(SolveOptions)); + + /* step and command are static */ + args->step = NULL; + args->command = NULL; + + return args; +} diff --git a/src/commands.h b/src/commands.h index f2703fa..938a0bd 100644 --- a/src/commands.h +++ b/src/commands.h @@ -7,6 +7,7 @@ #define NCOMMANDS 10 void free_args(CommandArgs *args); +CommandArgs * new_args(); extern Command * commands[NCOMMANDS]; -- cgit v1.3