Skip to content

Check null to avoid null pointer exception #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 31, 2025

Conversation

bact
Copy link
Collaborator

@bact bact commented May 21, 2025

Check if the string input is null in getFirstLicenseToken and isSingleTokenString

To fix #333

Check if the string input is null in `getFirstLicenseToken` and `isSingleTokenString`

Signed-off-by: Arthit Suriyawongkul <[email protected]>
@bact bact added the bug Something isn't working label May 21, 2025
Copy link
Member

@goneall goneall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're now using a Java version that support the @Nullable annotations, we should add these to the calls.

Do we want to open a separate PR to fix the empty optional strings returning true when it should be false?

bact and others added 2 commits May 24, 2025 20:18
@bact
Copy link
Collaborator Author

bact commented May 24, 2025

Do we want to open a separate PR to fix the empty optional strings returning true when it should be false?

We can fix it here as well.
I also have to update the unit test to expect false when the string is empty.

@goneall
Copy link
Member

goneall commented May 25, 2025

Do we want to open a separate PR to fix the empty optional strings returning true when it should be false?

We can fix it here as well. I also have to update the unit test to expect false when the string is empty.

It looks like there are quite a few unit test failures. It looks like I was wrong about returning false - @bact - try returning true for empty and null and see if it passes the CI.

@bact
Copy link
Collaborator Author

bact commented May 25, 2025

Thanks @goneall. I'm trying to understand the rationale behind isSingleTokenString() method.

In general, I think we like to return true if the text contains zero or one token (and what constitute token does not include whitespaces and certain punctuations) -- is this correct?

Another thing I'm not sure about is this line, on why we treat newline differently from other whitespaces:

if (text.contains("\n")) {
return false;
}

Do you mind to help me confirm this truth table please?

text tokens (m.group()) tokens (m.group(1)) isSingleTokenString(text)
"token" token token true
" token" , token (empty), token true
"\ntoken" \n, token (empty), token true ?
"token\n" token\n token true ?
"'''token" ''', token (empty), token true
"token'''" token''' token true
"a and" a , and a, and false
"a\nand" a\n, and a, and false
"Apache-2.0" Apache-2., 0 Apache-2, 0 false
" " (two spaces) (two spaces) (empty) true
" " (one space) (one space) (empty) true
"\n" \n (empty) true ?
"" (empty) (empty) true
null n/a n/a true

bact added 5 commits May 26, 2025 01:02
Signed-off-by: Arthit Suriyawongkul <[email protected]>
Signed-off-by: Arthit Suriyawongkul <[email protected]>
Signed-off-by: Arthit Suriyawongkul <[email protected]>
Signed-off-by: Arthit Suriyawongkul <[email protected]>
Signed-off-by: Arthit Suriyawongkul <[email protected]>
@bact bact requested a review from goneall May 29, 2025 11:19
@goneall
Copy link
Member

goneall commented May 31, 2025

@bact - the table above looks correct

This code was originally added to fix a corner case in the matching. I believe the actual check is to see if a text field contains more than one token. If it contains more than one token, we need to do some extra processing.

Copy link
Member

@goneall goneall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thans @bact for the analysis and fix.

This seems to pass the unit tests.

We could add more documentation that the isSingleToken is really returning 0 or 1 tokens, but that can be done in a separate PR.

I'll go ahead and merge this so we can move forward with a working solution.

@goneall goneall merged commit 662b4f8 into spdx:master May 31, 2025
1 check passed
@bact bact deleted the fix-null-pointer-isSingleTokenString branch May 31, 2025 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPE on matching templates with no text in optional rule
2 participants