[sheepdog] [PATCH]Check malloc error for collie

harryxiyou at gmail.com harryxiyou at gmail.com
Thu Feb 14 16:03:57 CET 2013


Check malloc error in func init_command for collie binary.

Signed-off-by: Harry Wei <harryxiyou at gmail.com>

---
 collie/collie.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/collie/collie.c b/collie/collie.c
index a84baba..41bb3a7 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -131,7 +131,7 @@ static const struct sd_option *find_opt(int ch)
 	exit(EXIT_SYSFAIL);
 }
 
-static void init_commands(const struct command **commands)
+static int init_commands(const struct command **commands)
 {
 	static struct command *cmds;
 	struct command command_list[] = {
@@ -144,11 +144,15 @@ static void init_commands(const struct command **commands)
 
 	if (!cmds) {
 		cmds = (struct command *)malloc(sizeof(command_list));
+		if (cmds == NULL) {
+			fprintf(stderr, "Failed to allocate enough RAM.\n");
+			return -1;
+		}
 		memcpy(cmds, command_list, sizeof(command_list));
 	}
 
 	*commands = cmds;
-	return;
+	return 0;
 }
 
 static const struct subcommand *find_subcmd(const char *cmd, const char *subcmd)
@@ -322,7 +326,11 @@ int main(int argc, char **argv)
 	char *p;
 	const struct sd_option *sd_opts;
 
-	init_commands(&commands);
+	ret = init_commands(&commands);
+	if (ret < 0) {
+		fprintf(stderr, "Failed to init commands.\n");
+		exit(EXIT_SYSFAIL);
+	}
 
 	if (argc < 3)
 		usage(commands, 0);
-- 
1.7.0.4




More information about the sheepdog mailing list