Skip to content

Commit 3f2d742

Browse files
author
dashodanger
committed
Leak/ASAN fixes
1 parent ec8667c commit 3f2d742

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

tinyprimesynth.hpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ struct VersionTag {
546546
uint16_t minor;
547547
};
548548

549+
#pragma pack(push, 1)
549550
struct PresetHeader {
550551
char preset_name[20];
551552
uint16_t preset;
@@ -588,7 +589,9 @@ struct SF2Sample {
588589
int8_t original_key;
589590
int8_t correction;
590591
uint16_t sample_link;
592+
uint16_t sample_type;
591593
};
594+
#pragma pack(pop)
592595
class Envelope {
593596
public:
594597
enum class Phase {
@@ -1361,6 +1364,12 @@ class Synthesizer::SoundFont {
13611364
}
13621365
}
13631366

1367+
~SoundFont() {
1368+
for (const Preset *preset : presets) {
1369+
delete preset;
1370+
}
1371+
}
1372+
13641373
inline const std::vector<Sample> &get_samples() const {
13651374
return samples;
13661375
}
@@ -1454,16 +1463,16 @@ class Synthesizer::SoundFont {
14541463
}
14551464

14561465
template <typename T>
1457-
void read_pdta_list(FileAndMemReader *p_file, std::vector<T> &p_list, uint32_t p_total_size, size_t p_struct_size,
1458-
Synthesizer *p_synth) {
1459-
if (p_total_size % p_struct_size != 0) {
1466+
void read_pdta_list(FileAndMemReader *p_file, std::vector<T> &p_list, uint32_t p_total_size, Synthesizer *p_synth) {
1467+
if (p_total_size % sizeof(T) != 0) {
14601468
printf("invalid chunk size");
14611469
p_synth->set_load_error(true);
14621470
return;
14631471
}
1464-
p_list.resize(p_total_size / p_struct_size);
1465-
for (size_t i = 0; i < p_total_size / p_struct_size; ++i) {
1466-
p_file->read((char *)&p_list[i], 1, p_struct_size);
1472+
size_t num_members = p_total_size / sizeof(T);
1473+
p_list.resize(num_members);
1474+
for (size_t i = 0; i < num_members; ++i) {
1475+
p_file->read((char *)&p_list[i], 1, sizeof(T));
14671476
}
14681477
}
14691478

@@ -1480,31 +1489,31 @@ class Synthesizer::SoundFont {
14801489
s += sizeof(subchunk_header) + subchunk_header.size;
14811490
switch (subchunk_header.id) {
14821491
case to_four_cc("phdr"):
1483-
read_pdta_list(p_file, phdr, subchunk_header.size, 38, p_synth);
1492+
read_pdta_list(p_file, phdr, subchunk_header.size, p_synth);
14841493
break;
14851494
case to_four_cc("pbag"):
1486-
read_pdta_list(p_file, pbag, subchunk_header.size, 4, p_synth);
1495+
read_pdta_list(p_file, pbag, subchunk_header.size, p_synth);
14871496
break;
14881497
case to_four_cc("pmod"):
14891498
read_mod_list(p_file, pmod, subchunk_header.size, p_synth);
14901499
break;
14911500
case to_four_cc("pgen"):
1492-
read_pdta_list(p_file, pgen, subchunk_header.size, 4, p_synth);
1501+
read_pdta_list(p_file, pgen, subchunk_header.size, p_synth);
14931502
break;
14941503
case to_four_cc("inst"):
1495-
read_pdta_list(p_file, inst, subchunk_header.size, 22, p_synth);
1504+
read_pdta_list(p_file, inst, subchunk_header.size, p_synth);
14961505
break;
14971506
case to_four_cc("ibag"):
1498-
read_pdta_list(p_file, ibag, subchunk_header.size, 4, p_synth);
1507+
read_pdta_list(p_file, ibag, subchunk_header.size, p_synth);
14991508
break;
15001509
case to_four_cc("imod"):
15011510
read_mod_list(p_file, imod, subchunk_header.size, p_synth);
15021511
break;
15031512
case to_four_cc("igen"):
1504-
read_pdta_list(p_file, igen, subchunk_header.size, 4, p_synth);
1513+
read_pdta_list(p_file, igen, subchunk_header.size, p_synth);
15051514
break;
15061515
case to_four_cc("shdr"):
1507-
read_pdta_list(p_file, shdr, subchunk_header.size, 46, p_synth);
1516+
read_pdta_list(p_file, shdr, subchunk_header.size, p_synth);
15081517
break;
15091518
default:
15101519
p_file->seek(subchunk_header.size, SEEK_CUR);

0 commit comments

Comments
 (0)