Skip to content

Commit

Permalink
libdrgn: allow formatting absent void objects
Browse files Browse the repository at this point in the history
Object(prog, "void") is a valid object and useful as a placeholder, but
trying to print one raises:

  TypeError: cannot format void object

Allow printing "(void)<absent>" instead.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Aug 29, 2024
1 parent ea87bfe commit fb0da46
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 5 additions & 5 deletions libdrgn/language_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -1530,11 +1530,6 @@ c_format_object_impl(const struct drgn_object *obj, size_t indent,
struct drgn_error *err;
struct drgn_type *underlying_type = drgn_underlying_type(obj->type);

if (drgn_type_kind(underlying_type) == DRGN_TYPE_VOID) {
return drgn_error_create(DRGN_ERROR_TYPE,
"cannot format void object");
}

/*
* Pointers are special because they can have an asterisk prefix if
* we're dereferencing them.
Expand Down Expand Up @@ -1569,6 +1564,11 @@ c_format_object_impl(const struct drgn_object *obj, size_t indent,
return NULL;
}

if (drgn_type_kind(underlying_type) == DRGN_TYPE_VOID) {
return drgn_error_create(DRGN_ERROR_TYPE,
"cannot format void object");
}

SWITCH_ENUM(drgn_type_kind(underlying_type),
case DRGN_TYPE_INT:
case DRGN_TYPE_BOOL:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_language_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -2223,9 +2223,8 @@ def test_function(self):
self.assertEqual(str(obj), "(void (void))0xffff0000")

def test_absent(self):
self.assertRaises(TypeError, str, Object(self.prog, "void"))

for type_ in [
"void",
"int",
"char",
"_Bool",
Expand Down

0 comments on commit fb0da46

Please sign in to comment.