summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/cassimg.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2025-01-31 03:29:36 +1100
committer Vas Crabb <vas@vastheman.com>2025-01-31 03:29:36 +1100
commitfbb446e8adfef79f5ca47b9b1f64bf72266c1e57 (patch)
treedbc38f8a0a97f3b85c1885d9162eae7d0802462c /src/lib/formats/cassimg.cpp
parent802bce33ebeb98d71a483112f9f7841ff1738f9b (diff)
Fix up some stuff:
* formats/cassimg.cpp: Put allocation outside loop again, check more allocations for failure. * pc8801_flop.xml: Actually mark clones as clones, transliterate title for S.F.3.D. * apple/macadb.cpp: Tidy a little.
Diffstat (limited to 'src/lib/formats/cassimg.cpp')
-rw-r--r--src/lib/formats/cassimg.cpp39
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)