Browse Source

AU: Avoid a crash when calling setCurrentProgram() with an out-of-bounds index

tags/2021-05-28
ed 4 years ago
parent
commit
f2fffe6757
1 changed files with 40 additions and 44 deletions
  1. +40
    -44
      modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm

+ 40
- 44
modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm View File

@@ -1213,23 +1213,33 @@ public:
} }
//============================================================================== //==============================================================================
int getNumPrograms() override
struct ScopedFactoryPresets
{ {
CFArrayRef presets;
UInt32 sz = sizeof (CFArrayRef);
int num = 0;
ScopedFactoryPresets (AudioUnit& au)
{
UInt32 sz = sizeof (CFArrayRef);
AudioUnitGetProperty (au, kAudioUnitProperty_FactoryPresets,
kAudioUnitScope_Global, 0, &presets, &sz);
}
if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
~ScopedFactoryPresets()
{ {
if (presets != nullptr) if (presets != nullptr)
{
num = (int) CFArrayGetCount (presets);
CFRelease (presets); CFRelease (presets);
}
} }
return num;
CFArrayRef presets = nullptr;
};
int getNumPrograms() override
{
ScopedFactoryPresets factoryPresets { audioUnit };
if (factoryPresets.presets != nullptr)
return (int) CFArrayGetCount (factoryPresets.presets);
return 0;
} }
int getCurrentProgram() override int getCurrentProgram() override
@@ -1246,33 +1256,26 @@ public:
void setCurrentProgram (int newIndex) override void setCurrentProgram (int newIndex) override
{ {
AUPreset current;
current.presetNumber = newIndex;
CFArrayRef presets;
UInt32 sz = sizeof (CFArrayRef);
ScopedFactoryPresets factoryPresets { audioUnit };
if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
if (factoryPresets.presets != nullptr
&& newIndex < (int) CFArrayGetCount (factoryPresets.presets))
{ {
if (auto* p = (const AUPreset*) CFArrayGetValueAtIndex (presets, newIndex))
current.presetName = p->presetName;
AUPreset current;
current.presetNumber = newIndex;
CFRelease (presets);
}
if (auto* p = static_cast<const AUPreset*> (CFArrayGetValueAtIndex (factoryPresets.presets, newIndex)))
current.presetName = p->presetName;
AudioUnitSetProperty (audioUnit, kAudioUnitProperty_PresentPreset,
kAudioUnitScope_Global, 0, &current, sizeof (AUPreset));
AudioUnitSetProperty (audioUnit, kAudioUnitProperty_PresentPreset,
kAudioUnitScope_Global, 0, &current, sizeof (AUPreset));
sendAllParametersChangedEvents();
sendAllParametersChangedEvents();
}
} }
const String getProgramName (int index) override const String getProgramName (int index) override
{ {
String s;
CFArrayRef presets;
UInt32 sz = sizeof (CFArrayRef);
if (index == -1) if (index == -1)
{ {
AUPreset current; AUPreset current;
@@ -1284,27 +1287,20 @@ public:
AudioUnitGetProperty (audioUnit, kAudioUnitProperty_PresentPreset, AudioUnitGetProperty (audioUnit, kAudioUnitProperty_PresentPreset,
kAudioUnitScope_Global, 0, &current, &prstsz); kAudioUnitScope_Global, 0, &current, &prstsz);
s = String::fromCFString (current.presetName);
return String::fromCFString (current.presetName);
} }
else if (AudioUnitGetProperty (audioUnit, kAudioUnitProperty_FactoryPresets,
kAudioUnitScope_Global, 0, &presets, &sz) == noErr)
ScopedFactoryPresets factoryPresets { audioUnit };
if (factoryPresets.presets != nullptr)
{ {
for (CFIndex i = 0; i < CFArrayGetCount (presets); ++i)
{
if (auto* p = (const AUPreset*) CFArrayGetValueAtIndex (presets, i))
{
for (CFIndex i = 0; i < CFArrayGetCount (factoryPresets.presets); ++i)
if (auto* p = static_cast<const AUPreset*> (CFArrayGetValueAtIndex (factoryPresets.presets, i)))
if (p->presetNumber == index) if (p->presetNumber == index)
{
s = String::fromCFString (p->presetName);
break;
}
}
}
CFRelease (presets);
return String::fromCFString (p->presetName);
} }
return s;
return {};
} }
void changeProgramName (int /*index*/, const String& /*newName*/) override void changeProgramName (int /*index*/, const String& /*newName*/) override


Loading…
Cancel
Save