Skip to content

Commit e4a6539

Browse files
committed
Fix index error.
1 parent 7501aa2 commit e4a6539

File tree

2 files changed

+13
-9
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes

2 files changed

+13
-9
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def _test_join(basetype, type2test):
259259
def test_join():
260260
_test_join(bytes, BytesSubclass)
261261
_test_join(bytearray, ByteArraySubclass)
262+
assert b"--".join([]) == b""
263+
assert b"--".join([b"hello"]) == b"hello"
262264

263265

264266
def test_setslice():

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,19 @@ private static int append(ArrayList<byte[]> parts, byte[] barr) {
9696

9797
@TruffleBoundary(allowInlining = true, transferToInterpreterOnException = false)
9898
private static byte[] joinArrays(byte[] sep, ArrayList<byte[]> parts, int partsTotalSize) {
99-
byte[] joinedBytes = new byte[partsTotalSize + (parts.size() - 1) * sep.length];
100-
int offset = 0;
101-
byte[] array = parts.get(0);
102-
System.arraycopy(array, 0, joinedBytes, offset, array.length);
103-
offset += array.length;
104-
for (int i = 1; i < parts.size(); i++) {
105-
array = parts.get(i);
106-
System.arraycopy(sep, 0, joinedBytes, offset, sep.length);
107-
offset += sep.length;
99+
byte[] joinedBytes = new byte[Math.max(0, partsTotalSize + (parts.size() - 1) * sep.length)];
100+
if (parts.size() > 0) {
101+
int offset = 0;
102+
byte[] array = parts.get(0);
108103
System.arraycopy(array, 0, joinedBytes, offset, array.length);
109104
offset += array.length;
105+
for (int i = 1; i < parts.size(); i++) {
106+
array = parts.get(i);
107+
System.arraycopy(sep, 0, joinedBytes, offset, sep.length);
108+
offset += sep.length;
109+
System.arraycopy(array, 0, joinedBytes, offset, array.length);
110+
offset += array.length;
111+
}
110112
}
111113
return joinedBytes;
112114
}

0 commit comments

Comments
 (0)