Fix cliparser buffer allocation

This commit is contained in:
Philippe Teuwen 2020-06-10 11:33:15 +02:00
commit 569107579e

View file

@ -11,6 +11,9 @@
#include "cliparser.h" #include "cliparser.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#ifndef ARRAYLEN
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
#endif
int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp) { int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp) {
*ctx = malloc(sizeof(CLIParserContext)); *ctx = malloc(sizeof(CLIParserContext));
@ -88,15 +91,14 @@ int CLIParserParseStringEx(CLIParserContext *ctx, const char *str, void *vargtab
char *argv[200] = {NULL}; char *argv[200] = {NULL};
int len = strlen(str); int len = strlen(str);
char buf[500] = {0}; memset(ctx->buf, 0x00, ARRAYLEN(ctx->buf));
memset(ctx->buf, 0x00, 500); char *bufptr = ctx->buf;
char *bufptr = buf;
char *spaceptr = NULL; char *spaceptr = NULL;
enum ParserState state = PS_FIRST; enum ParserState state = PS_FIRST;
argv[argc++] = bufptr; argv[argc++] = bufptr;
// param0 = program name // param0 = program name
memcpy(buf, ctx->programName, strlen(ctx->programName) + 1); // with 0x00 memcpy(ctx->buf, ctx->programName, strlen(ctx->programName) + 1); // with 0x00
bufptr += strlen(ctx->programName) + 1; bufptr += strlen(ctx->programName) + 1;
if (len) if (len)
argv[argc++] = bufptr; argv[argc++] = bufptr;