Cleanup: use a vector instead of a raw array.
authorCarl Hetherington <cth@carlh.net>
Sun, 3 Apr 2022 22:24:26 +0000 (00:24 +0200)
committerCarl Hetherington <cth@carlh.net>
Thu, 7 Apr 2022 20:13:11 +0000 (22:13 +0200)
src/lib/ext.cc

index af5229f72f41c02941f6905911eb34495be91d61..1af6c137bf3f2945d22a0655579dec340a564eaa 100644 (file)
@@ -119,7 +119,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
        }
 
-       uint8_t* buffer = new uint8_t[block_size];
+       std::vector<uint8_t> buffer(block_size);
        Digester digester;
 
        int progress_frequency = 1;
@@ -127,28 +127,25 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
        uint64_t remaining = file_size (from);
        while (remaining > 0) {
                uint64_t const this_time = min(remaining, block_size);
-               size_t read = fread (buffer, 1, this_time, in);
+               size_t read = fread (buffer.data(), 1, this_time, in);
                if (read != this_time) {
                        fclose (in);
                        ext4_fclose (&out);
-                       delete[] buffer;
                        throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
                }
 
-               digester.add (buffer, this_time);
+               digester.add (buffer.data(), this_time);
 
                size_t written;
-               r = ext4_fwrite (&out, buffer, this_time, &written);
+               r = ext4_fwrite (&out, buffer.data(), this_time, &written);
                if (r != EOK) {
                        fclose (in);
                        ext4_fclose (&out);
-                       delete[] buffer;
                        throw CopyError ("Write failed", r);
                }
                if (written != this_time) {
                        fclose (in);
                        ext4_fclose (&out);
-                       delete[] buffer;
                        throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
                }
                remaining -= this_time;
@@ -162,7 +159,6 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
 
        fclose (in);
        ext4_fclose (&out);
-       delete[] buffer;
 
        set_timestamps_to_now (to);
 
@@ -182,21 +178,20 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_
        }
        LOG_DISK("Opened %1 for read", to.generic_string());
 
-       uint8_t* buffer = new uint8_t[block_size];
+       std::vector<uint8_t> buffer(block_size);
        Digester digester;
 
        uint64_t remaining = file_size (from);
        while (remaining > 0) {
                uint64_t const this_time = min(remaining, block_size);
                size_t read;
-               r = ext4_fread (&in, buffer, this_time, &read);
+               r = ext4_fread (&in, buffer.data(), this_time, &read);
                if (read != this_time) {
                        ext4_fclose (&in);
-                       delete[] buffer;
                        throw VerifyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
                }
 
-               digester.add (buffer, this_time);
+               digester.add (buffer.data(), this_time);
                remaining -= this_time;
                total_remaining -= this_time;
                if (nanomsg) {
@@ -205,7 +200,6 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_
        }
 
        ext4_fclose (&in);
-       delete[] buffer;
 
        return digester.get ();
 }