Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion c/src/ml-api-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ _ml_info_set_value (ml_info_s * info, const char *key, void *value,
ml_data_destroy_cb destroy)
{
ml_info_value_s *info_value;
gchar *key_dup;

if (!STR_IS_VALID (key))
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
Expand All @@ -1507,14 +1508,33 @@ _ml_info_set_value (ml_info_s * info, const char *key, void *value,
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'info' or 'value' is NULL. It should be a valid ml_info and value.");

if (!info->table)
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
"The parameter, 'info' is invalid. It should have a valid table instance.");

info_value = g_new0 (ml_info_value_s, 1);
if (!info_value)
_ml_error_report_return (ML_ERROR_OUT_OF_MEMORY,
"Failed to allocate memory for the info value. Out of memory?");

info_value->value = value;
info_value->destroy = destroy;
g_hash_table_insert (info->table, g_strdup (key), (gpointer) info_value);

/**
* Note: 'value' ownership is transferred to the table.
* If we fail to duplicate the key (OOM), we should free 'value' using
* the provided destroy callback to avoid leaks.
*/
key_dup = g_strdup (key);
if (!key_dup) {
if (info_value->destroy)
info_value->destroy (info_value->value);
g_free (info_value);
_ml_error_report_return (ML_ERROR_OUT_OF_MEMORY,
"Failed to duplicate the key string. Out of memory?");
}

g_hash_table_insert (info->table, key_dup, (gpointer) info_value);

return ML_ERROR_NONE;
}
Expand Down
Loading