From 54841ab50c20d6fa6c9cc3eb826989da3a22d934 Mon Sep 17 00:00:00 2001 From: Wolfgang Denk Date: Mon, 28 Jun 2010 22:00:46 +0200 Subject: Make sure that argv[] argument pointers are not modified. The hush shell dynamically allocates (and re-allocates) memory for the argument strings in the "char *argv[]" argument vector passed to commands. Any code that modifies these pointers will cause serious corruption of the malloc data structures and crash U-Boot, so make sure the compiler can check that no such modifications are being done by changing the code into "char * const argv[]". This modification is the result of debugging a strange crash caused after adding a new command, which used the following argument processing code which has been working perfectly fine in all Unix systems since version 6 - but not so in U-Boot: int main (int argc, char **argv) { while (--argc > 0 && **++argv == '-') { /* ====> */ while (*++*argv) { switch (**argv) { case 'd': debug++; break; ... default: usage (); } } } ... } The line marked "====>" will corrupt the malloc data structures and usually cause U-Boot to crash when the next command gets executed by the shell. With the modification, the compiler will prevent this with an error: increment of read-only location '*argv' N.B.: The code above can be trivially rewritten like this: while (--argc > 0 && **++argv == '-') { char *arg = *argv; while (*++arg) { switch (*arg) { ... Signed-off-by: Wolfgang Denk Acked-by: Mike Frysinger --- examples/standalone/82559_eeprom.c | 2 +- examples/standalone/atmel_df_pow2.c | 2 +- examples/standalone/eepro100_eeprom.c | 2 +- examples/standalone/hello_world.c | 2 +- examples/standalone/interrupt.c | 2 +- examples/standalone/mem_to_mem_idma2intr.c | 4 ++-- examples/standalone/smc91111_eeprom.c | 2 +- examples/standalone/smc911x_eeprom.c | 2 +- examples/standalone/stubs.c | 2 +- examples/standalone/test_burst.c | 2 +- examples/standalone/timer.c | 2 +- 11 files changed, 12 insertions(+), 12 deletions(-) (limited to 'examples/standalone') diff --git a/examples/standalone/82559_eeprom.c b/examples/standalone/82559_eeprom.c index 5e2eee9e98..8dd7079ae7 100644 --- a/examples/standalone/82559_eeprom.c +++ b/examples/standalone/82559_eeprom.c @@ -305,7 +305,7 @@ write_config_word(int bus, int dev, int func, int reg, u16 data) } -int main (int argc, char *argv[]) +int main (int argc, char * const argv[]) { unsigned char *eth_addr; uchar buf[6]; diff --git a/examples/standalone/atmel_df_pow2.c b/examples/standalone/atmel_df_pow2.c index db0cd693a9..b5b450317d 100644 --- a/examples/standalone/atmel_df_pow2.c +++ b/examples/standalone/atmel_df_pow2.c @@ -114,7 +114,7 @@ static char *getline(void) } } -int atmel_df_pow2(int argc, char *argv[]) +int atmel_df_pow2(int argc, char * const argv[]) { /* Print the ABI version */ app_startup(argv); diff --git a/examples/standalone/eepro100_eeprom.c b/examples/standalone/eepro100_eeprom.c index 771a7e887e..3c7f380977 100644 --- a/examples/standalone/eepro100_eeprom.c +++ b/examples/standalone/eepro100_eeprom.c @@ -25,7 +25,7 @@ static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr); -int eepro100_eeprom(int argc, char *argv[]) +int eepro100_eeprom(int argc, char * const argv[]) { int ret = 0; diff --git a/examples/standalone/hello_world.c b/examples/standalone/hello_world.c index 9317f6d8c0..067c39046d 100644 --- a/examples/standalone/hello_world.c +++ b/examples/standalone/hello_world.c @@ -24,7 +24,7 @@ #include #include -int hello_world (int argc, char *argv[]) +int hello_world (int argc, char * const argv[]) { int i; diff --git a/examples/standalone/interrupt.c b/examples/standalone/interrupt.c index f3061d1ec0..a5b58a1a08 100644 --- a/examples/standalone/interrupt.c +++ b/examples/standalone/interrupt.c @@ -44,7 +44,7 @@ static void irq_handler (void *arg); -int interrupt (int argc, char *argv[]) +int interrupt (int argc, char * const argv[]) { int c, irq = -1; diff --git a/examples/standalone/mem_to_mem_idma2intr.c b/examples/standalone/mem_to_mem_idma2intr.c index f35f1ba5f6..d0a75eadfd 100644 --- a/examples/standalone/mem_to_mem_idma2intr.c +++ b/examples/standalone/mem_to_mem_idma2intr.c @@ -204,9 +204,9 @@ int memcmp(const void * cs,const void * ct,size_t count) #endif /* STANDALONE */ #ifdef STANDALONE -int mem_to_mem_idma2intr (int argc, char *argv[]) +int mem_to_mem_idma2intr (int argc, char * const argv[]) #else -int do_idma (bd_t * bd, int argc, char *argv[]) +int do_idma (bd_t * bd, int argc, char * const argv[]) #endif /* STANDALONE */ { int i; diff --git a/examples/standalone/smc91111_eeprom.c b/examples/standalone/smc91111_eeprom.c index b91f34c3af..c98b0fc075 100644 --- a/examples/standalone/smc91111_eeprom.c +++ b/examples/standalone/smc91111_eeprom.c @@ -48,7 +48,7 @@ void print_MAC (struct eth_device *dev); int read_eeprom_reg (struct eth_device *dev, int reg); void print_macaddr (struct eth_device *dev); -int smc91111_eeprom (int argc, char *argv[]) +int smc91111_eeprom (int argc, char * const argv[]) { int c, i, j, done, line, reg, value, start, what; char input[50]; diff --git a/examples/standalone/smc911x_eeprom.c b/examples/standalone/smc911x_eeprom.c index 104047f5a5..c51a05028a 100644 --- a/examples/standalone/smc911x_eeprom.c +++ b/examples/standalone/smc911x_eeprom.c @@ -313,7 +313,7 @@ static char *getline(void) /** * smc911x_eeprom - our application's main() function */ -int smc911x_eeprom(int argc, char *argv[]) +int smc911x_eeprom(int argc, char * const argv[]) { /* Avoid initializing on stack as gcc likes to call memset() */ struct eth_device dev; diff --git a/examples/standalone/stubs.c b/examples/standalone/stubs.c index f3e1ab5c6a..2d2e7098b7 100644 --- a/examples/standalone/stubs.c +++ b/examples/standalone/stubs.c @@ -189,7 +189,7 @@ void __attribute__((unused)) dummy(void) extern unsigned long __bss_start, _end; -void app_startup(char **argv) +void app_startup(char * const *argv) { unsigned char * cp = (unsigned char *) &__bss_start; diff --git a/examples/standalone/test_burst.c b/examples/standalone/test_burst.c index 7109c098ef..2b101b74e6 100644 --- a/examples/standalone/test_burst.c +++ b/examples/standalone/test_burst.c @@ -85,7 +85,7 @@ static unsigned long test_pattern [] = { }; -int test_burst (int argc, char *argv[]) +int test_burst (int argc, char * const argv[]) { unsigned long size = CACHE_LINE_SIZE; unsigned int pass = 0; diff --git a/examples/standalone/timer.c b/examples/standalone/timer.c index 6628b21de9..834cc9a49e 100644 --- a/examples/standalone/timer.c +++ b/examples/standalone/timer.c @@ -115,7 +115,7 @@ void setPeriod (tid_8xx_cpmtimer_t *hwp, ulong interval); static char *usage = "\n[q, b, e, ?] "; -int timer (int argc, char *argv[]) +int timer (int argc, char * const argv[]) { cpmtimer8xx_t *cpmtimerp; /* Pointer to the CPM Timer structure */ tid_8xx_cpmtimer_t hw; -- cgit v1.2.1