Skip to content

[C API] Add null check in BinaryenTableGrow for missing table#8376

Open
sumleo wants to merge 1 commit intoWebAssembly:mainfrom
sumleo:fix-tablegrow-null-check
Open

[C API] Add null check in BinaryenTableGrow for missing table#8376
sumleo wants to merge 1 commit intoWebAssembly:mainfrom
sumleo:fix-tablegrow-null-check

Conversation

@sumleo
Copy link
Contributor

@sumleo sumleo commented Feb 25, 2026

Summary

  • Fix null pointer dereference in BinaryenTableGrow when the table name doesn't exist in the module
  • The convenience path where value == nullptr calls getTableOrNull(name) and immediately dereferences the result without a null check
  • Add a null check so that if the table doesn't exist, value remains nullptr and downstream code handles the error gracefully

Test plan

  • Verified the fix compiles and the diff is minimal
  • Checked no other instances of this pattern exist in binaryen-c.cpp
  • Existing c-api-kitchen-sink test continues to work (it passes a non-null value so doesn't exercise this path)

BinaryenTableGrow has a convenience path where passing value=nullptr
auto-creates a ref.null of the table's element type. However, it calls
getTableOrNull(name) and immediately dereferences the result without
checking for null. If the table name doesn't exist in the module, this
causes a null pointer dereference.

Add a null check after getTableOrNull so that if the table doesn't
exist, value remains nullptr and downstream code handles the error.
auto* table = (*(Module*)module).getTableOrNull(name);
if (table) {
value = BinaryenRefNull(module, (BinaryenType)table->type.getID());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if value is null and table is not present, this will just be wrong, so I think it is better to error here, using Fatal() as done elsewhere in the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants