Ensure that strings are valid UTF-8 before calling libyaml

Libyaml has a crash bug that can be triggered by invalid UTF-8 input. This
patch adds a UTF-8 encoding validator function borrowed from
ext/standard/html.c to validate strings encoding before calling libyaml.

Bug: 61770
Merge branch 'pull-request/6'