Skip to content

Commit f6fffda

Browse files
Avoiding array out of bounds
1 parent bd8208d commit f6fffda

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/Identity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ public String strValue() {
280280
public static Identity valueOf(String identityStr) {
281281
String[] info = identityStr.split(":");
282282
Type type = Type.valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, info[0]));
283-
if (type.equals(Type.ALL_AUTHENTICATED_USERS) || type.equals(Type.ALL_USERS)) {
283+
if (info.length == 1) {
284284
return new Identity(type, null);
285+
} else if (info.length == 2){
286+
return new Identity(type, info[1]);
285287
} else {
286-
return new Identity(type, checkNotNull(info[1]));
288+
throw new IllegalArgumentException("Illegal identity string: \"" + identityStr + "\"");
287289
}
288290
}
289291
}

google-cloud-core/src/main/java/com/google/cloud/StringEnumType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public EnumT valueOfStrict(String constant) {
6666
* Get the enum object for the given String constant, and allow unrecognized values.
6767
*/
6868
public EnumT valueOf(String constant) {
69+
if (constant == null || constant.isEmpty()) {
70+
throw new IllegalArgumentException("Empty enum constants not allowed.");
71+
}
6972
EnumT value = knownValues.get(constant);
7073
if (value != null) {
7174
return value;

google-cloud-core/src/test/java/com/google/cloud/IdentityTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ public void testIdentityToAndFromPb() {
142142
compareIdentities(PROJECT_VIEWER, Identity.valueOf(PROJECT_VIEWER.strValue()));
143143
}
144144

145+
@Test(expected = IllegalArgumentException.class)
146+
public void testValueOfEmpty() {
147+
Identity.valueOf("");
148+
}
149+
150+
@Test(expected = IllegalArgumentException.class)
151+
public void testValueOfThreePart() {
152+
Identity.valueOf("a:b:c");
153+
}
154+
155+
@Test
156+
public void testUnrecognizedToString() {
157+
assertEquals("a:b", Identity.valueOf("a:b").strValue());
158+
}
159+
145160
private void compareIdentities(Identity expected, Identity actual) {
146161
assertEquals(expected, actual);
147162
assertEquals(expected.getType(), actual.getType());

0 commit comments

Comments
 (0)