Previously, no decoding happened, meaning that Perl interpreted the
string as single bytes instead of Unicode code points when reading
the config. Note: while I would have preferred to decode the text
right after reading from the file, there are some Perl functions
like Digest::SHA::sha1_hex that expect bytes instead of UTF-8.

Also, config files are now explicitly encoded as UTF-8 when writing
the config, preventing issues the other way around.

For more information, please read:
https://perldoc.perl.org/perlunifaq#When-should-I-decode-or-encode?

Signed-off-by: Laurențiu Leahu-Vlăducu <l.leahu-vlad...@proxmox.com>
---
 src/PVE/SectionConfig.pm | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/PVE/SectionConfig.pm b/src/PVE/SectionConfig.pm
index 6a297d3..4e98c1c 100644
--- a/src/PVE/SectionConfig.pm
+++ b/src/PVE/SectionConfig.pm
@@ -98,6 +98,7 @@ use strict;
 use warnings;
 
 use Carp;
+use Encode qw(decode);
 use Digest::SHA;
 
 use PVE::Exception qw(raise_param_exc);
@@ -1091,7 +1092,7 @@ Only used for error messages and warnings, so it may also 
be something else.
 
 =item C<$raw>
 
-The raw content of C<$filename>.
+The raw content of C<$filename>. It is assumed to be encoded as UTF-8.
 
 =item C<$allow_unknown> (optional)
 
@@ -1185,11 +1186,12 @@ sub parse_config {
     $raw = '' if !defined($raw);
 
     my $digest = Digest::SHA::sha1_hex($raw);
+    my $utf8_text = Encode::decode('UTF-8', $raw);
 
     my $pri = 1;
 
     my $lineno = 0;
-    my @lines = split(/\n/, $raw);
+    my @lines = split(/\n/, $utf8_text);
     my $nextline = sub {
        while (defined(my $line = shift @lines)) {
            $lineno++;
@@ -1430,6 +1432,8 @@ my sub format_config_line {
     $output = $class->write_config($filename, $cfg, $allow_unknown)
 
 Generates the output that should be written to the C<L<PVE::SectionConfig>> 
file.
+The output is encoded as bytes (encoded from UTF-8) that can be directly
+written to the config file.
 
 =over
 
@@ -1560,7 +1564,7 @@ sub write_config {
        $out .= "$data\n";
     }
 
-    return $out;
+    return Encode::encode('UTF-8', $out);
 }
 
 sub assert_if_modified {
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to