Skip to content

Commit

Permalink
Normalize PATH set to filesystem encoding, as in MRI.
Browse files Browse the repository at this point in the history
Fixes the rest of #3907.
  • Loading branch information
headius committed May 25, 2016
1 parent 65db8b2 commit 6daad69
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions core/src/main/java/org/jruby/RubyGlobal.java
Expand Up @@ -58,6 +58,7 @@
import org.jruby.util.OSEnvironment;
import org.jruby.util.RegexpOptions;
import org.jruby.util.cli.OutputStrings;
import org.jruby.util.io.EncodingUtils;
import org.jruby.util.io.OpenFile;
import org.jruby.util.io.STDIO;

Expand Down Expand Up @@ -420,9 +421,9 @@ protected IRubyObject case_aware_op_aset(ThreadContext context,
return super.delete(context, key, org.jruby.runtime.Block.NULL_BLOCK);
}

IRubyObject keyAsStr = normalizeEnvString(Helpers.invoke(context, key, "to_str"));
IRubyObject keyAsStr = normalizeEnvString(context, key, Helpers.invoke(context, key, "to_str"));
IRubyObject valueAsStr = value.isNil() ? context.nil :
normalizeEnvString(Helpers.invoke(context, value, "to_str"));
normalizeEnvString(context, key, Helpers.invoke(context, value, "to_str"));

if (updateRealENV) {
POSIX posix = context.runtime.getPosix();
Expand Down Expand Up @@ -458,14 +459,26 @@ private RubyString getCorrectKey(IRubyObject key, ThreadContext context) {
return actualKey;
}

private IRubyObject normalizeEnvString(IRubyObject str) {
if (str instanceof RubyString) {
Encoding enc = getRuntime().getEncodingService().getLocaleEncoding();
RubyString newStr = getRuntime().newString(new ByteList(str.toString().getBytes(), enc));
private IRubyObject normalizeEnvString(ThreadContext context, IRubyObject key, IRubyObject value) {
if (value instanceof RubyString) {
Ruby runtime = context.runtime;
RubyString valueStr = (RubyString) value;
Encoding enc;

// Ensure PATH is encoded like filesystem
if (key.toString().equalsIgnoreCase("PATH")) {
enc = runtime.getEncodingService().getFileSystemEncoding();
} else {
enc = runtime.getEncodingService().getLocaleEncoding();
}

RubyString newStr = EncodingUtils.strConvEnc(context, valueStr, valueStr.getEncoding(), enc);

newStr.setFrozen(true);

return newStr;
}
return str;
return value;
}
}

Expand Down

0 comments on commit 6daad69

Please sign in to comment.