NSH library: malloc/free IOBUFFER instead of using stack

This commit is contained in:
Gregory Nutt 2014-04-21 18:12:59 -06:00
parent a2600cbe83
commit 03ec268ef9
3 changed files with 21 additions and 3 deletions

View File

@ -889,4 +889,6 @@
* apps/system/nxplayer/nxplayer.c: Complilation failure in one
configuration reported by Manuel Stuhn (2014-4-21).
* apps/system/sdcard: Remove an STM32 dependency. From Bob Doiron
(2014-4-21)
(2014-4-21).
* apps/nshlib: malloc/free IOBUFFER for 'cat' and 'hexdump' commands
instead of using the stack. From Bob Doiron (2014-4-21).

View File

@ -373,7 +373,7 @@ int cmd_xd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
#ifndef CONFIG_NSH_DISABLE_HEXDUMP
int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
uint8_t buffer[IOBUFFERSIZE];
FAR uint8_t *buffer;
char msg[32];
off_t position;
int fd;
@ -394,6 +394,13 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
return ERROR;
}
buffer = (FAR uint8_t *)malloc(IOBUFFERSIZE);
if(buffer == NULL)
{
nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "malloc", NSH_ERRNO);
return ERROR;
}
#ifdef CONFIG_NSH_CMDOPT_HEXDUMP
for (x = 2; x < argc; x++)
{
@ -489,6 +496,7 @@ int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
}
(void)close(fd);
free(buffer);
return ret;
}
#endif

View File

@ -416,7 +416,7 @@ static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath,
static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR const char *filename)
{
char buffer[IOBUFFERSIZE];
FAR char *buffer;
int fd;
int ret = OK;
@ -429,6 +429,13 @@ static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
return ERROR;
}
buffer = (FAR char *)malloc(IOBUFFERSIZE);
if(buffer == NULL)
{
nsh_output(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO);
return ERROR;
}
/* And just dump it byte for byte into stdout */
for (;;)
@ -514,6 +521,7 @@ static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
/* Close the input file and return the result */
(void)close(fd);
free(buffer);
return ret;
}
#endif