Skip to content

Commit 8050fd9

Browse files
[issue-771] fix license expression error handling in tag-value parser
Signed-off-by: Armin Tänzer <[email protected]>
1 parent 32e74cd commit 8050fd9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/spdx_tools/spdx/parser/tagvalue/parser.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import re
1515

1616
from beartype.typing import Any, Dict, List
17-
from license_expression import get_spdx_licensing
17+
from license_expression import ExpressionError, get_spdx_licensing
1818
from ply import yacc
1919
from ply.yacc import LRParser
2020

@@ -233,7 +233,13 @@ def p_none(self, p):
233233

234234
@grammar_rule("license_or_no_assertion_or_none : LINE")
235235
def p_license(self, p):
236-
p[0] = get_spdx_licensing().parse(p[1])
236+
try:
237+
p[0] = get_spdx_licensing().parse(p[1])
238+
except ExpressionError as err:
239+
error_message = f"Error while parsing license expression: {p[1]}"
240+
if err.args:
241+
error_message += f": {err.args[0]}"
242+
self.current_element["logger"].append(error_message)
237243

238244
@grammar_rule("actor_or_no_assertion : PERSON_VALUE\n | ORGANIZATION_VALUE")
239245
def p_actor_values(self, p):

tests/spdx/parser/tagvalue/test_tag_value_parser.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,41 @@ def test_document_with_mixed_values():
9898
"Element Package is not the current element in scope, probably the expected "
9999
"tag to start the element (PackageName) is missing. Line: 4"
100100
]
101+
102+
103+
def test_faulty_license_expression():
104+
parser = Parser()
105+
document_str = "\n".join(
106+
[
107+
f"SPDXID:{DOCUMENT_SPDX_ID}",
108+
"FileName: File with faulty license expression",
109+
"SPDXID: SPDXRef-File",
110+
"FileChecksum: SHA1: d6a770ba38583ed4bb4525bd96e50461655d2759",
111+
"LicenseConcluded: LicenseRef-foo/bar",
112+
"PackageName: Package with faulty license expression",
113+
"SPDXID: SPDXRef-Package",
114+
"PackageDownloadLocation: www.download.com",
115+
"PackageLicenseConcluded: LicenseRef-bar/foo",
116+
"SnippetSPDXID: SPDXRef-Snippet",
117+
"SnippetName: Snippet with faulty license expression",
118+
"SnippetLicenseConcluded: LicenseRef-foo/foo",
119+
]
120+
)
121+
122+
with pytest.raises(SPDXParsingError) as err:
123+
parser.parse(document_str)
124+
125+
assert err.value.get_messages() == [
126+
'Error while parsing File: ["Error while parsing license expression: '
127+
"LicenseRef-foo/bar: Invalid license key: the valid characters are: letters "
128+
"and numbers, underscore, dot, colon or hyphen signs and spaces: "
129+
"'LicenseRef-foo/bar'\"]",
130+
'Error while parsing Package: ["Error while parsing license expression: '
131+
"LicenseRef-bar/foo: Invalid license key: the valid characters are: letters "
132+
"and numbers, underscore, dot, colon or hyphen signs and spaces: "
133+
"'LicenseRef-bar/foo'\"]",
134+
'Error while parsing Snippet: ["Error while parsing license expression: '
135+
"LicenseRef-foo/foo: Invalid license key: the valid characters are: letters "
136+
"and numbers, underscore, dot, colon or hyphen signs and spaces: "
137+
"'LicenseRef-foo/foo'\"]",
138+
]

0 commit comments

Comments
 (0)