From: b Date: Mon, 19 Sep 2022 21:10:59 +0000 (+0000) Subject: introduce masked replacement to avoid undefined behavior X-Git-Tag: v1.0.0~3 X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=47932af96bb36161becbe034e1765077730d08e2;p=botm%2Fconfig introduce masked replacement to avoid undefined behavior --- diff --git a/configure.pl b/configure.pl index 68c2487..9952e8d 100755 --- a/configure.pl +++ b/configure.pl @@ -494,6 +494,28 @@ sub replace { return (1, $before.$replacement.$after); } +# replace but with mask to avoid duplicated replacement +sub replace_masked { + (my $text, my $mask, my $to_replace, my $replacement) = @_; + + my $ind = index($mask, $to_replace); + if ($ind <0) { + return (0, $text, $mask); + } + my $len = length($to_replace); + + my $text_before = substr($text, 0, $ind); + my $mask_before = substr($mask, 0, $ind); + my $mask_insert = "\r" x length($replacement); # CR can never appear in $text + my $text_after = substr($text, $ind+$len); + my $mask_after = substr($mask, $ind+$len); + + $text = $text_before.$replacement.$text_after; + $mask = $mask_before.$mask_insert.$mask_after; + + return (1, $text, $mask); +} + sub print_debug { (my $depth, my $text) = @_; if ($debug_enabled) { @@ -976,9 +998,10 @@ sub convert_file { next LINE; } } + my $mask = $line; foreach my $key (keys %replace_keyword) { do { - ($status, $line) = replace($line, $key, $replace_keyword{$key}); + ($status, $line, $mask) = replace_masked($line, $mask, $key, $replace_keyword{$key}); } while ($status); } print $out "$line\n";