# Windows ENV object being corrupted

**URL:** <https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605>\
**Category:** Ruby\
**Created:** [August 11, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605 "2009-08-11T23:07:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 11, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/1 "2009-08-11T23:07:22Z")

</div>

I am running in rake ( I have reproduced the problem with a straight  
ruby script ) and I read in a batch file that has a few hundred  
environment variables in it. At some magic point when 323 entries are  
made to the ENV object, suddenly the ENV object gets corrupted.  
Suddenly the adds don’t seem to take and later a bunch of my Environment  
variables go missing.

The “local\_copy” hash does seem to hold but it isn’t what I need.

```
  environment.split("\n").each() do |pair|
         splitpos = pair.index('=')

         if ! splitpos.nil?
            key = pair.slice(0, splitpos)
            splitpos += 1
            value = pair.slice(splitpos, pair.length() - splitpos)
         end

         if ( ! key.nil? && ! value.nil?)
           key.strip!
           value.strip!
           verbose( ENV.size().to_s() + " -> ENV[#{key}] = " +

```

value, 1)  
ENV[key] = value.dup;  
local\_copy[key] = value.dup;  
else  
verbose(“Nil value found. Input String: #{pair}”, 5)  
end  
end

output:  
…  
322 -\> ENV[MAS\_REV] = 2  
323 -\> ENV[MAS\_VER] = 1.03c  
324 -\> ENV[MeetingCenterApp] = C:\Program Files\Meeting Center  
324 -\> ENV[MINIBOOST\_INC] =  
F:\source\iie\trunk\external\_libs\windows\_32\3rdparty\spirit-1.8.4\miniboost  
325 -\> ENV[MYSQL\_DEB\_LIB\_LOC] =  
F:\source\iie\trunk\external\_libs\windows\_32\3rdparty\mysql\4.1.16\rev1\shared\debug\win32\_mtdll  
326 -\> ENV[MYSQL\_DEB\_PRODUCT\_NAME] = libmysqld  
…  
363 -\> ENV[US\_COM\_DRS\_INC] =  
F:\source\iie\trunk\external\_libs\windows\_32\USCommercialDRS\DRS\10.01.03.M\Non-Dongled  
364 -\> ENV[US\_COM\_DRS\_LIB\_LOC] =  
F:\source\iie\trunk\external\_libs\windows\_32\USCommercialDRS\DRS\10.01.03.M\Non-Dongled  
31 -\> ENV[US\_COM\_DRS\_PRODUCT\_NAME] = POSTAL32

Anyone every ran into this before?

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 11, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/2 "2009-08-11T23:52:36Z")

</div>

Herman S. wrote:

> I am running in rake ( I have reproduced the problem with a straight  
> ruby script ) and I read in a batch file that has a few hundred  
> environment variables in it. At some magic point when 323 entries are  
> made to the ENV object, suddenly the ENV object gets corrupted.  
> Suddenly the adds don’t seem to take and later a bunch of my Environment  
> variables go missing.

This appears to be related to spaces in the value strings. As a Hack I  
removed any Env value that had a space, which happens to work for me  
now, but I still can’t wrap my head around why size plays a factor.

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 12, 2009, 9:44am UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/3 "2009-08-12T09:44:51Z")

</div>

2009/8/11 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> now, but I still can’t wrap my head around why size plays a factor.  
> I cannot either but this is due to the fact that I am missing  
> information in your original posting. For example, what is the format  
> of the “batch file” are you reading? What does variable “environment”  
> contain?

robert

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 12, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/4 "2009-08-12T17:01:16Z")

</div>

Robert K. wrote:

> 2009/8/11 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > now, but I still can’t wrap my head around why size plays a factor.  
> > I cannot either but this is due to the fact that I am missing  
> > information in your original posting. For example, what is the format  
> > of the “batch file” are you reading? What does variable “environment”  
> > contain?
> 
> robert

What I do is write the output from ‘set’ on windows to a ruby string  
like this:

environment = `call setEnvironment.bat;set`

setEnvironment.bat is just a bunch of environment sets

i.e.: SET LINK\_PLATFORM=windows\_32

The environment variables that were giving me trouble come from the  
system  
Ones like:  
ALLUSERSPROFILE=C:\Documents and Settings\All Users

So I get the sense that spaces are the issue, I believe that quoting  
variables with spaces would probably solve the issue but I haven’t tried  
it yet.

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 12, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/5 "2009-08-12T21:40:49Z")

</div>

On 12.08.2009 18:17, Robert K. wrote:

> And what is your aim, i.e. what do you want to achieve? If you want  
> to set environment variables for your Ruby script, you could easily do  
> this instead:
> 
> File.foreach “setEnvironment.bat” do |line|  
> if /^\s\*([^\s=]+)\s\*=\s\*(.\*)$/ =~ line  
> ENV[$1] = $2  
> end  
> end

Pardon, should have been

File.foreach “setEnvironment.bat” do |line|  
if /^\s_SET\s_([^\s=]+)\s\*=\s\*(.\*)$/i =~ line  
ENV[$1] = $2  
end  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 12, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/6 "2009-08-12T23:00:54Z")

