microsoft / ifc

SDK for the IFC specification at https://github.com/microsoft/ifc-spec
Other
57 stars 15 forks source link

Don't throw exception of built-in types #33

Open GabrielDosReis opened 1 year ago

GabrielDosReis commented 1 year ago

During the review of #30 , @menuet correctly observed there is existing code that violated Core Guidelines E.14. That should have been caught before it was checked in. Upon further logic inspection, it looks like that the Reader constructor was detecting too late an erroneous condition. Move that detection earlier, at the very minimum, at the site of the Reader object construction.

whoan commented 1 year ago

I am facing the same issue discussed in #30.

This is the very simple code I used:

#include <iostream>

int main() {
    std::cout << "Hello world" << std::endl;
    return 0;
}

And I slightly modified the exception handling in ifc-printer to be able to debug:

--- a/src/ifc-printer/main.cxx
+++ b/src/ifc-printer/main.cxx
@@ -140,8 +140,8 @@ int main(int argc, char** argv)
         for (const auto& file : arguments.files)
             process_ifc(file, arguments.options);
     }
-    catch (...)
+    catch (const std::exception& ex)
     {
-        translate_exception();
+        std::cerr << ex.what() << std::endl;
     }
 }

This is the stack trace in case it helps to fix the issue:

(gdb) bt
#0  __restore_sigs (set=set@entry=0x7ffdf4878cc0) at ./arch/x86_64/syscall_arch.h:40
#1  0x00007fa30ba44e5c in raise (sig=sig@entry=6) at src/signal/raise.c:11
#2  0x00007fa30ba17fa8 in abort () at src/exit/abort.c:11
#3  0x00007fa30b863785 in ?? () from /usr/lib/libstdc++.so.6
#4  0x00007fa30b86e51a in __cxxabiv1::__terminate(void (*)()) () from /usr/lib/libstdc++.so.6
#5  0x00007fa30b86e585 in std::terminate() () from /usr/lib/libstdc++.so.6
#6  0x00007fa30b86e7d7 in __cxa_throw () from /usr/lib/libstdc++.so.6
#7  0x0000562c58c28aef in ifc::(anonymous namespace)::offset<ifc::ExprSort, 61> (table=..., name=...) at /ifc-main/src/sgraph.cxx:549
#8  0x0000562c58c27f1a in ifc::(anonymous namespace)::entry_by_name<ifc::(anonymous namespace)::exprsort_table, &ifc::TableOfContents::exprs> (toc=..., name=...) at /ifc-main/src/sgraph.cxx:564
#9  0x0000562c58c27a3d in ifc::summary_by_partition_name (toc=..., name=...) at /ifc-main/src/sgraph.cxx:710
#10 0x0000562c58c38680 in ifc::Reader::read_table_of_contents (this=0x7ffdf48790c0) at /ifc-main/src/ifc-reader/reader.cxx:33
#11 0x0000562c58c3853f in ifc::Reader::Reader (this=0x7ffdf48790c0, ifc_=...) at /ifc-main/src/ifc-reader/reader.cxx:14
#12 0x0000562c58bdef41 in process_ifc (name=..., options=ifc::util::PrintOptions::None) at /ifc-main/src/ifc-printer/main.cxx:115
#13 0x0000562c58bdf1a2 in main (argc=2, argv=0x7ffdf487a898) at /ifc-main/src/ifc-printer/main.cxx:141
(gdb) frame 9
#9  0x0000562c58c27a3d in ifc::summary_by_partition_name (toc=..., name=...) at /ifc-main/src/sgraph.cxx:710
710                 return entry.dispatcher(toc, name);
(gdb) p name
$1 = {_M_len = 26, _M_str = 0x7fa30b77c9d4 "expr.class-subobject-value"}