diff options
Diffstat (limited to 'src/lib/formats/cassimg.cpp')
-rw-r--r-- | src/lib/formats/cassimg.cpp | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/lib/formats/cassimg.cpp b/src/lib/formats/cassimg.cpp index 599b2698a81..fc953c2bdf2 100644 --- a/src/lib/formats/cassimg.cpp +++ b/src/lib/formats/cassimg.cpp @@ -791,7 +791,8 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l error err; int length; int sample_count; - std::vector<int16_t> samples; + std::unique_ptr<int16_t []> samples; + std::unique_ptr<uint8_t []> chunk; int pos = 0; uint64_t offset = 0; @@ -814,15 +815,21 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l /* determine number of samples */ if (args.chunk_sample_calc != nullptr) { - if (size > 0x7FFFFFFF) + if (size > 0x7fffffff) { err = error::OUT_OF_MEMORY; goto done; } - std::vector<uint8_t> bytes(size); - image_read(&bytes[0], 0, size); - sample_count = args.chunk_sample_calc(&bytes[0], (int)size); + std::unique_ptr<uint8_t []> bytes(new (std::nothrow) uint8_t [size]); + if (!bytes) + { + err = error::OUT_OF_MEMORY; + goto done; + } + + image_read(bytes.get(), 0, size); + sample_count = args.chunk_sample_calc(bytes.get(), (int)size); // chunk_sample_calc functions report errors by returning negative numbers if (sample_count < 0) @@ -842,7 +849,12 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l sample_count += args.header_samples + args.trailer_samples; /* allocate a buffer for the completed samples */ - samples.resize(sample_count); + samples.reset(new (std::nothrow) int16_t [sample_count]); + if (!samples) + { + err = error::OUT_OF_MEMORY; + goto done; + } /* if there has to be a header */ if (args.header_samples > 0) @@ -857,11 +869,15 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l } /* convert the file data to samples */ + chunk.reset(new (std::nothrow) uint8_t [args.chunk_size]); + if (!chunk) + { + err = error::OUT_OF_MEMORY; + goto done; + } while ((pos < sample_count) && (offset < size)) { - /* allocate a buffer for the binary data */ - std::vector<uint8_t> chunk(args.chunk_size); - image_read(&chunk[0], offset, args.chunk_size); + image_read(chunk.get(), offset, args.chunk_size); offset += args.chunk_size; /* @@ -871,10 +887,10 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l without knowing how much data available in the image. Having wrong header with size bigger than image couses illegal access beyond image data. Desired state is: - length = args.fill_wave(&samples[pos], args.chunk_size, &chunk[0]); + length = args.fill_wave(&samples[pos], args.chunk_size, chunk.get()); aslo the fix for tap is commented out in 'tap_cas_fill_wave' */ - length = args.fill_wave(&samples[pos], sample_count - pos, &chunk[0]); + length = args.fill_wave(&samples[pos], sample_count - pos, chunk.get()); if (length < 0) { err = error::INVALID_IMAGE; @@ -884,6 +900,7 @@ cassette_image::error cassette_image::legacy_construct(const LegacyWaveFiller *l if (length == 0) break; } + chunk.reset(); /* if there has to be a trailer */ if (args.trailer_samples > 0) |