Skip to content

Commit db49073

Browse files
authored
Merge pull request #29 from levyfan/dev
add illegal id check in idToPiece
2 parents 23ee83c + 5d56fe2 commit db49073

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ on: [push, pull_request]
55
jobs:
66
build:
77
strategy:
8-
max-parallel: 10
9-
fail-fast: true
108
matrix:
119
os: [ubuntu-latest, macos-latest, windows-latest]
12-
java: [8, 9, 10, 11]
1310

1411
runs-on: ${{ matrix.os }}
1512

1613
steps:
17-
- uses: actions/checkout@v1
18-
- name: set up JDK
19-
uses: actions/setup-java@v1
14+
- uses: actions/checkout@v3
15+
- name: Set up JDK 11
16+
uses: actions/setup-java@v3
2017
with:
21-
java-version: ${{ matrix.java }}
22-
- name: "Build"
23-
run: |
24-
mvn clean install
25-
18+
java-version: '11'
19+
distribution: 'adopt'
20+
- name: Build with Maven
21+
run: mvn clean install

‎src/main/native/com_github_google_sentencepiece_SentencePieceJNI.cc‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,11 @@ JNIEXPORT jint JNICALL Java_com_github_google_sentencepiece_SentencePieceJNI_spp
533533
JNIEXPORT jstring JNICALL Java_com_github_google_sentencepiece_SentencePieceJNI_sppIdToPiece
534534
(JNIEnv *env, jclass cls, jlong ptr, jint id) {
535535
auto* spp = (SentencePieceProcessor*) ptr;
536-
536+
if (id < 0 || id >= spp->GetPieceSize()) {
537+
jclass cls = env->FindClass("com/github/google/sentencepiece/SentencePieceException");
538+
env->ThrowNew(cls, "id must be 0 <= id < GetPieceSize()");
539+
return nullptr;
540+
}
537541
const std::string &piece = spp->IdToPiece(id);
538542
return stringToJstring(env, piece);
539543
}

‎src/test/java/com/github/google/sentencepiece/SentencePieceProcessorTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ public void testSerializedProto() {
109109
Assert.assertNotEquals(none, sp.decodePiecesAsSerializedProto(Arrays.asList("foo", "bar")));
110110
Assert.assertNotEquals(none, sp.decodeIdsAsSerializedProto(20, 30));
111111
}
112+
113+
@Test
114+
public void testIllegalId() {
115+
Assert.assertThrows(SentencePieceException.class, () -> sp.idToPiece(38067));
116+
}
112117
}

0 commit comments

Comments
 (0)