nshlib/nsh_codeccmd.c: fix potential NULL dereference and check malloc return values

This commit is contained in:
Juha Niskanen 2020-04-16 13:36:21 +03:00 committed by patacongo
parent d2625c8b9d
commit 89b981bdb6

View File

@ -360,6 +360,12 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
}
srcbuf = malloc(CONFIG_NSH_CODECS_BUFSIZE + 2);
if (!srcbuf)
{
fmt = g_fmtcmdoutofmemory;
goto errout;
}
#ifdef HAVE_CODECS_BASE64ENC
if (mode == CODEC_MODE_BASE64ENC)
{
@ -373,6 +379,12 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
buflen = calc_codec_buffsize(srclen + 2, mode);
destbuf = malloc(buflen);
if (!destbuf)
{
fmt = g_fmtcmdoutofmemory;
goto errout;
}
while (true)
{
memset(srcbuf, 0, srclen + 2);
@ -412,7 +424,7 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
else
#endif
{
func(srcbuf, ret, destbuf, &buflen,(iswebsafe)?1:0);
func(srcbuf, ret, destbuf, &buflen, iswebsafe ? 1 : 0);
nsh_output(vtbl, "%s", destbuf);
}
}
@ -447,7 +459,6 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
srclen = strlen(sdata);
buflen = calc_codec_buffsize(srclen, mode);
destbuf = malloc(buflen);
destbuf[0]=0;
if (!destbuf)
{
fmt = g_fmtcmdoutofmemory;
@ -477,7 +488,7 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
else
#endif
{
func(srcbuf, srclen, destbuf, &buflen,(iswebsafe)?1:0);
func(srcbuf, srclen, destbuf, &buflen, iswebsafe ? 1 : 0);
}
}