[tex-live] [PATCH] Avoid infinite loop in variable expansion

Jjgod Jiang gzjjgod at gmail.com
Tue Mar 10 07:25:53 CET 2009


When expanding a environment variable, say, if we want to
expand $PATH where PATH=%FOO%, the original code below:

while ($s =~ /^([^%]*)%([^%]+)%(.*)$/) {
  foreach $k (keys %ENV) {
    if (uc($k) eq uc($2)) {
      $s = $1 . $ENV{$k} . $3;
      last;
    }
  }
}

will try to find a variable named "FOO" in environment
variables, if it's found, then the PATH variable will
got expanded and we will step to the next variable
surrounded by %%, but if we cannot found a match, the while
loop will continue look for $ENV{FOO} infinitely, which will
cause a serious problem during TeXLive installation in
Windows.

This patch (provided by Dieken at newsmth.net) fixed it.
---
 TLWinGoo.pm |   20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/TLWinGoo.pm b/TLWinGoo.pm
index edffe91..b1cfe76 100644
--- a/TLWinGoo.pm
+++ b/TLWinGoo.pm
@@ -286,17 +286,19 @@ values as environment variable and returns the result.
 
 =cut
 
+sub expand_env {
+  my $key = shift;
+  foreach my $k (keys %ENV) {
+     return $ENV{$k} if (uc($k) eq uc($key));
+  }
+
+  return "%$key%";
+}
+
 sub expand_string {
   $s = shift @_;
-  while ($s =~ /^([^%]*)%([^%]+)%(.*)$/) {
-    foreach $k (keys %ENV) {
-      if (uc($k) eq uc($2)) {
-        $s = $1 . $ENV{$k} . $3;
-        last;
-      }
-    }
-  }
-  $s;
+  $s =~ s/%([^%;]+)%/expand_env($1)/eg;
+  return $s;
 }
 
 =pod
-- 
1.6.0.4



More information about the tex-live mailing list