</div>

Robert K. wrote:

> 2009/8/12 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > So I get the sense that spaces are the issue, I believe that quoting  
> > variables with spaces would probably solve the issue but I haven’t tried  
> > it yet.
> 
> And what is your aim, i.e. what do you want to achieve? If you want  
> to set environment variables for your Ruby script, you could easily do  
> this instead:
> 
> File.foreach “setEnvironment.bat” do |line|  
> if /^\s\*([^\s=]+)\s\*=\s\*(.\*)$/ =~ line  
> ENV[$1] = $2  
> end  
> end
> 
> and be done. Your approach looks suspiciously complicated to me. Of  
> course I may be missing something…
> 
> Kind regards
> 
> robert

Thanks, Robert. I can’t argue your approach isn’t simpler. In fact, I  
can’t remember why we ended up with this. All we really want is the same  
environment that the build system uses available for test and publishing  
systems. Your suggestion is noted.

However, the problem with ENV still exists. The problem with spaces and  
large numbers of environment variables. It’s just not in my way  
anymore. When I get a chance I will use your approach and see if I can  
re-create the problem from just parsing the setEnvironment.bat.

Thanks, again.

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 12, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/7 "2009-08-12T18:18:15Z")

</div>

2009/8/12 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So I get the sense that spaces are the issue, I believe that quoting  
> variables with spaces would probably solve the issue but I haven’t tried  
> it yet.

And what is your aim, i.e. what do you want to achieve? If you want  
to set environment variables for your Ruby script, you could easily do  
this instead:

File.foreach “setEnvironment.bat” do |line|  
if /^\s\*([^\s=]+)\s\*=\s\*(.\*)$/ =~ line  
ENV[$1] = $2  
end  
end

and be done. Your approach looks suspiciously complicated to me. Of  
course I may be missing something…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 13, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/8 "2009-08-13T12:18:14Z")

</div>

2009/8/12 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > this instead:  
> > Thanks, Robert. Â I can’t argue your approach isn’t simpler. Â In fact, I  
> > can’t remember why we ended up with this. All we really want is the same  
> > environment that the build system uses available for test and publishing  
> > systems. Â Your suggestion is noted.
> 
> However, the problem with ENV still exists. Â The problem with spaces and  
> large numbers of environment variables. Â It’s just not in my way  
> anymore. Â When I get a chance I will use your approach and see if I can  
> re-create the problem from just parsing the setEnvironment.bat.

I was curious and created the test attached but could not provoke any  
issues with spaces. I am pretty sure that the size of the environment  
is limited somehow but I did not hit a limit or any issues with my  
test (at 36,000 and still running).

What’s going on here?

robert

---

<div class="post-metadata">

**Author:** ![hermans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hermans](https://www.ruby-forum.com/u/hermans)\
**Post date:** [August 21, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/windows-env-object-being-corrupted/173605/9 "2009-08-21T17:13:20Z")

</div>

Robert K. wrote:

> 2009/8/12 Herman S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > this instead:  
> > > Thanks, Robert. Â I can’t argue your approach isn’t simpler. Â In fact, I  
> > > can’t remember why we ended up with this. All we really want is the same  
> > > environment that the build system uses available for test and publishing  
> > > systems. Â Your suggestion is noted.
> > 
> > However, the problem with ENV still exists. Â The problem with spaces and  
> > large numbers of environment variables. Â It’s just not in my way  
> > anymore. Â When I get a chance I will use your approach and see if I can  
> > re-create the problem from just parsing the setEnvironment.bat.
> 
> I was curious and created the test attached but could not provoke any  
> issues with spaces. I am pretty sure that the size of the environment  
> is limited somehow but I did not hit a limit or any issues with my  
> test (at 36,000 and still running).
> 
> What’s going on here?
> 
> robert

I had chance to get and try your routine. I think the author of this  
code was trying to get around a recursive problem namely that  
environment variables are comprised of one or more environment  
variables.

Once I added a routine to resolve this issue I have run without  
incident.

def resolve\_variables( field )

```
value = field

value.strip!
replace = ""
verbose( "In Value = " + value , 5)
if value =~ /%(\w+)%/
   value.gsub!(/%(\w+)%/) do
      found = $1
      replace = found.gsub("%","")
      replace = ENV[replace]

      if replace.nil?
         abort("??Error: Use of undefined Environment Variable 

```

#{found} in #{field}")  
end  
verbose("Rep = " + replace, 5)  
verbose( "Val = " + value , 5)  
replace  
end  
verbose( "Val = " + value , 4)  
end  
verbose( "Return → " + value , 4)  
return value  
end

```
  File.foreach env_batch_file do |line|
     if /^\s*SET\s*([^\s=]+)\s*=\s*(.*)$/i =~ line
        key = $1
        value = resolve_variables($2)
        verbose("ENV[" + key + "] = " + value, 1)
        ENV[key] = value
     end
  end

```
