question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enums imported from C headers silently discard Negative explicit values

See original GitHub issue

Describe 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

  1. Create a C header file containing the following definition: enum my_enum { invalid = -1, zero, one, two, five = 5, six };
  2. Use the “Parse C source” function to import the header file.
  3. In the Data Type Manager, locate “my_enum” and Edit it.
  4. 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tombsarcommented, Aug 6, 2019

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.

1reaction
astrelskycommented, Apr 29, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found