Skip to content

safety: a safe extern "C" fn accepts null and safe code calls it (SIGTRAP) #2216

Closed

Description

@ehartford

Safe code can hold and call a null C function pointer:

fn main:
    let f: extern "C" fn(i32) -> i32 = null
    print(f(1))      // exit 133 (SIGTRAP), no diagnostic

extern "C" fn is the safe callable type (§16.11: only unsafe extern "C" fn needs an unsafe context), so this is a crash in a program with no unsafe. The null literal is also accepted for unsafe extern "C" fn and for c_import function-pointer fields (rec { cb: null }).

Root of the class: nothing distinguishes a nullable C function pointer from a non-null one. D72's zero-storage walk already treats TY_EXTERN_FN as never zero, so the type is non-null by the compiler's own reasoning; the null literal contradicts it.

Fix plan (with #2215): extern "C" fn is non-null and refuses null; the nullable form is Option[extern "C" fn], pointer-sized with null = None (it is 16 bytes today, so it cannot sit in a repr(C) field yet). c_import maps C function-pointer fields and returns to the Option form (§16.2b.2: unknown nullability is nullable).

Activity

  1. ehartford commented on Oct 6, 2026

    @ehartford
    ContributorAuthor

    Fixed by #2218 (73c7cf8): extern "C" fn refuses null with the §16.6 message; the nullable form is Option of it, pointer-sized with None = null (with-abi.md §3 guarantee).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions