Skip to content

Commit 02819a1

Browse files
bactgoneall
authored andcommitted
Check null to avoid null pointer exception
Check if the string input is null in `getFirstLicenseToken` and `isSingleTokenString` Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
1 parent 3ec6b34 commit 02819a1

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,25 @@ public static String locateOriginalText(String fullLicenseText, int startToken,
190190
}
191191
// ignore
192192
}
193-
194-
/*
195-
* @param text text to test
196-
* @return the first token in the license text
193+
194+
/**
195+
* Return the first license token found in the given text
196+
* <p>
197+
* The method normalizes the input text, removes comment characters,
198+
* and splits it into tokens
199+
* using {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
200+
* It returns the first non-empty token found,
201+
* or {@code null} if no such token exists.
202+
* </p>
203+
*
204+
* @param text The license text to extract the first token from.
205+
* @return The first non-empty token as a {@link String},
206+
* or {@code null} if none is found.
197207
*/
198208
public static String getFirstLicenseToken(String text) {
209+
if (text == null) {
210+
return null;
211+
}
199212
String textToTokenize = LicenseTextHelper.normalizeText(LicenseTextHelper.replaceMultWord(LicenseTextHelper.replaceSpaceComma(
200213
LicenseTextHelper.removeLineSeparators(removeCommentChars(text))))).toLowerCase();
201214
Matcher m = LicenseTextHelper.TOKEN_SPLIT_PATTERN.matcher(textToTokenize);
@@ -206,12 +219,23 @@ public static String getFirstLicenseToken(String text) {
206219
}
207220
return null;
208221
}
209-
222+
210223
/**
211-
* @param text text to test
212-
* @return true if the text contains a single token
224+
* Check whether the given text contains only a single token
225+
* <p>
226+
* A single token string is defined as a non-null string that does not
227+
* contain any newline characters and contains exactly one non-empty token
228+
* as identified by the {@link LicenseTextHelper#TOKEN_SPLIT_PATTERN}.
229+
* </p>
230+
*
231+
* @param text The text to test.
232+
* @return {@code true} if the text contains a single token,
233+
* {@code false} otherwise.
213234
*/
214235
public static boolean isSingleTokenString(String text) {
236+
if (text == null) {
237+
return false;
238+
}
215239
if (text.contains("\n")) {
216240
return false;
217241
}

src/test/java/org/spdx/utility/compare/LicenseCompareHelperTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,18 +539,19 @@ public void testLicenseEqualsNoneLicense() throws InvalidSPDXAnalysisException,
539539
Map<String, String> xlationMap = new HashMap<>();;
540540
assertTrue(LicenseCompareHelper.isLicenseEqual(lic3, lic4, xlationMap));
541541
assertFalse(LicenseCompareHelper.isLicenseEqual(lic4, lic2, xlationMap));
542-
}
543-
544-
542+
}
543+
545544
public void testisSingleTokenString() {
545+
assertTrue(LicenseCompareHelper.isSingleTokenString(""));
546546
assertTrue(LicenseCompareHelper.isSingleTokenString(" token "));
547547
assertTrue(LicenseCompareHelper.isSingleTokenString("'"));
548548
assertTrue(LicenseCompareHelper.isSingleTokenString(" '"));
549549
assertTrue(LicenseCompareHelper.isSingleTokenString("' "));
550550
assertFalse(LicenseCompareHelper.isSingleTokenString("a and"));
551551
assertFalse(LicenseCompareHelper.isSingleTokenString("a\nand"));
552+
assertFalse(LicenseCompareHelper.isSingleTokenString(null));
552553
}
553-
554+
554555
public void regressionTestMatchingGpl20Only() throws IOException, InvalidSPDXAnalysisException, SpdxCompareException {
555556
String compareText = UnitTestHelper.fileToText(GPL_2_TEXT);
556557
DifferenceDescription result = LicenseCompareHelper.isTextStandardLicense(LicenseInfoFactory.getListedLicenseById("GPL-2.0-only"), compareText);
@@ -568,11 +569,16 @@ public void testMatchingStandardLicenseIds() throws IOException, InvalidSPDXAnal
568569
assertTrue(result[3].startsWith("GPL-2"));
569570
}
570571
}
571-
572+
572573
public void testFirstLicenseToken() {
573574
assertEquals("first", LicenseCompareHelper.getFirstLicenseToken(" first,token that is needed\nnext"));
575+
assertEquals("first", LicenseCompareHelper.getFirstLicenseToken("// first,second"));
576+
assertNull(LicenseCompareHelper.getFirstLicenseToken(null));
577+
assertNull(LicenseCompareHelper.getFirstLicenseToken(""));
578+
assertNull(LicenseCompareHelper.getFirstLicenseToken(" <!-- -->"));
579+
assertNull(LicenseCompareHelper.getFirstLicenseToken("# "));
574580
}
575-
581+
576582
@SuppressWarnings("unused")
577583
private String stringCharToUnicode(String s, int location) {
578584
return "\\u" + Integer.toHexString(s.charAt(location) | 0x10000).substring(1);

0 commit comments

Comments
 (0)