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

@ -156,7 +156,7 @@ static void urlencode_cb(FAR char *src, int srclen, FAR char *dest,
static void urldecode_cb(FAR char *src, int srclen, FAR char *dest, static void urldecode_cb(FAR char *src, int srclen, FAR char *dest,
FAR int *destlen, int mode) FAR int *destlen, int mode)
{ {
urldecode(src,srclen,dest,destlen); urldecode(src, srclen, dest, destlen);
} }
#endif #endif
@ -359,7 +359,13 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
goto exit; goto exit;
} }
srcbuf = malloc(CONFIG_NSH_CODECS_BUFSIZE+2); srcbuf = malloc(CONFIG_NSH_CODECS_BUFSIZE + 2);
if (!srcbuf)
{
fmt = g_fmtcmdoutofmemory;
goto errout;
}
#ifdef HAVE_CODECS_BASE64ENC #ifdef HAVE_CODECS_BASE64ENC
if (mode == CODEC_MODE_BASE64ENC) if (mode == CODEC_MODE_BASE64ENC)
{ {
@ -371,19 +377,25 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
srclen = CONFIG_NSH_CODECS_BUFSIZE; srclen = CONFIG_NSH_CODECS_BUFSIZE;
} }
buflen = calc_codec_buffsize(srclen+2, mode); buflen = calc_codec_buffsize(srclen + 2, mode);
destbuf = malloc(buflen); destbuf = malloc(buflen);
if (!destbuf)
{
fmt = g_fmtcmdoutofmemory;
goto errout;
}
while (true) while (true)
{ {
memset(srcbuf, 0, srclen+2); memset(srcbuf, 0, srclen + 2);
ret=read(fd, srcbuf, srclen); ret = read(fd, srcbuf, srclen);
if (ret < 0) if (ret < 0)
{ {
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO); nsh_error(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
ret = ERROR; ret = ERROR;
goto exit; goto exit;
} }
else if (ret==0) else if (ret == 0)
{ {
break; break;
} }
@ -391,13 +403,13 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
#ifdef HAVE_CODECS_URLDECODE #ifdef HAVE_CODECS_URLDECODE
if (mode == CODEC_MODE_URLDECODE) if (mode == CODEC_MODE_URLDECODE)
{ {
if (srcbuf[srclen-1]=='%') if (srcbuf[srclen - 1] == '%')
{ {
ret += read(fd,&srcbuf[srclen],2); ret += read(fd, &srcbuf[srclen], 2);
} }
else if (srcbuf[srclen-2]=='%') else if (srcbuf[srclen - 2] == '%')
{ {
ret += read(fd,&srcbuf[srclen],1); ret += read(fd, &srcbuf[srclen], 1);
} }
} }
#endif #endif
@ -407,17 +419,17 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
#ifdef HAVE_CODECS_HASH_MD5 #ifdef HAVE_CODECS_HASH_MD5
if (mode == CODEC_MODE_HASH_MD5) if (mode == CODEC_MODE_HASH_MD5)
{ {
func(srcbuf, ret, (char *)&ctx, &buflen,0); func(srcbuf, ret, (char *)&ctx, &buflen, 0);
} }
else else
#endif #endif
{ {
func(srcbuf, ret, destbuf, &buflen,(iswebsafe)?1:0); func(srcbuf, ret, destbuf, &buflen, iswebsafe ? 1 : 0);
nsh_output(vtbl, "%s", destbuf); nsh_output(vtbl, "%s", destbuf);
} }
} }
buflen = calc_codec_buffsize(srclen+2, mode); buflen = calc_codec_buffsize(srclen + 2, mode);
} }
#ifdef HAVE_CODECS_HASH_MD5 #ifdef HAVE_CODECS_HASH_MD5
@ -447,7 +459,6 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
srclen = strlen(sdata); srclen = strlen(sdata);
buflen = calc_codec_buffsize(srclen, mode); buflen = calc_codec_buffsize(srclen, mode);
destbuf = malloc(buflen); destbuf = malloc(buflen);
destbuf[0]=0;
if (!destbuf) if (!destbuf)
{ {
fmt = g_fmtcmdoutofmemory; fmt = g_fmtcmdoutofmemory;
@ -477,11 +488,11 @@ static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
else else
#endif #endif
{ {
func(srcbuf, srclen, destbuf, &buflen,(iswebsafe)?1:0); func(srcbuf, srclen, destbuf, &buflen, iswebsafe ? 1 : 0);
} }
} }
nsh_output(vtbl, "%s\n",destbuf); nsh_output(vtbl, "%s\n", destbuf);
srcbuf = NULL; srcbuf = NULL;
goto exit; goto exit;
} }