From 08bccc4f21e3a3a2ee598198dab2f8aeb2f31be5 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Mon, 16 Nov 2020 11:45:41 +0900 Subject: [PATCH] rawmidi: skip check of return value from g_malloc() 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 --- src/rawmidi/query.c | 49 ++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/src/rawmidi/query.c b/src/rawmidi/query.c index 385dc12..73c3ee0 100644 --- a/src/rawmidi/query.c +++ b/src/rawmidi/query.c @@ -153,11 +153,8 @@ void alsarawmidi_get_device_id_list(guint card_id, guint **entries, goto end; length = strlen(PREFIX_SYSNAME_TEMPLATE) + calculate_digits(card_id) + 1; - prefix = g_try_malloc0(length); - if (prefix == NULL) { - generate_error(error, ENOMEM); - goto end; - } + prefix = g_malloc0(length); + snprintf(prefix, length, PREFIX_SYSNAME_TEMPLATE, card_id); entry_list = udev_enumerate_get_list_entry(enumerator); @@ -175,11 +172,7 @@ void alsarawmidi_get_device_id_list(guint card_id, guint **entries, 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) { @@ -233,11 +226,8 @@ void alsarawmidi_get_rawmidi_sysname(guint card_id, guint device_id, length = strlen(RAWMIDI_SYSNAME_TEMPLATE) + calculate_digits(card_id) + calculate_digits(device_id) + 1; - name = g_try_malloc0(length); - if (name == NULL) { - generate_error(error, ENOMEM); - return; - } + name = g_malloc0(length); + snprintf(name, length, RAWMIDI_SYSNAME_TEMPLATE, card_id, device_id); ctx = udev_new(); @@ -281,11 +271,8 @@ void alsarawmidi_get_rawmidi_devnode(guint card_id, guint device_id, length = strlen(RAWMIDI_SYSNAME_TEMPLATE) + calculate_digits(card_id) + calculate_digits(device_id) + 1; - name = g_try_malloc0(length); - if (name == NULL) { - generate_error(error, ENOMEM); - return; - } + name = g_malloc0(length); + snprintf(name, length, RAWMIDI_SYSNAME_TEMPLATE, card_id, device_id); ctx = udev_new(); @@ -304,13 +291,10 @@ void alsarawmidi_get_rawmidi_devnode(guint card_id, guint device_id, } node = udev_device_get_devnode(dev); - if (node == NULL) { + if (node == NULL) generate_error(error, ENOENT); - } else { - *devnode = strdup(node); - if (*devnode == NULL) - generate_error(error, ENOMEM); - } + else + *devnode = g_strdup(node); udev_device_unref(dev); udev_unref(ctx); @@ -327,11 +311,8 @@ static void rawmidi_perform_ctl_ioctl(guint card_id, long request, void *data, int fd; length = strlen(CTL_SYSNAME_TEMPLATE) + calculate_digits(card_id) + 1; - sysname = g_try_malloc0(length); - if (sysname == NULL) { - generate_error(error, ENOMEM); - return; - } + sysname = g_malloc0(length); + snprintf(sysname, length, CTL_SYSNAME_TEMPLATE, card_id); ctx = udev_new(); @@ -406,11 +387,7 @@ void alsarawmidi_get_subdevice_id_list(guint card, guint device, if (*error != NULL) return; - *entries = g_try_malloc0_n(info.subdevices_count, sizeof(guint)); - if (*entries == NULL) { - generate_error(error, ENOMEM); - return; - } + *entries = g_malloc0_n(info.subdevices_count, sizeof(guint)); for (i = 0; i < info.subdevices_count; ++i) (*entries)[i] = i; -- 2.47.3