Skip to content

Commit

Permalink
Appropriate short-circuit for shell commands. Fixes #4361.
Browse files Browse the repository at this point in the history
The original logic was a bad port of the following C code:

```c
static const char posix_sh_cmds[][9] = {
...
}
if (first.len > 0 && first.len <= sizeof(posix_sh_cmds[0]) &&
...
```

In C, the array allocation sets all elements to be sizeof = 9,
even though most terminate via null at a shorter C string length.
The check here is to filter out any commands longer than those
known to be shell commands; my fix is to just have that max length
be a constant and reference it where sizeof is used in MRI.
  • Loading branch information
headius committed Dec 6, 2016
1 parent 3a7b5a2 commit 69da0af
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/util/io/PopenExecutor.java
Expand Up @@ -1729,6 +1729,7 @@ public static RubyString checkArgv(ThreadContext context, IRubyObject[] argv) {
return prog;
}

private static final int posix_sh_cmd_length = 8;
private static final String posix_sh_cmds[] = {
"!", /* reserved */
".", /* special built-in */
Expand Down Expand Up @@ -1855,7 +1856,7 @@ else if (progByteList.get(p) == '/'){
}
if (!has_meta && first.getUnsafeBytes() != DUMMY_ARRAY) {
if (first.length() == 0) first.setRealSize(p - first.getBegin());
if (first.length() > 0 && first.length() <= posix_sh_cmds[0].length() &&
if (first.length() > 0 && first.length() <= posix_sh_cmd_length &&
Arrays.binarySearch(posix_sh_cmds, first.toString(), StringComparator.INSTANCE) >= 0)
has_meta = true;
}
Expand Down

0 comments on commit 69da0af

Please sign in to comment.