1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-19 14:25:11 +03:00

[Horus] Malloc check added

This commit is contained in:
Bertrand Songis 2017-01-22 21:32:19 +01:00
parent 05129b6785
commit fb6c9cde6b
2 changed files with 53 additions and 30 deletions

View file

@ -133,6 +133,11 @@ int cliRead(const char ** argv)
}
uint8_t * buffer = (uint8_t*) malloc(bufferSize);
if (!buffer) {
serialPrint("Not enough memory");
return 0;
}
FRESULT result = f_open(&file, argv[1], FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK) {
free(buffer);
@ -177,11 +182,15 @@ int cliReadSD(const char ** argv)
}
if (toInt(argv, 3, &bufferSectors) == 0 || bufferSectors < 0 ) {
serialPrint("%s: Invalid number of buffrer sectors \"%s\"", argv[0], argv[3]);
serialPrint("%s: Invalid number of buffer sectors \"%s\"", argv[0], argv[3]);
return 0;
}
uint8_t * buffer = (uint8_t*) malloc(512*bufferSectors);
if (!buffer) {
serialPrint("Not enough memory");
return 0;
}
uint32_t bytesRead = numberOfSectors * 512;
tmr10ms_t start = get_tmr10ms();
@ -228,15 +237,19 @@ int cliTestSD(const char ** argv)
// get sector count
uint32_t sectorCount;
if (disk_ioctl(0, GET_SECTOR_COUNT, &sectorCount) != RES_OK) {
serialPrint("Error: can't read sector count");
return 0;
serialPrint("Error: can't read sector count");
return 0;
}
serialPrint("SD card has %u sectors", sectorCount);
// read last 16 sectors one sector at the time
serialPrint("Starting single sector read test, reading 16 sectors one by one");
uint8_t * buffer = (uint8_t*) malloc(512);
for(uint32_t s = sectorCount - 16; s<sectorCount; ++s) {
if (!buffer) {
serialPrint("Not enough memory");
return 0;
}
for (uint32_t s = sectorCount - 16; s<sectorCount; ++s) {
DRESULT res = __disk_read(0, buffer, s, 1);
if (res != RES_OK) {
serialPrint("sector %d read FAILED, err: %d", s, res);
@ -249,9 +262,14 @@ int cliTestSD(const char ** argv)
serialCrlf();
// read last 16 sectors, two sectors at the time with a multi-block read
buffer = (uint8_t*) malloc(512*2);
buffer = (uint8_t *) malloc(512*2);
if (!buffer) {
serialPrint("Not enough memory");
return 0;
}
serialPrint("Starting multiple sector read test, reading two sectors at the time");
for(uint32_t s = sectorCount - 16; s<sectorCount; s+=2) {
for (uint32_t s = sectorCount - 16; s<sectorCount; s+=2) {
DRESULT res = __disk_read(0, buffer, s, 2);
if (res != RES_OK) {
serialPrint("sector %d-%d read FAILED, err: %d", s, s+1, res);
@ -265,6 +283,11 @@ int cliTestSD(const char ** argv)
// read last 16 sectors, all sectors with single multi-block read
buffer = (uint8_t*) malloc(512*16);
if (!buffer) {
serialPrint("Not enough memory");
return 0;
}
serialPrint("Starting multiple sector read test, reading 16 sectors at the time");
DRESULT res = __disk_read(0, buffer, sectorCount-16, 16);
if (res != RES_OK) {

View file

@ -1,23 +1,23 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* \file syscalls.c
*
@ -54,17 +54,17 @@ extern void _exit( int status ) ;
extern void _kill( int pid, int sig ) ;
extern int _getpid ( void ) ;
unsigned char *heap = (unsigned char *)&_end;
unsigned char * heap = (unsigned char *)&_end;
extern caddr_t _sbrk(int nbytes)
{
if (heap + nbytes < (unsigned char *)&_heap_end) {
unsigned char *prev_heap = heap;
unsigned char * prev_heap = heap;
heap += nbytes;
return (caddr_t) prev_heap;
return (caddr_t)prev_heap;
}
else {
errno = ENOMEM;
return ((void*)-1);
return ((void *)-1);
}
}