More paranoid input checks, random error injection

Check for NULL input data buffers and set values to NULL after freeing()
local variables to avoid memory corruption on use-after-freee.

Add lots of error injection in the host testing to help find bugs on
the PC where they're easier to debug.
This commit is contained in:
Earle F. Philhower, III committed 2018-01-20 21:08:13 -08:00
1 parent f16cd2b269
commit d04beb1105
3 files changed
+27 -3

No files matched your search

+9 -1
View File
@@ -46,7 +46,7 @@ bool AudioFileSourceHTTPStream::open(const char *url)
return false;
}
size = http.getSize();
free(saveURL);
free(saveURL); saveURL = NULL;
saveURL = strdup(url);
return true;
}
@@ -58,11 +58,19 @@ AudioFileSourceHTTPStream::~AudioFileSourceHTTPStream()
uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len)
{
if (data==NULL) {
Serial.printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::read passed NULL data\n"));
return 0;
}
return readInternal(data, len, false);
}
uint32_t AudioFileSourceHTTPStream::readNonBlock(void *data, uint32_t len)
{
if (data==NULL) {
Serial.printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::readNonBlock passed NULL data\n"));
return 0;
}
return readInternal(data, len, true);
}
+1 -1
View File
@@ -56,7 +56,7 @@ bool AudioFileSourceICYStream::open(const char *url)
}
icyByteCount = 0;
size = http.getSize();
free(saveURL);
free(saveURL); saveURL = NULL;
saveURL = strdup(url);
return true;
}
+17 -1
View File
@@ -21,12 +21,14 @@
#include <Arduino.h>
#ifndef ARDUINO
#include <time.h>
#include "AudioFileSourceSTDIO.h"
AudioFileSourceSTDIO::AudioFileSourceSTDIO()
{
f = NULL;
srand(time(NULL));
}
AudioFileSourceSTDIO::AudioFileSourceSTDIO(const char *filename)
@@ -48,7 +50,21 @@ AudioFileSourceSTDIO::~AudioFileSourceSTDIO()
uint32_t AudioFileSourceSTDIO::read(void *data, uint32_t len)
{
return fread(reinterpret_cast<uint8_t*>(data), 1, len, f);
if (rand() % 100 == 69) { // Give 0 data 1%
printf("0 read\n");
len = 0;
} else if (rand() % 100 == 1) { // Give short reads 1%
printf("0 read\n");
len = 0;
}
int ret = fread(reinterpret_cast<uint8_t*>(data), 1, len, f);
if (ret && rand() % 100 < 5 ) {
// We're really mean...throw bad data in the mix
printf("bad data\n");
for (int i=0; i<100; i++)
*(reinterpret_cast<uint8_t*>(data) + (rand() % ret)) = rand();
}
return ret;
}
bool AudioFileSourceSTDIO::seek(int32_t pos, int dir)