From e26dcee4135b3b41de1267ecba9d29f0efba1c4a Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sun, 23 Apr 2023 21:37:01 +0300 Subject: [PATCH 1/2] fix: use explicit key mapping for flow keys --- emitterc.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/emitterc.go b/emitterc.go index a079def..781913d 100644 --- a/emitterc.go +++ b/emitterc.go @@ -677,7 +677,9 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve } } - if !emitter.canonical && yaml_emitter_check_simple_key(emitter) { + if !emitter.canonical && + !yaml_emitter_check_empty_flow(emitter) && + yaml_emitter_check_simple_key(emitter) { emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE) return yaml_emitter_emit_node(emitter, event, false, false, true, true) } @@ -968,6 +970,30 @@ func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool { emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT } +// Check if the next events represent an empty flow sequence/mapping. +func yaml_emitter_check_empty_flow(emitter *yaml_emitter_t) bool { + if len(emitter.events)-emitter.events_head < 2 { + return false + } + + start := emitter.events[emitter.events_head] + switch start.typ { + case yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT: + default: + return false + } + + end := emitter.events[emitter.events_head+1] + switch end.typ { + case yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT: + default: + return false + } + + return start.style == yaml_style_t(yaml_FLOW_SEQUENCE_STYLE) || + start.style == yaml_style_t(yaml_FLOW_MAPPING_STYLE) +} + // Check if the next node can be expressed as a simple key. func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool { length := 0 From 03d700e9910ef426058c6130af625cb1ffc2f55d Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sun, 23 Apr 2023 21:37:18 +0300 Subject: [PATCH 2/2] chore: add fuzz corpus --- testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579 | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579 diff --git a/testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579 b/testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579 new file mode 100644 index 0000000..2df1fc8 --- /dev/null +++ b/testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("{{} #\n}")