Skip to content

Commit 2116bb5

Browse files
committed
fix(coap-core): handle null in getCustomOption method
1 parent 4a7719b commit 2116bb5

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2025 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -133,7 +133,7 @@ protected boolean parseOption(int type, Opaque data) {
133133
* @return byte array value or null if does not exist
134134
*/
135135
public Opaque getCustomOption(Integer optNumber) {
136-
if (!unrecognizedOptions.containsKey(optNumber)) {
136+
if (unrecognizedOptions == null || !unrecognizedOptions.containsKey(optNumber)) {
137137
return null;
138138
}
139139
return unrecognizedOptions.get(optNumber).getFirstValue();

coap-core/src/test/java/com/mbed/coap/packet/HeaderOptionsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2025 java-coap contributors (https://github.com/open-coap/java-coap)
2+
* Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap)
33
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
44
* SPDX-License-Identifier: Apache-2.0
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -533,6 +533,13 @@ void correlationOptionShouldBeElectiveAndSafeToForward() {
533533
assertFalse(hasNoCacheKey(HeaderOptions.OPEN_COAP_CORRELATION_TAG));
534534
}
535535

536+
@Test
537+
void getCustomOption_shouldReturnNull_whenNoCustomOptionsSet() {
538+
BasicHeaderOptions hdr = new BasicHeaderOptions();
539+
540+
assertNull(hdr.getCustomOption(100));
541+
}
542+
536543
private static byte[] serialize(BasicHeaderOptions hdr) throws IOException, CoapException {
537544
ByteArrayOutputStream baos = new ByteArrayOutputStream();
538545
hdr.serialize(baos);

0 commit comments

Comments
 (0)