]> git.alsa-project.org Git - alsa-gobject.git/commitdiff
ctl: skip check of return value from g_malloc()
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Fri, 13 Nov 2020 07:26:26 +0000 (16:26 +0900)
committer坂本 貴史 <o-takashi@sakamocchi.jp>
Fri, 13 Nov 2020 23:30:49 +0000 (08:30 +0900)
In GLib implementation, the call of g_malloc() can bring program abort
due to memory starvation. This is necessarily preferable because applications
cannot handle the case and GLib has alternative APIs named 'try' to
allow applications to handle the case.

On the other hand, the memory starvation is system wide issue and it's
hard for applications to solve the issue. It's reasonable for the
implementation to have program abort somehow.

Furthermore, the call of g_object_new() can bring program abort due
to memory starvation. It's impossible for applications to handle the
case.

This commit skips check of return value from g_malloc().

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
src/ctl/card.c
src/ctl/query.c

index 0a49fbbd0e00c40202199f36a8de253d97ce9ab9..42534a2966a352624a5a195939c74f9100e672b6 100644 (file)
@@ -289,11 +289,7 @@ static void allocate_elem_ids(int fd, struct snd_ctl_elem_list *list,
         return;
 
     // Allocate spaces for these elements.
-    ids = calloc(list->count, sizeof(*ids));
-    if (!ids) {
-        generate_error(error, ENOMEM);
-        return;
-    }
+    ids = g_malloc_n(list->count, sizeof(*ids));
 
     list->offset = 0;
     while (list->offset < list->count) {
@@ -396,10 +392,6 @@ static void parse_enum_names(ALSACtlCardPrivate *priv,
     int i;
 
     *labels = g_malloc0_n(count + 1, sizeof(**labels));
-    if (*labels == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
 
     for (i = 0; i < count; ++i) {
         info->value.enumerated.item = i;
@@ -408,11 +400,7 @@ static void parse_enum_names(ALSACtlCardPrivate *priv,
             goto error;
         }
 
-        (*labels)[i] = strdup(info->value.enumerated.name);
-        if ((*labels)[i] == NULL) {
-            generate_error(error, ENOMEM);
-            goto error;
-        }
+        (*labels)[i] = g_strdup(info->value.enumerated.name);
     }
 
     (*labels)[count] = NULL;
@@ -521,10 +509,6 @@ void alsactl_card_write_elem_tlv(ALSACtlCard *self,
     container_size = container_count * sizeof(*container);
 
     packet = g_malloc0(sizeof(*packet) + container_size);
-    if (packet == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
 
     packet->numid = elem_id->numid;
     packet->length = container_size;
@@ -571,10 +555,6 @@ void alsactl_card_read_elem_tlv(ALSACtlCard *self, const ALSACtlElemId *elem_id,
     container_size = *container_count * sizeof(**container);
 
     packet = g_malloc0(sizeof(*packet) + container_size);
-    if (packet == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
 
     packet->numid = elem_id->numid;
     packet->length = container_size;
@@ -624,10 +604,6 @@ void alsactl_card_command_elem_tlv(ALSACtlCard *self,
     container_size = *container_count * sizeof(**container);
 
     packet = g_malloc0(sizeof(*packet) + container_size);
-    if (packet == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
 
     packet->numid = elem_id->numid;
     packet->length = container_size;
@@ -662,8 +638,7 @@ static int prepare_enum_names(struct snd_ctl_elem_info *info, const gchar **labe
         return -EINVAL;
 
     pos = g_malloc0(length);
-    if (pos == NULL)
-        return -ENOMEM;
+
     info->value.enumerated.names_ptr = (__u64)pos;
     info->value.enumerated.names_length = length;
 
@@ -1005,11 +980,7 @@ void alsactl_card_create_source(ALSACtlCard *self, GSource **gsrc,
         return;
     }
 
-    buf = g_try_malloc0(page_size);
-    if (buf == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
+    buf = g_malloc0(page_size);
 
     *gsrc = g_source_new(&funcs, sizeof(CtlCardSource));
     src = (CtlCardSource *)(*gsrc);
index 889d989bf2a3076d32e179e41136615eb9ab6f78..4bec20d25fd7c178dedec32fa89cdfa84a8a877b 100644 (file)
@@ -147,11 +147,7 @@ void alsactl_get_card_id_list(guint **entries, gsize *entry_count,
     if (count == 0)
         goto end;
 
-    *entries = g_try_malloc0_n(count, sizeof(**entries));
-    if (*entries == NULL) {
-        generate_error(error, ENOMEM);
-        goto end;
-    }
+    *entries = g_malloc0_n(count, sizeof(**entries));
 
     index = 0;
     udev_list_entry_foreach(entry, entry_list) {
@@ -198,11 +194,7 @@ static void allocate_sysname(char **sysname ,const char *template,
     }
 
     length = strlen(template) + digits;
-    *sysname = g_try_malloc0(length + 1);
-    if (*sysname == NULL) {
-        generate_error(error, ENOMEM);
-        return;
-    }
+    *sysname = g_malloc0(length + 1);
 
     snprintf(*sysname, length, template, card_id);
 }
@@ -332,7 +324,7 @@ void alsactl_get_control_devnode(guint card_id, char **devnode, GError **error)
 
     node = udev_device_get_devnode(dev);
     if (node != NULL)
-        *devnode = strdup(node);
+        *devnode = g_strdup(node);
     else
         generate_error(error, ENODEV);