From 399bc7ca80c876b8b7722fb050010319955d19a3 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Apr 2023 08:59:51 +0300 Subject: [PATCH 1/3] fix: put indentation before tag of block mapping --- emitterc.go | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/emitterc.go b/emitterc.go index 208621e..d3cefe6 100644 --- a/emitterc.go +++ b/emitterc.go @@ -766,11 +766,6 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_ // Expect a block key node. func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { - if first { - if !yaml_emitter_increase_indent(emitter, false, false) { - return false - } - } if !yaml_emitter_process_head_comment(emitter) { return false } @@ -932,16 +927,38 @@ func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event // Expect MAPPING-START. func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { - if !yaml_emitter_process_anchor(emitter) { - return false - } - if !yaml_emitter_process_tag(emitter) { - return false - } if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE || yaml_emitter_check_empty_mapping(emitter) { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE } else { + // FIXME(tdakkota): should we do it for sequence/flow style? + // + // Write same indent as mapping have. + // In cases like this: + // + // a: + // b: # comment + // !tag + // c: + // + if !yaml_emitter_increase_indent(emitter, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE } return true From 7787df8abdf38cc415e70738abd49119624ac9b0 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Apr 2023 09:06:35 +0300 Subject: [PATCH 2/3] fix: put indentation before tag of block sequence --- emitterc.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/emitterc.go b/emitterc.go index d3cefe6..11be40e 100644 --- a/emitterc.go +++ b/emitterc.go @@ -730,11 +730,6 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e // Expect a block item node. func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { - if first { - if !yaml_emitter_increase_indent(emitter, false, false) { - return false - } - } if event.typ == yaml_SEQUENCE_END_EVENT { emitter.indent = emitter.indents[len(emitter.indents)-1] emitter.indents = emitter.indents[:len(emitter.indents)-1] @@ -910,16 +905,28 @@ func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool // Expect SEQUENCE-START. func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { - if !yaml_emitter_process_anchor(emitter) { - return false - } - if !yaml_emitter_process_tag(emitter) { - return false - } if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || yaml_emitter_check_empty_sequence(emitter) { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE } else { + if !yaml_emitter_increase_indent(emitter, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE } return true @@ -937,7 +944,7 @@ func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_ } emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE } else { - // FIXME(tdakkota): should we do it for sequence/flow style? + // FIXME(tdakkota): should we do it for flow style? // // Write same indent as mapping have. // In cases like this: From 3f3f2ddda993cc5f3bb39e5e64622a247f8549cb Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 24 Apr 2023 09:00:30 +0300 Subject: [PATCH 3/3] chore: add fuzz corpus --- testdata/fuzz/FuzzDecodeEncodeDecode/3fd9827db9432432 | 2 ++ testdata/fuzz/FuzzDecodeEncodeDecode/64588d00b12b492f | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 testdata/fuzz/FuzzDecodeEncodeDecode/3fd9827db9432432 create mode 100644 testdata/fuzz/FuzzDecodeEncodeDecode/64588d00b12b492f diff --git a/testdata/fuzz/FuzzDecodeEncodeDecode/3fd9827db9432432 b/testdata/fuzz/FuzzDecodeEncodeDecode/3fd9827db9432432 new file mode 100644 index 0000000..28a0fea --- /dev/null +++ b/testdata/fuzz/FuzzDecodeEncodeDecode/3fd9827db9432432 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("0:\n 0: #\n !0\n -") diff --git a/testdata/fuzz/FuzzDecodeEncodeDecode/64588d00b12b492f b/testdata/fuzz/FuzzDecodeEncodeDecode/64588d00b12b492f new file mode 100644 index 0000000..362b08a --- /dev/null +++ b/testdata/fuzz/FuzzDecodeEncodeDecode/64588d00b12b492f @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("0:\n 0: #\n !0\n 0:")