* @G_CONVERT_ERROR_PARTIAL_INPUT: Partial character sequence at end of input.
* @G_CONVERT_ERROR_BAD_URI: URI is invalid.
* @G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: Pathname is not an absolute path.
+ * @G_CONVERT_ERROR_NO_MEMORY: No memory available. Since: 2.40
*
* Error codes returned by character set conversion routines.
*/
G_CONVERT_ERROR_FAILED,
G_CONVERT_ERROR_PARTIAL_INPUT,
G_CONVERT_ERROR_BAD_URI,
- G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
+ G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
+ G_CONVERT_ERROR_NO_MEMORY
} GConvertError;
/**
return result;
}
+static gpointer
+try_malloc (gsize n_bytes, GError **error)
+{
+ gpointer ptr = g_try_malloc (n_bytes);
+ if (ptr == NULL)
+ g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY,
+ _("Failed to allocate memory"));
+ return ptr;
+}
+
/**
* g_utf8_to_ucs4:
* @str: a UTF-8 encoded string
in = g_utf8_next_char (in);
}
- result = g_new (gunichar, n_chars + 1);
-
+ result = try_malloc (sizeof (gunichar) * (n_chars + 1), error);
+ if (result == NULL)
+ goto err_out;
+
in = str;
for (i=0; i < n_chars; i++)
{
result_length += UTF8_LENGTH (str[i]);
}
- result = g_malloc (result_length + 1);
+ result = try_malloc (result_length + 1, error);
+ if (result == NULL)
+ goto err_out;
+
p = result;
i = 0;
/* At this point, everything is valid, and we just need to convert
*/
/********** DIFFERENT for UTF8/UCS4 **********/
- result = g_malloc (n_bytes + 1);
-
+ result = try_malloc (n_bytes + 1, error);
+ if (result == NULL)
+ goto err_out;
+
high_surrogate = 0;
out = result;
in = str;
/* At this point, everything is valid, and we just need to convert
*/
/********** DIFFERENT for UTF8/UCS4 **********/
- result = g_malloc (n_bytes + 4);
-
+ result = try_malloc (n_bytes + 4, error);
+ if (result == NULL)
+ goto err_out;
+
high_surrogate = 0;
out = result;
in = str;
in = g_utf8_next_char (in);
}
- result = g_new (gunichar2, n16 + 1);
-
+ result = try_malloc (sizeof (gunichar2) * (n16 + 1), error);
+ if (result == NULL)
+ goto err_out;
+
in = str;
for (i = 0; i < n16;)
{
i++;
}
-
- result = g_new (gunichar2, n16 + 1);
-
+
+ result = try_malloc (sizeof (gunichar2) * (n16 + 1), error);
+ if (result == NULL)
+ goto err_out;
+
for (i = 0, j = 0; j < n16; i++)
{
gunichar wc = str[i];