From ade5670dba5266e73c4a61060b370b9acf06cade Mon Sep 17 00:00:00 2001 From: Leo C Date: Sun, 8 Apr 2018 21:18:35 +0200 Subject: [PATCH] Remove xmalloc.c, fix potential 0-pointer derefenrence in cli.c. --- avr/Tupfile | 4 ++-- avr/cli.c | 19 +++++++++++-------- avr/env.c | 5 ++--- avr/xmalloc.c | 32 -------------------------------- include/xmalloc.h | 8 -------- 5 files changed, 15 insertions(+), 53 deletions(-) delete mode 100644 avr/xmalloc.c delete mode 100644 include/xmalloc.h diff --git a/avr/Tupfile b/avr/Tupfile index db17fad..e3ab922 100644 --- a/avr/Tupfile +++ b/avr/Tupfile @@ -9,8 +9,7 @@ SRC += cli.c cli_readline.c command.c command_tbl.c SRC += cmd_run.c cmd_boot.c cmd_misc.c SRC += cmd_date.c cmd_mem.c cmd_gpio.c cmd_attach.c SRC += cmd_loadihex.c cmd_loadcpm3.c cmd_sd.c cmd_fat.c -SRC += env.c xmalloc.c con-utils.c print-utils.c -SRC += getopt-min.c eval_arg.c +SRC += env.c con-utils.c print-utils.c getopt-min.c eval_arg.c SRC += timer.c serial.c i2c.c bcd.c pcf8583.c mmc.c SRC += background.c z180-serv.c z80-if.c gpio.c SRC += $(FATFS) $(TOP)/fatfs/src/option/unicode.c @@ -96,6 +95,7 @@ ASFLAGS += -mmcu=$(MCU_TARGET) -x assembler-with-cpp $(ASFLAGS) # Linker flags LDFLAGS += -Wl,--gc-sections LDFLAGS += -Wl,--cref +#LDFLAGS += -flto -Os # Assemble: create object files from assembler source files. #.S.o: diff --git a/avr/cli.c b/avr/cli.c index f9ffc37..b96ca1b 100644 --- a/avr/cli.c +++ b/avr/cli.c @@ -12,16 +12,10 @@ */ #include "cli.h" -#include "common.h" - -#include +#include "command.h" #include -#include -#include #include "config.h" -#include "command.h" -#include "xmalloc.h" #include "debug.h" #include "env.h" #include "cli_readline.h" @@ -156,7 +150,12 @@ char *process_macros(char *input, char *output) outp = output; } else { int outputlen = outp - output; - outp = xrealloc(output, outputlen); + outp = realloc(output, outputlen); + if (outp == NULL) { + free(output); + output = outp; + break; + } output = outp; } @@ -315,6 +314,10 @@ static int cli_run_command(const char *cmd, uint_fast8_t flag) /* find macros in this token and replace them */ finaltoken = process_macros(token, finaltoken); + if (finaltoken == NULL) { + rc = -1; /* no command at all */ + break; + } /* Extract arguments */ argc = cli_parse_line(finaltoken, argv); diff --git a/avr/env.c b/avr/env.c index 3a12325..3b299c4 100644 --- a/avr/env.c +++ b/avr/env.c @@ -9,7 +9,6 @@ #include "config.h" #include "debug.h" -#include "xmalloc.h" #include "crc.h" @@ -194,7 +193,7 @@ int envlist_import(uint8_t flags) return -1; } - np = (char *) xmalloc(len+1); + np = (char *) malloc(len+1); if (np == NULL) { printf_P(PSTR("## Can't malloc %d bytes\n"), len+1); return 1; @@ -483,7 +482,7 @@ command_ret_t _do_env_set(uint_fast8_t flag, int argc, char * const argv[]) for (i = 2, len += 1; i < argc; ++i) len += strlen(argv[i]) + 1; - value = xmalloc(len); + value = malloc(len); if (value == NULL) { printf_P(PSTR("## Can't malloc %d bytes\n"), len); return CMD_RET_FAILURE; diff --git a/avr/xmalloc.c b/avr/xmalloc.c deleted file mode 100644 index d42d5c9..0000000 --- a/avr/xmalloc.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * (C) Copyright 2014 Leo C. - * - * SPDX-License-Identifier: GPL-2.0+ - */ - -#include -#include "debug.h" -#include "xmalloc.h" - -void* xmalloc(size_t size) -{ - void *p; - - p = malloc(size); - - if (p == NULL) - debug("*** Out of memory!\n"); - - return p; -} - - -void* xrealloc(void *p, size_t size) -{ - p = realloc(p, size); - - if (p == NULL) - debug("*** Out of memory!\n"); - - return p; -} diff --git a/include/xmalloc.h b/include/xmalloc.h deleted file mode 100644 index cb0019f..0000000 --- a/include/xmalloc.h +++ /dev/null @@ -1,8 +0,0 @@ - -#ifndef XMALLOC_H -#define XMALLOC_H - -void* xmalloc(size_t size); -void* xrealloc(void *p, size_t size); - -#endif /* XMALLOC_H */ -- 2.39.2