Enums imported from C headers silently discard Negative explicit values
See original GitHub issueDescribe the bug When importing a C header file containing an Enum which defines at least one of its values as an explicit Negative number, that number is silently ignored, causing incorrect values to be assigned to subsequent entries.
To Reproduce
- Create a C header file containing the following definition:
enum my_enum { invalid = -1, zero, one, two, five = 5, six };
- Use the “Parse C source” function to import the header file.
- In the Data Type Manager, locate “my_enum” and Edit it.
- Observe that the entry “invalid” has a value of 0, “zero” is 1, “one” is 2, and “two” is 3. “five” is properly set to 5 (as its explicit value was positive), and “six” was correctly assigned to 6.
Expected behavior The enum entries should have been assigned as “invalid” = -1 (or 0xFFFFFFFF), “zero” = 0, “one” = 1, “two” = 2, “five” = 5, and “six” = 6. Alternatively, “Parse C Source” should have thrown an error and aborted.
Screenshots N/A
Attachments N/A
Environment
- OS: Windows 7 SP1 x64
- Java Version: 11.0.2
- Ghidra Version: 9.0.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
enum to string in modern C++11 / C++14 / C++17 and future C ...
Declaring a namespace scoped enum requires that the enum first be forward declared inside its namespace scope, then defined in the global namespace....
Read more >C enumeration declarations | Microsoft Learn
The constant-expression must have int type and can be negative. The following rules apply to the members of an enumeration set:
Read more >Diagnostic flags in Clang — Clang 16.0.0git documentation
Some of the diagnostics controlled by this flag are enabled by default. Diagnostic text: warning: A attribute parameter B is negative and will...
Read more >"enum" members other than the first one should not be ...
C static code analysis: "enum" members other than the first one should not be explicitly initialized unless all members are explicitly initialized.
Read more >Changelog — Python 3.11.1 documentation
PurePath.parents sequence of an absolute path using negative index values produced ... C extensions should only include the main <Python.h> header file.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is still an issue in 9.0.4. Ghidra internally treats enums as an unsigned data type, which is a significant oversight imo. A hacky fix would be to modify the enum GUI and source parser to accept negative decimal numbers and convert them to the equivalent two’s complement hex value, but that would still break if the user changes the size (number of bytes) of the type.
@quietust if the builtins enum type is unsigned, then that is likely the cause. I have been having one hell of a time overriding the built-in datatypes, specifically the size of a long. Even when defined as 8 in the cspec files things such as the c header parser don’t care and apply the builtins type anyway. Anything that tries to conflict with the builtins seems to cause s headache. There should be an easy way to override them.