]> sigrok.org Git - pulseview.git/commitdiff
inputfile: Use a vector instead of an array on the stack.
authorUwe Hermann <redacted>
Sat, 14 Apr 2018 20:20:43 +0000 (22:20 +0200)
committerUwe Hermann <redacted>
Sun, 15 Apr 2018 15:11:51 +0000 (17:11 +0200)
Increasing the chunk size could cause issues when trying to put
large arrays on the stack.

pv/devices/inputfile.cpp

index b866deb8245ed1d6051833f897efc2b7d59ee282..1bf34bad22a33bc0307199657bfe5ecf8dc9ef3d 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <cassert>
 #include <fstream>
+#include <vector>
 
 #include <QString>
 
@@ -30,6 +31,7 @@ using std::streamsize;
 using std::string;
 using std::ifstream;
 using std::ios;
+using std::vector;
 
 namespace pv {
 namespace devices {
@@ -64,13 +66,14 @@ void InputFile::open()
        // we can't open the device without sending some data first
        f = new ifstream(file_name_, ios::binary);
 
-       char buffer[BufferSize];
-       f->read(buffer, BufferSize);
+       vector<char> buffer(BufferSize);
+
+       f->read(buffer.data(), BufferSize);
        const streamsize size = f->gcount();
        if (size == 0)
                return;
 
-       input_->send(buffer, size);
+       input_->send(buffer.data(), size);
 
        try {
                device_ = input_->device();
@@ -93,22 +96,22 @@ void InputFile::start()
 
 void InputFile::run()
 {
-       char buffer[BufferSize];
-
        if (!f) {
                // Previous call to run() processed the entire file already
                f = new ifstream(file_name_, ios::binary);
                input_->reset();
        }
 
+       vector<char> buffer(BufferSize);
+
        interrupt_ = false;
        while (!interrupt_ && !f->eof()) {
-               f->read(buffer, BufferSize);
+               f->read(buffer.data(), BufferSize);
                const streamsize size = f->gcount();
                if (size == 0)
                        break;
 
-               input_->send(buffer, size);
+               input_->send(buffer.data(), size);
 
                if (size != BufferSize)
                        break;