]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
Remove xmalloc.c, fix potential 0-pointer derefenrence in cli.c.
authorLeo C <erbl259-lmu@yahoo.de>
Sun, 8 Apr 2018 19:18:35 +0000 (21:18 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Sun, 8 Apr 2018 19:18:35 +0000 (21:18 +0200)
avr/Tupfile
avr/cli.c
avr/env.c
avr/xmalloc.c [deleted file]
include/xmalloc.h [deleted file]

index db17fad6f6a592c4098bec8655b2e75dd3521e62..e3ab922751432d9796d7bb48d589f6ea006f71c3 100644 (file)
@@ -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:
index f9ffc37a33127ca4cd0f4e1b906b722069161a4d..b96ca1bcf9ba8dfc247a47724307967283977ad8 100644 (file)
--- a/avr/cli.c
+++ b/avr/cli.c
  */
 
 #include "cli.h"
-#include "common.h"
-
-#include <string.h>
+#include "command.h"
 #include <ctype.h>
-#include <stdlib.h>
-#include <stdio.h>
 
 #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);
index 3a123255ca01633144d0a635275fa3e977ec9176..3b299c4b9cb18e99d0a828dc9ceb070b983910f2 100644 (file)
--- 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 (file)
index d42d5c9..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
- *
- * SPDX-License-Identifier:    GPL-2.0+
- */
-
-#include <stdlib.h>
-#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 (file)
index cb0019f..0000000
+++ /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 */