1
1
/*
2
- * Copyright (c) 2003, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -124,9 +124,12 @@ OSStatus ChangeListenerProc(AudioObjectID inObjectID, UInt32 inNumberAddresses,
124
124
kAudioHardwarePropertyDevices , &size);
125
125
if (err == noErr ) {
126
126
int count = size/sizeof (AudioDeviceID);
127
- AudioDeviceID devices[count];
127
+ AudioDeviceID *devices = (AudioDeviceID *) malloc (size);
128
+ if (!devices) {
129
+ break ;
130
+ }
128
131
err = GetAudioObjectProperty (kAudioObjectSystemObject , kAudioObjectPropertyScopeGlobal ,
129
- kAudioHardwarePropertyDevices , count* sizeof (AudioDeviceID) , devices, 1 );
132
+ kAudioHardwarePropertyDevices , size , devices, 1 );
130
133
if (err == noErr ) {
131
134
bool found = false ;
132
135
for (int j = 0 ; j < count; j++) {
@@ -139,6 +142,7 @@ OSStatus ChangeListenerProc(AudioObjectID inObjectID, UInt32 inNumberAddresses,
139
142
invalid = true ;
140
143
}
141
144
}
145
+ free (devices);
142
146
}
143
147
break ;
144
148
case kAudioObjectPropertyOwnedObjects :
@@ -148,9 +152,12 @@ OSStatus ChangeListenerProc(AudioObjectID inObjectID, UInt32 inNumberAddresses,
148
152
kAudioObjectPropertyOwnedObjects , &size);
149
153
if (err == noErr ) {
150
154
int count = size / sizeof (AudioObjectID);
151
- AudioObjectID controlIDs[count];
155
+ AudioObjectID *controlIDs = (AudioObjectID *) malloc (size);
156
+ if (!controlIDs) {
157
+ break ;
158
+ }
152
159
err = GetAudioObjectProperty (mixer->deviceID , kAudioObjectPropertyScopeGlobal ,
153
- kAudioObjectPropertyOwnedObjects , count * sizeof (AudioObjectID), & controlIDs, 1 );
160
+ kAudioObjectPropertyOwnedObjects , size, controlIDs, 1 );
154
161
if (err == noErr ) {
155
162
for (PortControl *ctrl = mixer->portControls ; ctrl != NULL ; ctrl = ctrl->next ) {
156
163
for (int i = 0 ; i < ctrl->controlCount ; i++) {
@@ -168,6 +175,7 @@ OSStatus ChangeListenerProc(AudioObjectID inObjectID, UInt32 inNumberAddresses,
168
175
}
169
176
}
170
177
}
178
+ free (controlIDs);
171
179
}
172
180
}
173
181
}
@@ -229,6 +237,9 @@ INT32 PORT_GetPortMixerDescription(INT32 mixerIndex, PortMixerDescription* mixer
229
237
void * PORT_Open (INT32 mixerIndex) {
230
238
TRACE1 (" \n >>PORT_Open (mixerIndex=%d)\n " , (int )mixerIndex);
231
239
PortMixer *mixer = (PortMixer *)calloc (1 , sizeof (PortMixer));
240
+ if (!mixer) {
241
+ return nullptr ;
242
+ }
232
243
233
244
mixer->deviceID = deviceCache.GetDeviceID (mixerIndex);
234
245
if (mixer->deviceID != 0 ) {
@@ -480,16 +491,20 @@ void PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
480
491
mixer->deviceControlCount = size / sizeof (AudioObjectID);
481
492
TRACE1 (" PORT_GetControls: detected %d owned objects\n " , mixer->deviceControlCount );
482
493
483
- AudioObjectID controlIDs[mixer->deviceControlCount ];
494
+ AudioObjectID *controlIDs = (AudioObjectID *) malloc (size);
495
+ if (!controlIDs) {
496
+ return ;
497
+ }
484
498
485
499
err = GetAudioObjectProperty (mixer->deviceID , kAudioObjectPropertyScopeGlobal ,
486
- kAudioObjectPropertyOwnedObjects , sizeof (controlIDs) , controlIDs, 1 );
500
+ kAudioObjectPropertyOwnedObjects , size , controlIDs, 1 );
487
501
488
502
if (err) {
489
503
OS_ERROR1 (err, " PORT_GetControls (portIndex = %d) get OwnedObject values" , portIndex);
490
504
} else {
491
505
mixer->deviceControls = (AudioControl *)calloc (mixer->deviceControlCount , sizeof (AudioControl));
492
506
if (mixer->deviceControls == NULL ) {
507
+ free (controlIDs);
493
508
return ;
494
509
}
495
510
@@ -513,6 +528,7 @@ void PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
513
528
control->controlID , FourCC2Str (control->classID ), FourCC2Str (control->scope ), control->channel );
514
529
}
515
530
}
531
+ free (controlIDs);
516
532
}
517
533
}
518
534
@@ -524,10 +540,14 @@ void PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
524
540
int totalChannels = GetChannelCount (mixer->deviceID , port->scope == kAudioDevicePropertyScopeOutput ? 1 : 0 );
525
541
526
542
// collect volume and mute controls
527
- AudioControl* volumeControls[totalChannels+1 ]; // 0 - for master channel
528
- memset (&volumeControls, 0 , sizeof (AudioControl *) * (totalChannels+1 ));
529
- AudioControl* muteControls[totalChannels+1 ]; // 0 - for master channel
530
- memset (&muteControls, 0 , sizeof (AudioControl *) * (totalChannels+1 ));
543
+ size_t totalAndMaster = totalChannels + 1 ; // 0 - for master channel
544
+ AudioControl **volumeControls = (AudioControl **) calloc (totalAndMaster, sizeof (AudioControl *));
545
+ AudioControl **muteControls = (AudioControl **) calloc (totalAndMaster, sizeof (AudioControl *));
546
+ if (!volumeControls || !muteControls) {
547
+ free (muteControls);
548
+ free (volumeControls);
549
+ return ;
550
+ }
531
551
532
552
for (int i=0 ; i<mixer->deviceControlCount ; i++) {
533
553
AudioControl *control = &mixer->deviceControls [i];
@@ -626,13 +646,17 @@ void PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
626
646
CFIndex length = CFStringGetLength (cfname) + 1 ;
627
647
channelName = (char *)malloc (length);
628
648
if (channelName == NULL ) {
649
+ free (muteControls);
650
+ free (volumeControls);
629
651
return ;
630
652
}
631
653
CFStringGetCString (cfname, channelName, length, kCFStringEncodingUTF8 );
632
654
CFRelease (cfname);
633
655
} else {
634
656
channelName = (char *)malloc (16 );
635
657
if (channelName == NULL ) {
658
+ free (muteControls);
659
+ free (volumeControls);
636
660
return ;
637
661
}
638
662
snprintf (channelName, 16 , " Ch %d" , ch);
@@ -657,7 +681,8 @@ void PORT_GetControls(void* id, INT32 portIndex, PortControlCreator* creator) {
657
681
}
658
682
659
683
AddChangeListeners (mixer);
660
-
684
+ free (muteControls);
685
+ free (volumeControls);
661
686
TRACE1 (" <<PORT_GetControls (portIndex = %d)\n " , portIndex);
662
687
}
663
688
@@ -772,28 +797,35 @@ float PORT_GetFloatValue(void* controlIDV) {
772
797
PortControl *control = (PortControl *)controlIDV;
773
798
Float32 result = 0 ;
774
799
775
- Float32 subVolumes[control->controlCount ];
800
+ Float32 *subVolumes = (Float32 *) malloc (control->controlCount * sizeof (Float32 ));
801
+ if (!subVolumes) {
802
+ return DEFAULT_VOLUME_VALUE;
803
+ }
776
804
Float32 maxVolume;
777
805
778
806
switch (control->type ) {
779
807
case PortControl::Volume:
780
808
if (!TestPortControlValidity (control)) {
781
- return DEFAULT_VOLUME_VALUE;
809
+ result = DEFAULT_VOLUME_VALUE;
810
+ break ;
782
811
}
783
812
784
813
if (!GetPortControlVolumes (control, subVolumes, &maxVolume)) {
785
- return DEFAULT_VOLUME_VALUE;
814
+ result = DEFAULT_VOLUME_VALUE;
815
+ break ;
786
816
}
787
817
result = maxVolume;
788
818
break ;
789
819
case PortControl::Balance:
790
820
if (!TestPortControlValidity (control)) {
791
- return DEFAULT_BALANCE_VALUE;
821
+ result = DEFAULT_BALANCE_VALUE;
822
+ break ;
792
823
}
793
824
794
825
// balance control always has 2 volume controls
795
826
if (!GetPortControlVolumes (control, subVolumes, &maxVolume)) {
796
- return DEFAULT_VOLUME_VALUE;
827
+ result = DEFAULT_VOLUME_VALUE;
828
+ break ;
797
829
}
798
830
// calculate balance value
799
831
if (subVolumes[0 ] > subVolumes[1 ]) {
@@ -806,9 +838,10 @@ float PORT_GetFloatValue(void* controlIDV) {
806
838
break ;
807
839
default :
808
840
ERROR1 (" GetFloatValue requested for non-Float control (control-type == %d)\n " , control->type );
809
- return 0 ;
841
+ result = 0 ;
842
+ break ;
810
843
}
811
-
844
+ free (subVolumes);
812
845
TRACE1 (" <<PORT_GetFloatValue = %f\n " , result);
813
846
return result;
814
847
}
@@ -821,13 +854,16 @@ void PORT_SetFloatValue(void* controlIDV, float value) {
821
854
return ;
822
855
}
823
856
824
- Float32 subVolumes[control->controlCount ];
857
+ Float32 *subVolumes = (Float32 *) malloc (sizeof (Float32 ) * control->controlCount );
858
+ if (!subVolumes) {
859
+ return ;
860
+ }
825
861
Float32 maxVolume;
826
862
827
863
switch (control->type ) {
828
864
case PortControl::Volume:
829
865
if (!GetPortControlVolumes (control, subVolumes, &maxVolume)) {
830
- return ;
866
+ break ;
831
867
}
832
868
// update the values
833
869
if (maxVolume > 0.001 ) {
@@ -845,7 +881,7 @@ void PORT_SetFloatValue(void* controlIDV, float value) {
845
881
case PortControl::Balance:
846
882
// balance control always has 2 volume controls
847
883
if (!GetPortControlVolumes (control, subVolumes, &maxVolume)) {
848
- return ;
884
+ break ;
849
885
}
850
886
// calculate new values
851
887
if (value < 0 .0f ) {
@@ -861,8 +897,9 @@ void PORT_SetFloatValue(void* controlIDV, float value) {
861
897
break ;
862
898
default :
863
899
ERROR1 (" PORT_SetFloatValue requested for non-Float control (control-type == %d)\n " , control->type );
864
- return ;
900
+ break ;
865
901
}
902
+ free (subVolumes);
866
903
}
867
904
868
905
#endif // USE_PORTS
0 commit comments