Skip to content

Commit 8d54be1

Browse files
author
svorenova
committed
Adding exception and tests for missing closing delimiter
1 parent 608c6b6 commit 8d54be1

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

src/java_bytecode/java_bytecode_convert_class.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void java_bytecode_convert_classt::convert(const classt &c)
115115
}
116116
class_type=generic_class_type;
117117
}
118-
catch(unsupported_java_class_siganture_exceptiont)
118+
catch(unsupported_java_class_signature_exceptiont)
119119
{
120120
warning() << "we currently don't support parsing for example double "
121121
"bounded, recursive and wild card generics" << eom;

src/java_bytecode/java_local_variable_table.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ void java_bytecode_convert_methodt::setup_local_variables(
775775
{
776776
t=java_type_from_string(v.var.signature.value());
777777
}
778-
catch(unsupported_java_class_siganture_exceptiont &e)
778+
catch(unsupported_java_class_signature_exceptiont &e)
779779
{
780780
t=java_type_from_string(v.var.descriptor);
781781
}

src/java_bytecode/java_types.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ typet java_type_from_string(
209209
// string:
210210
// (TT;TU;I)V
211211
size_t closing_generic=find_closing_delimiter(src, 0, '<', '>');
212+
if(closing_generic==std::string::npos)
213+
{
214+
throw unsupported_java_class_signature_exceptiont(
215+
"Failed to find generic signature closing delimiter");
216+
}
212217
const typet &method_type=java_type_from_string(
213218
src.substr(closing_generic+1, std::string::npos), class_name_prefix);
214219

@@ -332,7 +337,7 @@ typet java_type_from_string(
332337
{
333338
std::size_t e_pos=find_closing_delimiter(src, f_pos, '<', '>');
334339
if(e_pos==std::string::npos)
335-
throw unsupported_java_class_siganture_exceptiont(
340+
throw unsupported_java_class_signature_exceptiont(
336341
"recursive generic");
337342

338343
// construct container type
@@ -431,7 +436,7 @@ typet java_type_from_string(
431436
#ifdef DEBUG
432437
std::cout << class_name_prefix << std::endl;
433438
#endif
434-
throw unsupported_java_class_siganture_exceptiont("wild card generic");
439+
throw unsupported_java_class_signature_exceptiont("wild card generic");
435440
}
436441
default:
437442
return nil_typet();
@@ -489,7 +494,7 @@ std::vector<typet> java_generic_type_from_string(
489494
std::string signature(src.substr(1, signature_end-1));
490495

491496
if(signature.find(";:")!=std::string::npos)
492-
throw unsupported_java_class_siganture_exceptiont("multiple bounds");
497+
throw unsupported_java_class_signature_exceptiont("multiple bounds");
493498

494499
PRECONDITION(signature[signature.size()-1]==';');
495500

src/java_bytecode/java_types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ inline const typet &java_generics_class_type_bound(
354354

355355
/// An exception that is raised for unsupported class signature.
356356
/// Currently we do not parse multiple bounds.
357-
class unsupported_java_class_siganture_exceptiont:public std::logic_error
357+
class unsupported_java_class_signature_exceptiont:public std::logic_error
358358
{
359359
public:
360-
explicit unsupported_java_class_siganture_exceptiont(std::string type):
360+
explicit unsupported_java_class_signature_exceptiont(std::string type):
361361
std::logic_error(
362362
"Unsupported class signature: "+type)
363363
{
@@ -373,7 +373,7 @@ inline typet java_type_from_string_with_exception(
373373
{
374374
return java_type_from_string(signature.value(), class_name);
375375
}
376-
catch (unsupported_java_class_siganture_exceptiont &e)
376+
catch (unsupported_java_class_signature_exceptiont &e)
377377
{
378378
return java_type_from_string(descriptor, class_name);
379379
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for parsing generic classes
4+
5+
Author: DiffBlue Limited. All rights reserved.
6+
7+
Note: Created by fotis on 09/10/2017.
8+
9+
\*******************************************************************/
10+
11+
#include <string>
12+
#include <vector>
13+
14+
#include <catch.hpp>
15+
16+
#include <java_bytecode/java_utils.h>
17+
18+
SCENARIO("Test that the generic signature delimiter lookup works reliably",
19+
"[core][java_util_test]")
20+
{
21+
GIVEN("Given the signatures of some generic classes")
22+
{
23+
std::vector<std::string> generic_sigs
24+
{
25+
// Valid inputs
26+
"List<Integer>",
27+
"HashMap<String, Integer>",
28+
"List<List<Int>>",
29+
"List<List<List<Int>>>",
30+
// Invalid inputs
31+
"<",
32+
"List<Integer",
33+
"List<List<Integer>",
34+
};
35+
36+
WHEN("We check if the closing tag is recognised correctly")
37+
{
38+
// TEST VALID CASES
39+
40+
// List<Integer>
41+
REQUIRE(find_closing_delimiter(generic_sigs[0], 4, '<', '>')==12);
42+
// HashMap<String, Integer>
43+
REQUIRE(find_closing_delimiter(generic_sigs[1], 7, '<', '>')==23);
44+
// List<List<Int>>
45+
REQUIRE(find_closing_delimiter(generic_sigs[2], 4, '<', '>')==14);
46+
REQUIRE(find_closing_delimiter(generic_sigs[2], 9, '<', '>')==13);
47+
// List<List<List<Int>>>
48+
REQUIRE(find_closing_delimiter(generic_sigs[3], 14, '<', '>')==18);
49+
50+
// TEST INVALID CASES
51+
52+
// <
53+
REQUIRE(find_closing_delimiter(generic_sigs[4], 0, '<', '>')
54+
==std::string::npos);
55+
// List<Integer
56+
REQUIRE(find_closing_delimiter(generic_sigs[5], 4, '<', '>')
57+
==std::string::npos);
58+
// List<List<Integer>
59+
// (make sure that we still recognise the first delimiter correctly)
60+
REQUIRE(find_closing_delimiter(generic_sigs[6], 4, '<', '>')
61+
==std::string::npos);
62+
REQUIRE(find_closing_delimiter(generic_sigs[6], 9, '<', '>')==17);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)