fix coverity 286922

This commit is contained in:
iceman1001 2020-06-29 10:42:22 +02:00
commit b5b5520546

View file

@ -2292,7 +2292,6 @@ static int selectfile(uint8_t *aid, uint32_t fileno, uint8_t *cs) {
return res; return res;
} }
static int CmdHF14ADesClearRecordFile(const char *Cmd) { static int CmdHF14ADesClearRecordFile(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes clearrecord", CLIParserInit(&ctx, "hf mfdes clearrecord",
@ -2822,7 +2821,6 @@ static int CmdHF14ADesChangeValue(const char *Cmd) {
return res; return res;
} }
static int CmdHF14ADesWriteData(const char *Cmd) { static int CmdHF14ADesWriteData(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
@ -2853,14 +2851,12 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
uint8_t offset[3] = {0}; uint8_t offset[3] = {0};
CLIParamHexToBuf(arg_get_str(ctx, 2), offset, 3, &offsetlength); CLIParamHexToBuf(arg_get_str(ctx, 2), offset, 3, &offsetlength);
int dlength = 0xFFFF; // iceman: we only have a 1024 byte commandline input array. So this is pointlessly large.
uint8_t *data = (uint8_t *)calloc(dlength, sizeof(uint8_t)); // with 2char hex, 512bytes could be input.
if (data == NULL) { // Instead large binary inputs should be BINARY files and written to card.
PrintAndLogEx(ERR, "failed to allocate memory"); int dlength = 512;
CLIParserFree(ctx); uint8_t data[512] = {0};
return PM3_EMALLOC; CLIParamHexToBuf(arg_get_str(ctx, 3), data, 512, &dlength);
}
CLIParamHexToBuf(arg_get_str(ctx, 3), data, 0xFFFF, &dlength);
int type = arg_get_int(ctx, 4); int type = arg_get_int(ctx, 4);
int aidlength = 3; int aidlength = 3;
@ -2874,31 +2870,26 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
if (type < 0 || type > 1) { if (type < 0 || type > 1) {
PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)"); PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)");
if (data) free(data);
return PM3_EINVARG; return PM3_EINVARG;
} }
if (dlength == 0) { if (dlength == 0) {
PrintAndLogEx(ERR, "Data needs some hex bytes to write"); PrintAndLogEx(ERR, "Data needs some hex bytes to write");
if (data) free(data);
return PM3_EINVARG; return PM3_EINVARG;
} }
if (offsetlength != 3 && offsetlength != 0) { if (offsetlength != 3 && offsetlength != 0) {
PrintAndLogEx(ERR, "Offset needs 3 hex bytes"); PrintAndLogEx(ERR, "Offset needs 3 hex bytes");
if (data) free(data);
return PM3_EINVARG; return PM3_EINVARG;
} }
if (filenolen != 1) { if (filenolen != 1) {
PrintAndLogEx(ERR, "File number is missing"); PrintAndLogEx(ERR, "File number is missing");
if (data) free(data);
return PM3_EINVARG; return PM3_EINVARG;
} }
if (_fileno[0] > 0x1F) { if (_fileno[0] > 0x1F) {
PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)");
if (data) free(data);
return PM3_EINVARG; return PM3_EINVARG;
} }
@ -2921,25 +2912,22 @@ static int CmdHF14ADesWriteData(const char *Cmd) {
uint8_t cs = 0; uint8_t cs = 0;
if (selectfile(aid, _fileno[0], &cs) != PM3_SUCCESS) { if (selectfile(aid, _fileno[0], &cs) != PM3_SUCCESS) {
PrintAndLogEx(ERR, _RED_(" Error on selecting file.")); PrintAndLogEx(ERR, _RED_(" Error on selecting file."));
DropField();
return PM3_ESOFT; return PM3_ESOFT;
} }
int res = PM3_ESOFT; int res = PM3_ESOFT;
if (data != NULL) { ft.data = data;
ft.data = data; res = handler_desfire_writedata(&ft, type, cs);
res = handler_desfire_writedata(&ft, type, cs); if (res == PM3_SUCCESS) {
if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Successfully wrote data");
PrintAndLogEx(SUCCESS, "Successfully wrote data"); } else {
} else { PrintAndLogEx(ERR, "Couldn't read data. Error %d", res);
PrintAndLogEx(ERR, "Couldn't read data. Error %d", res);
}
free(data);
} }
DropField(); DropField();
return res; return res;
} }
static int CmdHF14ADesCreateRecordFile(const char *Cmd) { static int CmdHF14ADesCreateRecordFile(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes createrecordfile", CLIParserInit(&ctx, "hf mfdes createrecordfile",