pavelliavonau/cmakeconverter

VCXParser crashes when encountering an empty Filter node in a filter file

MarkKoz opened this issue · 0 comments

Describe the bug

VCXParser crashes when encountering an empty Filter node in a filter file. It crashes when trying to do a string replacement on the node's text. The crash occurs because it naïvely assumes that the node will always contain text; it does not perform a check for None. The crash occurs here

return filter_node[0].text.replace('\\', '\\\\')

with the following traceback

Traceback (most recent call last):
  File "...\python\current\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "...\python\current\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "...\.venv\Scripts\cmake-converter.exe\__main__.py", line 7, in <module>
  File "...\.venv\lib\site-packages\cmake_converter\main.py", line 144, in main
    converter.convert_solution(project_context, os.path.abspath(args.solution))
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\solution.py", line 331, in convert_solution
    results = self.do_conversion(project_context, input_data_for_converter)
  File "...\.venv\lib\site-packages\cmake_converter\data_converter.py", line 301, in do_conversion
    results.append(self.run_conversion(data_for_converter))
  File "...\.venv\lib\site-packages\cmake_converter\data_converter.py", line 266, in run_conversion
    converted = self.convert_project(
  File "...\.venv\lib\site-packages\cmake_converter\data_converter.py", line 235, in convert_project
    self.collect_data(context)
  File "...\.venv\lib\site-packages\cmake_converter\data_converter.py", line 53, in collect_data
    context.parser.parse(context)
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\vcxproj\parser.py", line 145, in parse
    self._parse_nodes(context, root)
  File "...\.venv\lib\site-packages\cmake_converter\parser.py", line 101, in _parse_nodes
    node_handlers[child_node_tag](context, child_node)
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\vcxproj\parser.py", line 154, in __parse_item_group
    self._parse_nodes(context, node)
  File "...\.venv\lib\site-packages\cmake_converter\parser.py", line 92, in _parse_nodes
    self._parse_attributes(context, child_node)
  File "...\.venv\lib\site-packages\cmake_converter\parser.py", line 117, in _parse_attributes
    attributes_handlers[node_key](context, node_key, node.get(attr), node)
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\vcxproj\parser.py", line 222, in __parse_other_files_include_attr
    self.__parse_file_nodes(context, context.other_project_files, none_node, '')
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\vcxproj\parser.py", line 228, in __parse_file_nodes
    source_group = self.__get_source_group_from_filters(
  File "...\.venv\lib\site-packages\cmake_converter\visual_studio\vcxproj\parser.py", line 200, in __get_source_group_from_filters
    return filter_node[0].text.replace('\\', '\\\\')
AttributeError: 'NoneType' object has no attribute 'replace'

I'm running version 2.1.0 on Python 3.9.2.

To Reproduce

I can't provide a complete example with a solution that will reproduce this. However, here is an example of a filter file that would cause this error:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="file.ext">
      <Filter /> <!-- Notice this is empty! -->
    </None>
  </ItemGroup>
</Project>

Assuming this filter file exists alongside a project that is in a solution solution.sln, running cmake-converter -s solution.sln would reveal this error.

Expected behavior
It should check that the node's text isn't None to avoid a crash. In the case that it's None, it should probably return an empty string, but I'm not familiar enough with this package to determine the most appropriate solution.

Additional context
The solution I'm trying to convert was generated by an external tool rather than directly by Visual Studio. This is why there may be some inconsistencies with assumptions made for Visual Studio-generated projects.