Skip to content

Commit

Permalink
Don't crash dumping translation events without summary or desc
Browse files Browse the repository at this point in the history
Return a proper GError failure instead.

Fixes: #80

Signed-off-by: Stephen Gallagher <[email protected]>
  • Loading branch information
sgallagher committed Sep 4, 2018
1 parent 5e40dd0 commit 8aaba79
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion modulemd/include/modulemd-1.0/private/modulemd-yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum ModulemdYamlError
MODULEMD_YAML_ERROR_PROGRAMMING,
MODULEMD_YAML_ERROR_UNPARSEABLE,
MODULEMD_YAML_ERROR_PARSE,
MODULEMD_YAML_ERROR_EMIT
MODULEMD_YAML_ERROR_EMIT,
MODULEMD_YAML_ERROR_MISSING_REQUIRED
};

const gchar *
Expand Down
16 changes: 16 additions & 0 deletions modulemd/v1/modulemd-yaml-emitter-translation.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,26 @@ _emit_translation_entry (yaml_emitter_t *emitter,

name = g_strdup ("summary");
value = modulemd_translation_entry_get_summary (entry);
if (!value)
{
g_set_error (error,
MODULEMD_YAML_ERROR,
MODULEMD_YAML_ERROR_MISSING_REQUIRED,
"Translation entry missing summary field.");
return FALSE;
}
MMD_EMIT_STR_STR_DICT (&event, name, value, YAML_PLAIN_SCALAR_STYLE);

name = g_strdup ("description");
value = modulemd_translation_entry_get_description (entry);
if (!value)
{
g_set_error (error,
MODULEMD_YAML_ERROR,
MODULEMD_YAML_ERROR_MISSING_REQUIRED,
"Translation entry missing description field.");
return FALSE;
}
MMD_EMIT_STR_STR_DICT (&event, name, value, YAML_PLAIN_SCALAR_STYLE);

/* Profile Descriptions */
Expand Down
15 changes: 15 additions & 0 deletions modulemd/v1/tests/test-modulemd-python.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ def test_issue77(self):
component.set_buildorder(5)
assert component.props.buildorder == 5

def test_issue80(self):
mmd_translation = Modulemd.Translation(module_name="modulename",
module_stream="modulestream",
mdversion=1,
modified=201809041500)
entry = Modulemd.TranslationEntry(locale='en_US')
mmd_translation.add_entry(entry)

# Would crash attempting to dump to YAML
try:
yaml_output = mmd_translation.dumps()
except GLib.GError as err:
# A proper exception is expected here
pass


class TestIntent(unittest.TestCase):

Expand Down
26 changes: 26 additions & 0 deletions modulemd/v1/tests/test-modulemd-translation.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,25 @@ modulemd_translation_test_index (TranslationFixture *fixture,
"プロファイルの例");
}

static void
modulemd_translation_test_missing_summary (TranslationFixture *fixture,
gconstpointer user_data)
{
g_autoptr (GError) error = NULL;
g_autoptr (ModulemdTranslation) translation = NULL;
g_autoptr (ModulemdTranslationEntry) entry = NULL;
g_autofree gchar *yaml = NULL;

translation = modulemd_translation_new_full (
"modulename", "modulestream", 1, 201809041500);

entry = modulemd_translation_entry_new ("en_US");
modulemd_translation_add_entry (translation, entry);

yaml = modulemd_translation_dumps (translation, &error);
g_assert_null (yaml);
}


int
main (int argc, char *argv[])
Expand Down Expand Up @@ -439,5 +458,12 @@ main (int argc, char *argv[])
modulemd_translation_test_index,
NULL);

g_test_add ("/modulemd/translation/test_missing_summary",
TranslationFixture,
NULL,
NULL,
modulemd_translation_test_missing_summary,
NULL);

return g_test_run ();
}

0 comments on commit 8aaba79

Please sign in to comment.