Skip to content

Commit

Permalink
Merge pull request #61 from go-faster/fix/use-explicit-key-if-it-is-f…
Browse files Browse the repository at this point in the history
…low-mapping

fix: use explicit key mapping for flow keys
  • Loading branch information
ernado authored Apr 23, 2023
2 parents c6b72e5 + 03d700e commit d6b7a40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 27 additions & 1 deletion emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions testdata/fuzz/FuzzDecodeEncodeDecode/f91ab6905aa88579
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("{{} #\n}")

0 comments on commit d6b7a40

Please sign in to comment.