In SpyReader, don't return pointers into main content string
This improves test coverage of the lexer. Before, a SpyReader's read function would return pointers into a single string that contained the entire text. This could have masked bugs where out-of-bounds characters were being read. Now the chunks returned by the reader are copied into a separate buffer.
This commit is contained in:
parent
f2e2102a25
commit
c576d7d4a0
2 changed files with 15 additions and 9 deletions
|
|
@ -5,23 +5,23 @@ using std::string;
|
|||
|
||||
static const char * spy_read(void *data, size_t *bytes_read) {
|
||||
SpyReader *reader = static_cast<SpyReader *>(data);
|
||||
size_t size = std::min(reader->chunk_size,
|
||||
reader->content.length() - reader->position);
|
||||
const char *result = reader->content.data() + reader->position;
|
||||
reader->strings_read.back() += string(result, size);
|
||||
reader->position += size;
|
||||
*bytes_read = size;
|
||||
return result;
|
||||
string result = reader->content.substr(reader->position, reader->chunk_size);
|
||||
reader->position += result.size();
|
||||
reader->strings_read.back() += result;
|
||||
*bytes_read = result.size();
|
||||
memcpy(reader->buffer, result.data(), result.size());
|
||||
return reader->buffer;
|
||||
}
|
||||
|
||||
static int spy_seek(void *data, size_t position) {
|
||||
static int spy_seek(void *data, TSLength position) {
|
||||
SpyReader *reader = static_cast<SpyReader *>(data);
|
||||
reader->strings_read.push_back("");
|
||||
reader->position = position;
|
||||
reader->position = position.bytes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SpyReader::SpyReader(string content, size_t chunk_size) :
|
||||
buffer(new char[chunk_size]),
|
||||
content(content),
|
||||
position(0),
|
||||
chunk_size(chunk_size),
|
||||
|
|
@ -35,3 +35,7 @@ SpyReader::SpyReader(string content, size_t chunk_size) :
|
|||
void SpyReader::clear() {
|
||||
strings_read.clear();
|
||||
}
|
||||
|
||||
SpyReader::~SpyReader() {
|
||||
delete buffer;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,11 @@
|
|||
class SpyReader {
|
||||
public:
|
||||
SpyReader(std::string content, size_t chunk_size);
|
||||
~SpyReader();
|
||||
|
||||
void clear();
|
||||
|
||||
char *buffer;
|
||||
std::string content;
|
||||
size_t position;
|
||||
size_t chunk_size;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue