Skip to content

Commit d522165

Browse files
author
Ron
committed
Check memory allocations for success
Adds some missing checks spotted by eye in a visual review while looking into the details of https://bugs.debian.org/870608
1 parent df53ae0 commit d522165

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

src/audio_out.c

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ static char *_sanitize_matrix(int maxchannels, char *matrix, ao_device *device){
634634
char *ret = calloc(strlen(matrix)+1,1); /* can only get smaller */
635635
char *p=matrix;
636636
int count=0;
637+
638+
if(!ret)
639+
return NULL;
640+
637641
while(count<maxchannels){
638642
char *h,*t;
639643
int m=0;
@@ -706,6 +710,15 @@ static int _find_channel(int needle, char *haystack){
706710
return -1;
707711
}
708712

713+
static void _free_map(char **m){
714+
char **in=m;
715+
while(m && *m){
716+
free(*m);
717+
m++;
718+
}
719+
if(in)free(in);
720+
}
721+
709722
static char **_tokenize_matrix(char *matrix){
710723
char **ret=NULL;
711724
char *p=matrix;
@@ -730,6 +743,8 @@ static char **_tokenize_matrix(char *matrix){
730743
}
731744

732745
ret = calloc(count+1,sizeof(*ret));
746+
if(!ret)
747+
return NULL;
733748

734749
p=matrix;
735750
count=0;
@@ -748,31 +763,32 @@ static char **_tokenize_matrix(char *matrix){
748763
while(t>p && isspace(*(t-1)))t--;
749764

750765
ret[count] = calloc(t-p+1,1);
766+
if(!ret[count]){
767+
_free_map(ret);
768+
return NULL;
769+
}
751770
memcpy(ret[count],p,t-p);
752771
count++;
753772
if(!*h)break;
754773
p=h+1;
755774
}
756775

757776
return ret;
758-
759-
}
760-
761-
static void _free_map(char **m){
762-
char **in=m;
763-
while(m && *m){
764-
free(*m);
765-
m++;
766-
}
767-
if(in)free(in);
768777
}
769778

770779
static unsigned int _matrix_to_channelmask(int ch, char *matrix, char *premap, int **mout){
771780
unsigned int ret=0;
772781
char *p=matrix;
773782
int *perm=(*mout=malloc(ch*sizeof(*mout)));
774783
int i;
775-
char **map = _tokenize_matrix(premap);
784+
char **map;
785+
786+
if(!perm)
787+
return 0;
788+
789+
map = _tokenize_matrix(premap);
790+
if(!map)
791+
return 0;
776792

777793
for(i=0;i<ch;i++) perm[i] = -1;
778794
i=0;
@@ -810,6 +826,9 @@ static char *_channelmask_to_matrix(unsigned int mask, char *premap){
810826
char buffer[257]={0};
811827
char **map = _tokenize_matrix(premap);
812828

829+
if(!map)
830+
return NULL;
831+
813832
while(map[m]){
814833
if(mask & (1<<m)){
815834
if(count)
@@ -849,6 +868,9 @@ static char *_matrix_intersect(char *matrix,char *premap){
849868
int count=0;
850869
char **map = _tokenize_matrix(premap);
851870

871+
if(!map)
872+
return NULL;
873+
852874
while(1){
853875
char *h=p;
854876
int m=0;
@@ -1039,7 +1061,7 @@ static ao_device* _open_device(int driver_id, ao_sample_format *format,
10391061
device->output_matrix,
10401062
&device->input_map);
10411063
int channels = _channelmask_bits(mask);
1042-
if(channels<0){
1064+
if(channels<=0){
10431065
aerror("Unable to map any channels from input matrix to output");
10441066
errno = AO_EBADFORMAT;
10451067
goto error;
@@ -1060,7 +1082,7 @@ static ao_device* _open_device(int driver_id, ao_sample_format *format,
10601082
device->output_matrix,
10611083
&device->input_map);
10621084
int channels = _channelmask_bits(mask);
1063-
if(channels<0){
1085+
if(channels<=0){
10641086
aerror("Unable to map any channels from input matrix to output");
10651087
errno = AO_EBADFORMAT;
10661088
goto error;
@@ -1111,6 +1133,10 @@ static ao_device* _open_device(int driver_id, ao_sample_format *format,
11111133
int count=0;
11121134
device->inter_permute = calloc(device->output_channels,sizeof(int));
11131135

1136+
if (!device->inter_permute) {
1137+
errno = AO_EFAIL;
1138+
goto error;
1139+
}
11141140
adebug("\n");
11151141

11161142
while(count<device->output_channels){
@@ -1157,8 +1183,10 @@ static ao_device* _open_device(int driver_id, ao_sample_format *format,
11571183
for(i=0;i<device->output_channels;i++)
11581184
if(device->inter_permute[i]==j)break;
11591185
if(i==device->output_channels){
1160-
adebug("input %d (%s)\t -> none\n",
1161-
j,inch[j]);
1186+
if(inch){
1187+
adebug("input %d (%s)\t -> none\n",
1188+
j,inch[j]);
1189+
}
11621190
unflag=1;
11631191
}
11641192
}

src/plugins/macosx/ao_macosx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,11 @@ int ao_plugin_open(ao_device *device, ao_sample_format *format)
594594
internal->firstValidByteOffset = 0;
595595
internal->validByteCount = 0;
596596
internal->buffer = malloc(internal->bufferByteCount);
597-
memset(internal->buffer, 0, internal->bufferByteCount);
598597
if (!internal->buffer) {
599598
aerror("Unable to allocate queue buffer.\n");
600599
return 0;
601600
}
601+
memset(internal->buffer, 0, internal->bufferByteCount);
602602

603603
/* limited to stereo for now */
604604
//if(!device->output_matrix)

src/plugins/sndio/ao_sndio.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ int ao_plugin_device_init(ao_device *device)
6767
{
6868
ao_sndio_internal *internal;
6969
internal = (ao_sndio_internal *) calloc(1,sizeof(*internal));
70+
if (internal == NULL)
71+
return 0;
72+
7073
internal->id=-1;
7174
device->internal = internal;
7275
device->output_matrix_order = AO_OUTPUT_MATRIX_FIXED;

0 commit comments

Comments
 (0)