From d91948db49efb1b437f4e17f925fff9e7a64dc07 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Wed, 25 Feb 2004 11:24:29 +0000 Subject: [PATCH] - check the return value of malloc & co. --- src/conf.c | 8 ++++++++ src/pcm/pcm_adpcm.c | 2 ++ src/pcm/pcm_direct.c | 2 ++ src/pcm/pcm_ladspa.c | 6 ++++++ src/pcm/pcm_multi.c | 16 ++++++++++++++++ src/pcm/pcm_share.c | 4 ++++ src/pcm/pcm_shm.c | 4 ++++ 7 files changed, 42 insertions(+) diff --git a/src/conf.c b/src/conf.c index 7c5f9935..6c142197 100644 --- a/src/conf.c +++ b/src/conf.c @@ -2642,6 +2642,10 @@ static int snd_config_hooks_call(snd_config_t *root, snd_config_t *config, snd_c if (!func_name) { int len = 16 + strlen(str) + 1; buf = malloc(len); + if (! buf) { + err = -ENOMEM; + goto _err; + } snprintf(buf, len, "snd_config_hook_%s", str); buf[len-1] = '\0'; func_name = buf; @@ -3423,6 +3427,10 @@ static int _snd_config_evaluate(snd_config_t *src, if (!func_name) { int len = 9 + strlen(str) + 1; buf = malloc(len); + if (! buf) { + err = -ENOMEM; + goto _err; + } snprintf(buf, len, "snd_func_%s", str); buf[len-1] = '\0'; func_name = buf; diff --git a/src/pcm/pcm_adpcm.c b/src/pcm/pcm_adpcm.c index 235163a0..1b824a9f 100644 --- a/src/pcm/pcm_adpcm.c +++ b/src/pcm/pcm_adpcm.c @@ -439,6 +439,8 @@ static int snd_pcm_adpcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params) } assert(!adpcm->states); adpcm->states = malloc(adpcm->plug.slave->channels * sizeof(*adpcm->states)); + if (adpcm->states == NULL) + return -ENOMEM; return 0; } diff --git a/src/pcm/pcm_direct.c b/src/pcm/pcm_direct.c index 33c3e6dd..3b748d36 100644 --- a/src/pcm/pcm_direct.c +++ b/src/pcm/pcm_direct.c @@ -923,6 +923,8 @@ int snd_pcm_direct_parse_bindings(snd_pcm_direct_t *dmix, snd_config_t *cfg) return -EINVAL; } dmix->bindings = malloc(count * sizeof(unsigned int)); + if (dmix->bindings == NULL) + return -ENOMEM; for (chn = 0; chn < count; chn++) dmix->bindings[chn] = UINT_MAX; /* don't route */ snd_config_for_each(i, next, cfg) { diff --git a/src/pcm/pcm_ladspa.c b/src/pcm/pcm_ladspa.c index 6051a0de..3b80225d 100644 --- a/src/pcm/pcm_ladspa.c +++ b/src/pcm/pcm_ladspa.c @@ -771,6 +771,8 @@ static int snd_pcm_ladspa_check_dir(snd_pcm_ladspa_plugin_t * const plugin, } filename = malloc(len + strlen(dirent->d_name) + 1 + need_slash); + if (filename == NULL) + return -ENOMEM; strcpy(filename, path); if (need_slash) strcat(filename, "/"); @@ -880,6 +882,8 @@ static int snd_pcm_ladspa_parse_ioconfig(snd_pcm_ladspa_plugin_t *lplug, } if (count > 0) { array = (unsigned int *)calloc(count, sizeof(unsigned int)); + if (! array) + return -ENOMEM; memset(array, 0xff, count * sizeof(unsigned int)); io->port_bindings_size = count; io->port_bindings = array; @@ -928,6 +932,8 @@ static int snd_pcm_ladspa_parse_ioconfig(snd_pcm_ladspa_plugin_t *lplug, if ((lplug->desc->PortDescriptors[idx] & (io->pdesc | LADSPA_PORT_CONTROL)) == (io->pdesc | LADSPA_PORT_CONTROL)) count++; array = (LADSPA_Data *)calloc(count, sizeof(LADSPA_Data)); + if (!array) + return -ENOMEM; io->controls_size = count; io->controls = array; snd_config_for_each(i, next, controls) { diff --git a/src/pcm/pcm_multi.c b/src/pcm/pcm_multi.c index 2a530bf5..0f047ecf 100644 --- a/src/pcm/pcm_multi.c +++ b/src/pcm/pcm_multi.c @@ -696,8 +696,17 @@ int snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name, multi->slaves_count = slaves_count; multi->master_slave = master_slave; multi->slaves = calloc(slaves_count, sizeof(*multi->slaves)); + if (!multi->slaves) { + free(multi); + return -ENOMEM; + } multi->channels_count = channels_count; multi->channels = calloc(channels_count, sizeof(*multi->channels)); + if (!multi->channels) { + free(multi->slaves); + free(multi->channels); + return -ENOMEM; + } for (i = 0; i < slaves_count; ++i) { snd_pcm_multi_slave_t *slave = &multi->slaves[i]; assert(slaves_pcm[i]->stream == stream); @@ -914,6 +923,11 @@ int _snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name, slaves_channels = calloc(slaves_count, sizeof(*slaves_channels)); channels_sidx = calloc(channels_count, sizeof(*channels_sidx)); channels_schannel = calloc(channels_count, sizeof(*channels_schannel)); + if (!slaves_id || !slaves_conf || !slaves_pcm || !slaves_channels || + !channels_sidx || !channels_schannel) { + err = -ENOMEM; + goto _free; + } idx = 0; for (idx = 0; idx < channels_count; ++idx) channels_sidx[idx] = -1; @@ -1036,6 +1050,8 @@ _free: free(channels_sidx); if (channels_schannel) free(channels_schannel); + if (slaves_id) + free(slaves_id); return err; } #ifndef DOC_HIDDEN diff --git a/src/pcm/pcm_share.c b/src/pcm/pcm_share.c index 8b978400..89220135 100644 --- a/src/pcm/pcm_share.c +++ b/src/pcm/pcm_share.c @@ -1630,6 +1630,10 @@ int _snd_pcm_share_open(snd_pcm_t **pcmp, const char *name, goto _free; } channels_map = calloc(channels, sizeof(*channels_map)); + if (! channels_map) { + err = -ENOMEM; + goto _free; + } snd_config_for_each(i, next, bindings) { snd_config_t *n = snd_config_iterator_entry(i); diff --git a/src/pcm/pcm_shm.c b/src/pcm/pcm_shm.c index ff7c722a..1c963aff 100644 --- a/src/pcm/pcm_shm.c +++ b/src/pcm/pcm_shm.c @@ -795,6 +795,8 @@ int snd_is_local(struct hostent *hent) conf.ifc_len = numreqs * sizeof(struct ifreq); conf.ifc_buf = malloc((unsigned int) conf.ifc_len); + if (! conf.ifc_buf) + return -ENOMEM; while (1) { err = ioctl(s, SIOCGIFCONF, &conf); if (err < 0) { @@ -806,6 +808,8 @@ int snd_is_local(struct hostent *hent) numreqs *= 2; conf.ifc_len = numreqs * sizeof(struct ifreq); conf.ifc_buf = realloc(conf.ifc_buf, (unsigned int) conf.ifc_len); + if (! conf.ifc_buf) + return -ENOMEM; } numreqs = conf.ifc_len / sizeof(struct ifreq); for (i = 0; i < numreqs; ++i) { -- 2.47.1