Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fefb204

Browse files
authored
Merge pull request #5004 from maxonfjvipon/bug/#3538/bytes-slice-out-of-bounds
bug(#3538): throw error on `bytes.slice` with out of bounds
2 parents 6f1bb10 + b7a415a commit fefb204

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

eo-runtime/src/main/eo/bytes.eo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@
504504
[] +> throws-on-bytes-of-wrong-size-as-i64
505505
01-01-01-01.as-i64 > @
506506

507+
# Tests that slicing out of bounds throws an error.
508+
[] +> throws-on-out-of-bounds-slice
509+
20-1F-EE-B5-90.slice 3 10 > @
510+
507511
# Tests that bytes can be correctly converted to i64 integer.
508512
[] +> tests-bytes-converts-to-i64
509513
eq. > @

eo-runtime/src/main/java/org/eolang/EObytes$EOslice.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public final class EObytes$EOslice extends PhDefault implements Atom {
3030
}
3131

3232
@Override
33-
@SuppressWarnings("PMD.UnnecessaryLocalRule")
3433
public Phi lambda() {
34+
final byte[] bytes = new Dataized(this.take(Phi.RHO)).take();
3535
final int start = Expect.at(this, "start")
3636
.that(phi -> new Dataized(phi).asNumber())
3737
.otherwise("must be a number")
@@ -49,12 +49,11 @@ public Phi lambda() {
4949
.otherwise("must be an integer")
5050
.must(integer -> integer >= 0)
5151
.otherwise("must be a positive integer")
52-
.it();
53-
return new Data.ToPhi(
54-
Arrays.copyOfRange(
55-
new Dataized(this.take(Phi.RHO)).take(),
56-
start, start + length
52+
.must(integer -> start + integer <= bytes.length)
53+
.otherwise(
54+
String.format("is out of bounds for bytes of size %d", bytes.length)
5755
)
58-
);
56+
.it();
57+
return new Data.ToPhi(Arrays.copyOfRange(bytes, start, start + length));
5958
}
6059
}

eo-runtime/src/test/java/org/eolang/EObytesEOsliceTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ void takesLegalSlice() {
4646
);
4747
}
4848

49+
@Test
50+
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
51+
void throwsOnOutOfBoundsSlice() {
52+
MatcherAssert.assertThat(
53+
"error message is correct for out of bounds slice",
54+
new UncheckedText(
55+
new TextOf(
56+
Assertions.assertThrows(
57+
EOerror.ExError.class,
58+
() -> new Dataized(
59+
new PhWith(
60+
new PhWith(
61+
new Data.ToPhi("hello")
62+
.take("as-bytes")
63+
.take("slice")
64+
.copy(),
65+
"start",
66+
new Data.ToPhi(3)
67+
),
68+
"len",
69+
new Data.ToPhi(10)
70+
)
71+
).asString(),
72+
"doesnt throw on out of bounds slice"
73+
)
74+
)
75+
).asString(),
76+
Matchers.containsString("is out of bounds for bytes of size 5")
77+
);
78+
}
79+
4980
@Test
5081
@SuppressWarnings("PMD.UnitTestContainsTooManyAsserts")
5182
void takesWrongSlice() {

0 commit comments

Comments
 (0)