rubinius/rubinius — pull request #3157 by ryoqun (2014-10-05 09:34:41+00:00)
Title: Add extra padding for ByteArray after encoding

Currently String#encode produces an ill-state string in some cases.

In all other places, String allocates its internal ByteArray buffer with size + 1. Follow the rule here too.

This bug causes the following mysterious error (was hard to track down…):

## The reproducible code

```
require 'json'

(s = "70g・1パック".encode(Encoding::UTF_16LE).force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8));
p [s, s.bytesize, s.instance_variable_get(:@data)]
s.to_json
```

## Before

```
bash-3.2$ ./bin/rbx ~/rubinius/bad-encode.rb
["70g・1パック", 16, #]
An exception occurred running /Users/ryo-onodera/rubinius/bad-encode.rb:
source sequence is illegal/malformed utf-8 (JSON::GeneratorError)

Backtrace:

Object#__script__ at /Users/ryo-onodera/rubinius/bad-encode.rb:5
Rubinius::CodeLoader#load_script at kernel/delta/code_loader.rb:66
Rubinius::CodeLoader.load_script at kernel/delta/code_loader.rb:152
Rubinius::Loader#script at kernel/loader.rb:645
Rubinius::Loader#main at kernel/loader.rb:799
```

## After

```
bash-3.2$ ./bin/rbx ~/rubinius/bad-encode.rb
["70g・1パック", 16, #]
```

(*The final ByteArray size is aligned by ObjectHeader::align)