Skip to content

Commit 8aaba79

Browse files
committed
Don't crash dumping translation events without summary or desc
Return a proper GError failure instead. Fixes: #80 Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 5e40dd0 commit 8aaba79

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

modulemd/include/modulemd-1.0/private/modulemd-yaml.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ enum ModulemdYamlError
3030
MODULEMD_YAML_ERROR_PROGRAMMING,
3131
MODULEMD_YAML_ERROR_UNPARSEABLE,
3232
MODULEMD_YAML_ERROR_PARSE,
33-
MODULEMD_YAML_ERROR_EMIT
33+
MODULEMD_YAML_ERROR_EMIT,
34+
MODULEMD_YAML_ERROR_MISSING_REQUIRED
3435
};
3536

3637
const gchar *

modulemd/v1/modulemd-yaml-emitter-translation.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,26 @@ _emit_translation_entry (yaml_emitter_t *emitter,
245245

246246
name = g_strdup ("summary");
247247
value = modulemd_translation_entry_get_summary (entry);
248+
if (!value)
249+
{
250+
g_set_error (error,
251+
MODULEMD_YAML_ERROR,
252+
MODULEMD_YAML_ERROR_MISSING_REQUIRED,
253+
"Translation entry missing summary field.");
254+
return FALSE;
255+
}
248256
MMD_EMIT_STR_STR_DICT (&event, name, value, YAML_PLAIN_SCALAR_STYLE);
249257

250258
name = g_strdup ("description");
251259
value = modulemd_translation_entry_get_description (entry);
260+
if (!value)
261+
{
262+
g_set_error (error,
263+
MODULEMD_YAML_ERROR,
264+
MODULEMD_YAML_ERROR_MISSING_REQUIRED,
265+
"Translation entry missing description field.");
266+
return FALSE;
267+
}
252268
MMD_EMIT_STR_STR_DICT (&event, name, value, YAML_PLAIN_SCALAR_STYLE);
253269

254270
/* Profile Descriptions */

modulemd/v1/tests/test-modulemd-python.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ def test_issue77(self):
201201
component.set_buildorder(5)
202202
assert component.props.buildorder == 5
203203

204+
def test_issue80(self):
205+
mmd_translation = Modulemd.Translation(module_name="modulename",
206+
module_stream="modulestream",
207+
mdversion=1,
208+
modified=201809041500)
209+
entry = Modulemd.TranslationEntry(locale='en_US')
210+
mmd_translation.add_entry(entry)
211+
212+
# Would crash attempting to dump to YAML
213+
try:
214+
yaml_output = mmd_translation.dumps()
215+
except GLib.GError as err:
216+
# A proper exception is expected here
217+
pass
218+
204219

205220
class TestIntent(unittest.TestCase):
206221

modulemd/v1/tests/test-modulemd-translation.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,25 @@ modulemd_translation_test_index (TranslationFixture *fixture,
393393
"プロファイルの例");
394394
}
395395

396+
static void
397+
modulemd_translation_test_missing_summary (TranslationFixture *fixture,
398+
gconstpointer user_data)
399+
{
400+
g_autoptr (GError) error = NULL;
401+
g_autoptr (ModulemdTranslation) translation = NULL;
402+
g_autoptr (ModulemdTranslationEntry) entry = NULL;
403+
g_autofree gchar *yaml = NULL;
404+
405+
translation = modulemd_translation_new_full (
406+
"modulename", "modulestream", 1, 201809041500);
407+
408+
entry = modulemd_translation_entry_new ("en_US");
409+
modulemd_translation_add_entry (translation, entry);
410+
411+
yaml = modulemd_translation_dumps (translation, &error);
412+
g_assert_null (yaml);
413+
}
414+
396415

397416
int
398417
main (int argc, char *argv[])
@@ -439,5 +458,12 @@ main (int argc, char *argv[])
439458
modulemd_translation_test_index,
440459
NULL);
441460

461+
g_test_add ("/modulemd/translation/test_missing_summary",
462+
TranslationFixture,
463+
NULL,
464+
NULL,
465+
modulemd_translation_test_missing_summary,
466+
NULL);
467+
442468
return g_test_run ();
443469
}

0 commit comments

Comments
 (0)