Skip to content

Commit

Permalink
fix dynamic-type-mismatch UBSAN error in t_json_generator.cc
Browse files Browse the repository at this point in the history
Summary:
Exposed by UBSAN:
```lang=bash
thrift/compiler/generate/t_json_generator.cc:278:25: runtime error: downcast of address 0x610000000a40 which does not point to an object of type 't_set'
0x610000000a40: note: object is of type '6t_list'
 03 00 80 34  28 b9 74 8a 5f 7f 00 00  58 0a 00 00 00 61 00 00  00 00 00 00 00 00 00 00  00 be be be
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for '6t_list'
    #0 0x7f5f8b1ec21d in t_json_generator::type_to_spec_args[abi:cxx11](t_type*) thrift/compiler/generate/t_json_generator.cc:278
    #1 0x7f5f8b1ece84 in t_json_generator::print_type(t_type*) thrift/compiler/generate/t_json_generator.cc:293
    #2 0x7f5f8b1f3de0 in t_json_generator::generate_struct(t_struct*) thrift/compiler/generate/t_json_generator.cc:484
    #3 0x7f5f8b1e66aa in t_json_generator::generate_program() thrift/compiler/generate/t_json_generator.cc:161
    #4 0x42ee58 in generate(t_program*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<cha
r, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::
basic_string<char, std::char_traits<char>, std::allocator<char> > > >&) thrift/compiler/main.cc:305
    #5 0x42b88e in main thrift/compiler/main.cc:681
    #6 0x7f5f8734b857 in __libc_start_main /home/engshare/third-party2/glibc/2.23/src/glibc-2.23/csu/../csu/libc-start.c:289
    #7 0x425a28 in _start /home/engshare/third-party2/glibc/2.23/src/glibc-2.23/csu/../sysdeps/x86_64/start.S:118

UndefinedBehaviorSanitizer: dynamic-type-mismatch thrift/compiler/generate/t_json_generator.cc:278:25
```
The fix is inspired by https://github.com/apache/thrift/pull/1222/files

Reviewed By: yfeldblum

Differential Revision: D8154131

fbshipit-source-id: 0ff7d993b87c0fe20652865a89a2bb205dde10e3
  • Loading branch information
Igor Sugak authored and facebook-github-bot committed Jun 8, 2018
1 parent 3a3db8e commit 65b903f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion thrift/compiler/generate/t_json_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,17 @@ string t_json_generator::type_to_spec_args(t_type* ttype) {
+ "\", \"spec_args\" : "
+ type_to_spec_args(((t_map*)ttype)->get_val_type())
+ "} } ";
} else if (ttype->is_set() || ttype->is_list()) {
} else if (ttype->is_set()) {
return "{ \"type_enum\" : \""
+ type_to_string(((t_set*)ttype)->get_elem_type())
+ "\", \"spec_args\" : "
+ type_to_spec_args(((t_set*)ttype)->get_elem_type())
+ "} ";
} else if (ttype->is_list()) {
return "{ \"type_enum\" : \"" +
type_to_string(((t_list*)ttype)->get_elem_type()) +
"\", \"spec_args\" : " +
type_to_spec_args(((t_list*)ttype)->get_elem_type()) + "} ";
}

throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name();
Expand Down

0 comments on commit 65b903f

Please sign in to comment.