libgimpconfig: a NULL GBytes is also a proper argument value.

Do not fail serialization when a GBytes argument is set to NULL by adding a
special case for this.
This commit is contained in:
Jehan 2023-08-30 18:30:13 +02:00
parent e318651c99
commit b7bff4ee9a
2 changed files with 39 additions and 15 deletions

View file

@ -1042,19 +1042,43 @@ gimp_config_deserialize_bytes (GValue *value,
GParamSpec *prop_spec,
GScanner *scanner)
{
GBytes *bytes;
guint8 *data;
gint data_length;
GTokenType token;
GBytes *bytes;
guint8 *data;
gint data_length;
if (! gimp_scanner_parse_int (scanner, &data_length))
return G_TOKEN_INT;
token = g_scanner_peek_next_token (scanner);
if (! gimp_scanner_parse_data (scanner, data_length, &data))
return G_TOKEN_STRING;
if (token == G_TOKEN_IDENTIFIER)
{
g_scanner_get_next_token (scanner);
bytes = g_bytes_new (data, data_length);
if (g_ascii_strcasecmp (scanner->value.v_identifier, "null") != 0)
/* Do not fail the whole file parsing. Just output to stderr and assume
* a NULL bytes property.
*/
g_printerr ("%s: expected NULL identifier for bytes token '%s', got '%s'. "
"Assuming NULL instead.\n",
G_STRFUNC, prop_spec->name, scanner->value.v_identifier);
g_value_take_boxed (value, bytes);
g_value_set_boxed (value, NULL);
}
else if (token == G_TOKEN_INT)
{
if (! gimp_scanner_parse_int (scanner, &data_length))
return G_TOKEN_INT;
if (! gimp_scanner_parse_data (scanner, data_length, &data))
return G_TOKEN_STRING;
bytes = g_bytes_new (data, data_length);
g_value_take_boxed (value, bytes);
}
else
{
return G_TOKEN_INT;
}
return G_TOKEN_RIGHT_PAREN;
}

View file

@ -290,14 +290,14 @@ gimp_config_serialize_property (GimpConfig *config,
gimp_config_writer_printf (writer, "%lu", data_length);
gimp_config_writer_data (writer, data_length, data);
success = TRUE;
}
else
{
gimp_config_writer_printf (writer, "%s", "NULL");
}
if (success)
gimp_config_writer_close (writer);
else
gimp_config_writer_revert (writer);
success = TRUE;
gimp_config_writer_close (writer);
}
else if (G_VALUE_HOLDS_OBJECT (&value) &&
G_VALUE_TYPE (&value) != G_TYPE_FILE)